Sign in to follow this  

Degrees in 2D

Recommended Posts

Hey, I'm wondering about the math behind things rotating in a 2D environment. For example, let's say I'm making an asteroids clone, and I want to rotate my ship a bit. I know how to increase its coordinates to move a ship left, right, up, down, ETC, but I don't understand how you can do turning in a pixilated invironment. Can someone explain how this is done, or point me to some tutorials, or something? Thanks.

Share this post

Link to post
Share on other sites
Its pretty much the same as in a none pixelated environment.

Your objects can be represented as a vector with an origin.
The origin is your objects location on the screen, and the other end of the vector is the direction the object is facing.

origin = (ox,oy)
pointing = (px, py)

px = sin(angle) * distance from origin + ox
py = cos(angle) * distance from origin + oy

likewise you can move your objects around

ox = ox + sin(angle) * moving_distance
oy = oy + cos(angle) * moving_distance

Because you work in rads, sin and cos are always less than 1, meaning your object will move the amount specified in moving_distance.

Working wioth these vectors, its very simple to start including gravity and inertial physics, (infact these dont need trig functions)

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