|
-
Apr 30th, 2012, 08:17 AM
#1
Thread Starter
PowerPoster
Space ship movement formula
Hi guys,
In my 2D game I have a space ship defined by position, speed and angle (or speed vector if you prefer). Now the player can control both, speed and angle, by clicking on the screen. This makes the ship accelerate and turn to the desired speed and angle over time (eg. 2 seconds).
My problem is to calculate the ship's position and angle at any given time. I've had no problems calculating the position as long as there's no change in the angle. That's simply an accelerated body. But what I need is an accelerated rotation at the same time.
Please also note that my game has no "main loop". So in the worst case the code only gets the initial input, like "player wants to accelerate to speed x and angle y", calculates the necessary time, say t, and the next "frame" is calculated after t seconds already.
Can anyone help me with this?
Last edited by Fox; Apr 30th, 2012 at 08:25 AM.
-
Apr 30th, 2012, 09:28 PM
#2
Re: Space ship movement formula
Sure. There's a little calculus in this post.
I assume your angle changes at a constant rate and that your acceleration is also constant. Say your angle is A(t) = A(0) + r*t for a constant r that's determined by the value you want A to be at the end. I'll assume the angle is measured with 0 degrees corresponding to the positive x-axis and 90 degrees corresponding to the positive y axis. In that case, your ship's velocity vector is
V(t) = S(t) * (cos(A(t)), sin(A(t)))
where S(t) is the ship's speed at time t.
Since acceleration is constant, S(t) = S(0) + U*t for a constant U that's determined by the value you want S to be at the end. In all, we have the velocity explicitly as a function of time as
V(t) = ((S(0) + U*t) cos(A(0) + r*t), (S(0) + U*t) sin(A(0) + r*t))
Since velocity is the derivative of position, from the fundamental theorem of calculus position is the integral of velocity. Thus all we have to do is integrate the above from t=0 to your final time T to recover the position formula. Doing so can be done using integration by parts and u-substitution; I can write out the steps, but they should be second nature to anyone in a high school calculus course. After some algebra one finds
P(t) = P(0) +
([r*(S(0) + U*t) sin(A(0) + r*t) - r*S(0) sin(A(0)) + U cos(A(0) + r*t) - U cos(A(0))] / r^2,
[-r*(S(0) + U*t) cos(A(0) + r*t) + r*S(0) cos(A(0)) + U sin(A(0) + r*t) - U sin(A(0))] / r^2)
As a sanity check, the above should reduce to the formula you linked in the limit as r goes to 0. Two applications of L'Hopital's Rule and a few lines of algebra give
lim_{r->0} of P(t)
= P(0) + [S(0) t + Ut^2 / 2] * (cos(A(0)), sin(A(0)))
If A(0) = 0, this reduces entirely to the formula you linked in the x-coordinate with 0 in the y-coordinate, just as it should be. If A(0) = 90 degrees, this reduces correctly in the y-coordinate.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|