Representing an objects position in 2D space

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

Recommended Posts

Hello, I have a 2d Ball. The ball is being acted on by gravity, downwards. When the user presses the Up and Down buttons, the ball is acted on by an extra force, the thrust (from its imaginary engine). So my question is, How would i describe the object's position, so that i can update it on each frame? What variables would I need to store, and how would I update the Y position every frame? Let g = 9.8 m/s^2, and the thrust force applies an acceleration of 20 m/s^2 either up or down, depending on what the user pressed. When the user releases the buttons, the thrust is 0.

Share on other sites
Physics calculations are generally best represented using vectors. This applies to most of the ball properties (position, velocity, momentum) as well as any forces acting upon it. (The position is actually a point, not a vector, but it's common to use a vector representation for points as well.)

In 2D a vector is simply (x,y). In most languages (such as C++) you can and should wrap this data up in an object for easy manipulation.

Share on other sites
jyk, thanks, but I'm well aware of what a vector is :)

I just simplified the problem into 1 dimension to make for an easier explanation. Perhaps that was a bad idea.

In actual fact, I have a ball in 2D space, and is always affected by gravity in the Y direction. (My axis is Y down, X right, basically window coordinates). Now, the 'ball' also has some thrusters on each side, i.e. top, bottom, left, right, that can push it in their respective directions.

So basically, my ball's position is represented by two variables on screen, int x, y;

How should I set them each frame, given those constraints?

Share on other sites
You would need to store position and velocity as 2D vectors. Acceleration appears to be constant in your case so you can just use a constants for those. Since your forces are axis-aligned, you don't need any special trig. For a simple physics application like this, Euler integration would probably work just fine. Every frame you do something like this:
const float Gravity = 9.8f;const float Thrust = 20.0f;// Time stepfloat DeltaTime = timeSinceLastFrame();// Right/Up arrow dominant (rewrite logic if you want thrust to cancel when both keys down)velocity.X += (bRightArrow ? Thrust : (bLeftArrow ? -Thrust : 0.0f)) * DeltaTime;velocity.Y += ((bUpArrow ? -Thrust : (bDownArrow ? Thrust : 0.0f)) + Gravity) * DeltaTime;// Vector component-wise addition and scalar multiplication operatorsposition += velocity * DeltaTime;

Keep in mind that 9.8 is a real-world constant that might not work well in your game. You can probably Google "Euler integration" or just check the articles section here on GameDev, to find more information on the subject as well as learn about alternative integration methods.

Share on other sites
ok, thanks, I was thinking something along those lines. I'll try to implement it and see how it goes. (I think i actually had something similar to this originally, before I started viciously hacking away at it :P)

EDIT:
Thanks! That method seems to work really well.