Sign in to follow this  
scorpion007

Representing an objects position in 2D space

Recommended Posts

scorpion007    118
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 this post


Link to post
Share on other sites
jyk    2094
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 this post


Link to post
Share on other sites
scorpion007    118
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 this post


Link to post
Share on other sites
Zipster    2359
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 step
float 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 operators
position += 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 this post


Link to post
Share on other sites
scorpion007    118
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.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by scorpion007
jyk, thanks, but I'm well aware of what a vector is :)
Sorry, my bad (I guess I misinterpreted your question).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this