Wild and Crazy Amateur Physics

Started by
8 comments, last by toblix 19 years, 5 months ago
So, I'm trying to make some kind of lander game, and wanted to get the craft to move the way it should. The problem is, no matter what values I give the different constants like GRAVITY or ENGINE_POWER, it ends up first sinking slowly, and then when I turn on the power, it eventually starts flying up, but continues off the screen, deccelerating slowly, and then when it comes back it's too fast and I can't control it. I know something's wrong but I can't quite see what. Also, I'm not that good at physics, as you can probably see. Here's the code for the physics part:
void fysikk(clock_t delta){
	//Oppdaterer skip
	ship.forces = gravity;
	if(girGass){
		ship.forces+=(Vector2d(cos(ship.angle), sin(ship.angle)))*ENGINE_POWER;
	}
	ship.acceleration = ship.forces/ship.mass;
	ship.velocity+=ship.acceleration*delta/1000;
	ship.x+=ship.velocity.x*delta/1000;
	ship.y+=ship.velocity.y*delta/1000;
}

I should probably mention that forces, gravity, acceleration and velocity are vectors.
Advertisement
What data types are you using for your values? floats, ints, doubles? I could imagine you getting some serious problems due to rounding errors if you use ints, and the range is rather small. If so, try making it floats or doubles real quick to see what happens.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
No, no, I'm using doubles for everything. I'd post all the code, but I'm not sure anyone would bother reading it. Everything moves smoothly, but the problem is that something's off with either the values or the calculations, since even though things run smoothly and looks all right at first glance, I'm unable to keep the ship inside the window for long (I haven't any constraints on the ship's position). Let me try to explain:

1. The program starts, with the ship in the middle of the window.
2. The ship starts sinking slowly.
3. I press the engine key (which is a pretty lame name for it).
4. The ship starts slowing down.
5. I let go of the engine key the moment I see that vy is approx. = 0.
...and here's where the trouble starts...
6. The ship continues to accelerate upwards and flies off the screen, and then , some seconds later come rushing down again. Now the speed is so big there's no hope of getting the ship under control again.

Shouldn't the ship start falling again the moment I let go of the key? It continues accelerating, though, and I can't see why...
Your handling of the forces, accel, vel, and pos all look correct. Definately use floats! You probably need to keep tweaking your values. Make sure your units are consistant. I always make that mistake when dealing with gravity. I'll use 9.81 but my units are inches so it doesn't work right, etc. Another thing you can do is to fake it somewhat (games don't always have to have exactly realistic physics under the hood to feel right). Maybe when you turn on the engine, don't use gravity. You could also clamp the ship a terminal velocity. If there is an atmosphere, an object won't continue to accelerate to infinity -- it's velocity will remain constant at some point based on air resistance.
I'll try that. I guess I was hoping to get the right feeling using as little "cheating" as possible, but after all, nobody cares if it's right as long as it feels good, eh? I'll try turing gravity off while the power is on.

Thanks!
A meter is about 39.37 inches, so multiply by that.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
I'm pretty sure gravity shouldn't be divided by your ship's mass. It should fall at the same speed no matter how big it is.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Thanks for all the replies! I found out what I needed: terminal velocity! Now I've got everything under control and I'm ready to implement rotation.

One thing, though, what's the policy here on showing code and distributing binaries?
Remember, though, that in space there is no terminal velocity (at least none due to air resistance) if there is no atmosphere. Perhaps your planet has an atmosphere though, so that's cool.

And you should always have your gravity accelerating your spaceship downwards. Good luck with your game, I tested a game with a similar theme (different gameplay though), you can download the demo (made by Fusion Apple Entertainment) if you need ideas, etc:

Mission: Land!
"Learn as though you would never be able to master it,
hold it as though you would be in fear of losing it" - Confucius
Okay, so now there are new problems. Currently, the game looks like this:
http://home.lyse.net/dahle/lander.jpg (Okay, I give up. What's the image code around here)

One thing I found was that OpenGL wouldn't draw my concave ground polygon, so I had to go with GL_LINE_STRIP instead. Any easy way of getting the polygon tesselated so OpenGL accepts it, or do I have to do it manually?

Stuff I want to add is some particle effects (from the rocket), collisions (with edges and ground) and the ability to land.

Any hints on what method to use for detecting collision against the ground?

This topic is closed to new replies.

Advertisement