views:

452

answers:

4

Hi, I'm new here.

I'm trying to make a top-down spaceship game and I want the movement to somewhat realistic. 360 degrees with inertia, gravity, etc.

My problem is I can make the ship move 360° with inertia with no problem, but what I need to do is impose a limit for how fast the engines can go while not limiting other forces pushing/pulling the ship.

So, if the engines speed is a maximum of 500 and the ship is going 1000 from a gravity well, the ship is not going to go 1500 when it's engines are on, but if is pointing away from the angle is going then it could slow down.

For what it's worth, I'm using Construct, and all I need is the math of it.

Thanks for any help, I'm going bald from trying to figure this out.

A: 

Your question is difficult for me to understand but it seems like you're not using real physics for this game. Have you considered using real physics equations such as velocity, acceleration, force, etc?

Edit: After your edits, I think I have a better understanding. You are simply keeping track of the current velocity (or something similar) but you don't keep track of the force where that velocity comes from. The ship should not be storing any of that information (other than engine thrust) -- it should come from the environment the ship is in.

For instance, the environment has a gravity vector (directional force) so you would need to take that into account when calculating the directional force provided by the engine.

Your ship should be storing its own engine force, acceleration, and velocity.

Joe Philllips
+2  A: 

Well, lets consider the realistic problem first and see why this doesn't work and how we have to differ from it. In space as long as your engines are firing, you will be accelerating. Your speed is only limited by your fuel (and in fact you can accelerate faster once you've spent some fuel because your moving less mass).

To give this model an effective maximum speed, you can consider particles in space slowing you down and causing friction. The faster you go, the more particles you're hitting and the faster you're hitting them, so eventually at some fast enough speed, you will be hitting enough particles the amount of decelerating they do exactly cancels out the amount of accelerating your engine is doing.

This realistic model does NOT sound like what you want. The reason being: You have to introduce friction. This means if you cut your engines, you will automatically start to slow down. You can probably count this as one of the unintended forces you do not want.

This leaves us with reducing the effective force of your engine to 0 upon reaching a certain speed. Now keep in mind if your going max speed in the north direction, you still want force to be able to push you in the east direction, so your engines shouldn't be cut out by raw velocity alone, but instead based on the velocity your going in the direction your engines are pointing.

So, for the math:

You want to do a cross dot product between your engine pointing vector and your velocity vector to get the effective velocity in the direction your engines are pointing. Once you have this velocity, say, 125 mph (with a max speed of 150) you can then scale back the force of your engines is exerting to (150-125)/150*(Force of Engines).

This will drastically change the velocity graph of how long it will take you to accelerate to full speed. As you approach the full speed your engines become less and less powerful. Test this out and see if it is what you want. Another approach is to just say Force of Engines = 0 if the dot product is >=150, otherwise it is full force. This will allow you to accelerate linearly to your max speed, but no further.

Now that I think about it, this model isn't perfect, because you could accelerate to 150 mph in the north direction, and then turn east and accelerate to 150 mph going in that direction for a total of 212 mph in the north east direction, so not a perfect solution.

Dan
Dot product, not cross product. Also, the idea of limiting the maximum speed is a neat idea, but to me, special relativity seems like the obvious way to do it. I think that might be more detailed physics than the OP wants to get into, though.
David Zaslavsky
Thanks, I tried this but I get a problem of when the angle change (let's just say by 180 degrees for simplicity) the engines never scale up to the full force.So, if the speed is 150 at angle 0 the Force of Engines is 0 but when the ship turns around it is still 0 but needs to be back at 150.So I need some way of calculating the ships speed at an angle, I guess?Sorry if I'm being obtuse and thanks for the help!
YAS
Friction forces are probably the easiest way to deal with this. Make them proportional to the magnitude of the velocity and tune their strength to cap the speed that the ship can reach. And don't worry about realism; you've already said you're not trying for it and there is (very tenuous) gas in space to provide the friction force. Of course, if this were a truly realistic model then you'd have to take into account the fact that the gas *flows* and is really a fast-moving plasma (solar wind) within star systems. :-)
Donal Fellows
what particles, what friction in space? well, except he flew into nebula :). really, this is from other part of physics. density in space is neglectable.
Andrey
@David Thanks... I've never had to cross out the word cross before. Seems appropriate.
Dan
@Andrey At a great enough speed even small amounts of friction become significant. Though you may already be at relativistic speeds before it matters, not sure. I would think that interplanetary travel would still have a decent amount of particles. On the other extreme, its the void between galaxies that is really negligible. But yes, if you want reasonable limits to speed you will have to use friction factors much higher than actual.
Dan
@Dan, i will just leave these links: http://en.wikipedia.org/wiki/Drag_equation http://en.wikipedia.org/wiki/Interstellar_medium
Andrey
+1  A: 

You need to have three variables for your ship, which you update at each physics time step based on the forces that are acting on it. These will be mass, position, and velocity. (note that position and velocity are single numbers but vectors). At each physics time step you update the position based on the velocity, and the velocity based on the acceleration. you calculate the acceleration based on the forces acting on the ship (gravity, friction, engines)

Newton's equation for force is F = M*A We can rearrange that to A = F/M to get Acceleration. Basically you need to figure out how much the ship should accelerate, and in which direction (vector), then add that acceleration to the ship's velocity, and add the ship's velocity to its position.

Here is the code you should execute each physics time step (I hope you can fill in the blanks) please ask if this is not enough detail

gravity = //calculate force of gravity acting on ship from Newton's law of universal gravitation
friction = //ten percent of the ship's velocity vector, in the opposite direction
engines = 0
if (engines_are_firing)
    engines = 500
forces = gravity + friction + engines
acceleration = forces / ship.mass
ship.velocity += acceleration
ship.position += velocity
redraw()
Nathan
I know nothing of this game creator you are using, but if it has no built in physics engine, it sucks as a game creator. Maybe it has one, look into it.
Nathan
@Nathan: Construct implements Newton Game Dynamics (http://newtondynamics.com/forum/newton.php). I think @YAS probably is using that, since he/she only needs the math (which you correctly provided).
ABach
This is just a stock implementation of Newton's equations; it does not limit the velocity in any way, like YAS asked.
Thomas
@Thomas If you assume engines blasting at maximum force and assume the weight of the ship to be constant and some coefficient of friction, you eventually stop accelerating at some Max Velocity (in this model you can't speed past it with the power of the engines alone... but could with a black hole or something to sling shot around, which is what the OP wanted). While "Max Velocity" isn't one of the inputs, knowing the ships engine power, you can make the max velocity whatever you want by changing the coefficient of friction you are working with.
Dan
+14  A: 
Leftium
Cool trick; I'd never thought about "modifying the speed of light."
dash-tom-bang
Cool answer! (upmodded for originality!) But (correct me if I'm wrong) in this model you couldn't exceed your max speed, even if you sling shot around a black hole or something or if a ship with a larger engine (higher max speed) is towing you. I think the OP may just want a limited speed achievable by your engines alone.
Dan
@Dan: to allow "super-light speeds" simply apply the Lorentz factor only when the engine force is applied. I sum all the forces on the obhject and find the net acceleration, but each force could be applied individually with a different c for each force! (I think this might cause weird behavior depending on the order in which forces are applied, though...)
Leftium
In that case I think you'd have a problem if some other force accelerated the ship to faster than the engines' "light speed;" then the Lorentz factor for the engines would become undefined. Of course you could work around that with conditionals, but it might still be weird. (It's actually possible to prove that in real special relativity, there can't be more than one limiting speed.)
David Zaslavsky
Upvoted for the pretty graph
Nathan
Sorry for my ignorance but what does "/=" mean?
YAS
@YAS:"dv /= lorentz_factor" is C syntactic sugar for "dv = dv / lorentz_factor." Also "v += dv" is short for "v = v + dv."
Leftium
So, I tried this and when I apply it I can only speed up. When it says "only apply Lorentz factor if acceleration increases speed" that would only make it speed up right?I tried changing it to only apply Lorentz factor if speed is less than top speed" and that works except I have to turn the ship more the 90 degrees to change direction. So, once the direction of the movement is 90 degrees to or less, than the direction wont change. Does this sound right?
YAS
@YAS: Acceleration that decreases the speed should still be applied, just without the Lorentz factor adjustment. This is to allow rapid deceleration even at high velocities. When moving at speeds close to the max speed changing direction less than 90 degrees requires moving faster than the max speed. If you want, you could adjust the direction of v as if there were no Lorentz factor "clipping" while still clipping the velocity. I will add another update...
Leftium