Public Group

# Wild and Crazy Amateur Physics

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

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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...

##### Share on other sites
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.

##### Share on other sites
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!

##### Share on other sites
A meter is about 39.37 inches, so multiply by that.

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
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!

##### Share on other sites
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?

1. 1
Rutin
48
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633409
• Total Posts
3011718
• ### Who's Online (See full list)

There are no registered users currently online

×