Yhoko 122 Report post Posted April 30, 2012 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 0 Share this post Link to post Share on other sites
h4tt3n 1974 Report post Posted April 30, 2012 A simple way to go about this would be to rotate the ship to the desired angle first and then accelerate it, but I suppose you've already thought about this? Cheers, Mike 0 Share this post Link to post Share on other sites
japro 887 Report post Posted April 30, 2012 I don't get the "I have no game loop"-part? Is it like turn based without updates outside of player interactions? 0 Share this post Link to post Share on other sites
Yhoko 122 Report post Posted April 30, 2012 @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). 0 Share this post Link to post Share on other sites
jefferytitan 2523 Report post Posted April 30, 2012 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. ;) 0 Share this post Link to post Share on other sites
Yhoko 122 Report post Posted April 30, 2012 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? 0 Share this post Link to post Share on other sites
jefferytitan 2523 Report post Posted April 30, 2012 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. 0 Share this post Link to post Share on other sites
Yhoko 122 Report post Posted May 1, 2012 Thanks alot for your help jefferytitan. I'll let you know how it all came out when it's done! 0 Share this post Link to post Share on other sites