Sign in to follow this  

Accelerated movement in 2D space

This topic is 2051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone,

In my 2D game I have a space ship defined by position, velocity and angle. Now by clicking on the screen the player sets a new target velocity and angle. The ship then accelerates and turns to the desired velocity and angle over 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 velocity x and turn to angle y", calculates the necessary time t, and the next "frame" is calculated after t seconds.

Can anyone help me with this?

Yhoko

Share this post


Link to post
Share on other sites
@Mike: That's correct. I want the ships to be able to change velocity and angle at the same time. I'm fine with uniform acceleration for both, btw.

@japro: Yes, kind of. For clarification, this is a network game and we're talking about the server-side calculations. On the client side a main loop renders the game constantly, but the server only calculates each vessels position when it's necessary (that is, on user input or script events).

Share this post


Link to post
Share on other sites
I think first you need to decide whether the turn and the acceleration happen at the same time. Different profiles for angular acceleration and linear acceleration would result in the ship ending at vastly different positions. The problem is essentially integration of a non-trivial function, which is not always possible exactly. You could use a numerical method to approximate it I guess.

For example, if we look at a turn/acceleration where we're turning 90 degrees at a constant angular velocity and accelerating from 0 m/s to 10 m/s over a period of 4 seconds (excuse any small mathematical errors):
Angle theta = (pi / 8) * t
Linear acceleration = 10/4
Linear speed = (10/4) * t
X position = Integral (t: 0 to 4) of sin((pi / 8) * t) * (10/4) * t
Y position = Integral (t: 0 to 4) of cos((pi / 8) * t) * (10/4) * t

Now I don't remember how to integrate that analytically, but even worse if we have angular acceleration instead of a constant angular velocity:

Angle theta = (pi / 32) * t ^ 2
Linear acceleration = 10/4
Linear speed = (10/4) * t
X position = Integral (t: 0 to 4) of sin((pi / 32) * t ^ 2) * (10/4) * t
Y position = Integral (t: 0 to 4) of cos((pi / 32) * t ^ 2) * (10/4) * t

That just makes my brain hurt, and it doesn't even count slowing down at the other end. Again, excuse any mathematical errors. It's been A LONG TIME since I did calculus. ;)

Share this post


Link to post
Share on other sites
Yep, that's exactly my problem :-) I was hoping for someone who's (still) familiar with this stuff.

And yes, both needs to happen at the same time. The example you named fits my situation exactly.

So... how can I integrate that into code?

Share this post


Link to post
Share on other sites
Based on a Google search for fast numerical integration, you can get links like the below:
[url="http://www.codeproject.com/Articles/31550/Fast-Numerical-Integration"]http://www.codeproject.com/Articles/31550/Fast-Numerical-Integration[/url]
[url="http://arminstraub.com/pub/a-fast-numerical-algorithm-for-the-integration-of-rational-functions"]http://arminstraub.com/pub/a-fast-numerical-algorithm-for-the-integration-of-rational-functions[/url]

These are not guaranteed to be exactly correct, but very close for arbitrary continuous functions over an interval. Do it separately for X and Y components. From that point you could trust the client if it's within 2% of your calculated answer, and snapback to the calculated value otherwise.

Share this post


Link to post
Share on other sites

This topic is 2051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this