- Home /
Question by
Asker · Nov 05, 2016 at 03:09 PM ·
rotationtranslation
Rotation& translation problem
I'm working on simple spacecraft simulation and I'm trying to implement spacecraft control. I can turn on engines of shuttle with space, rotate the spacecraft with w, s vertically and with a, d horizontally. But I have problem with rotation - it weirdly affects translation.
http://i.imgur.com/duyiy3i.gifv
My spacecraft control script:
private float verticalRotation, horizontalRotation, rotationSpeed;
private Kinematics kinematics;
void Start()
{
rotationSpeed = 0.25f;
kinematics = gameObject.GetComponent<Kinematics>();
}
void Update()
{
verticalRotation += Input.GetAxis("Vertical");
horizontalRotation += Input.GetAxis("Horizontal");
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(verticalRotation * rotationSpeed, horizontalRotation * rotationSpeed, 0), Time.deltaTime);
if (kinematics != null)
{
if (Input.GetKey(KeyCode.Space))
kinematics.EngineAcceleration = 0.0025f;
else
kinematics.EngineAcceleration = 0;
}
}
And kinematics script:
private float initialVelocity, velocity, acceleration, engineAcceleration, gravitationalAcceleration, motionTime;
private Vector3 initialPosition, position;
public float EngineAcceleration
{
set
{
engineAcceleration = value;
}
}
void Start()
{
initialPosition = transform.position;
position = initialPosition;
}
void FixedUpdate()
{
UpdateAcceleration();
UpdateVelocity();
UpdateMotionTime();
UpdatePosition();
}
void UpdateAcceleration()
{
acceleration = engineAcceleration + gravitationalAcceleration;
}
void UpdateVelocity()
{
velocity = initialVelocity + (acceleration * motionTime);
}
void UpdateMotionTime()
{
if (velocity != 0 || acceleration != 0)
motionTime += Time.deltaTime;
else
{
motionTime = 0;
initialPosition = transform.position;
}
}
void UpdatePosition()
{
position.x = initialPosition.x + (((initialVelocity + velocity) * motionTime * 0.5f) * transform.up.x);
position.y = initialPosition.y + (((initialVelocity + velocity) * motionTime * 0.5f) * transform.up.y);
position.z = initialPosition.z + (((initialVelocity + velocity) * motionTime * 0.5f) * transform.up.z);
transform.position = position;
}
I have to use my own physics. So please don't give me answers with unity physics methods.
Comment