# Accelerated movement in 2D space

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

## 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

Cheers,
Mike

##### Share on other sites
I don't get the "I have no game loop"-part? Is it like turn based without updates outside of player interactions?

##### 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 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 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 on other sites
Based on a Google search for fast numerical integration, you can get links like the below:
http://www.codeproject.com/Articles/31550/Fast-Numerical-Integration
http://arminstraub.com/pub/a-fast-numerical-algorithm-for-the-integration-of-rational-functions

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 on other sites
Thanks alot for your help jefferytitan. I'll let you know how it all came out when it's done!

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 47
• ### Forum Statistics

• Total Topics
632636
• Total Posts
3007574
• ### Who's Online (See full list)

There are no registered users currently online

×