Saturday, March 17, 2018

mathematics - How do you get an object to orbit around a certain point on a 2d plane?


I had a concept for a game come to me this morning but I can't figure out how I'd go about implementing the movement.


The general idea involves the left and right controls causing a sprite to go round the center of the plane/(or the centre of the game area) in an orbit rather than moving left and right and the up and down will cause the orbit distance to either increase or decrease unless there is something in the way.


The best way I can think of doing this would be to maintain the polar coordinates of my sprite and convert that to Cartesian coordinates for drawing. Would that work? Is there an easier way?


Also how would I go about ensuring that the sprite was rotated such that it was at a right angle to the line from the center point?


Would I be better off implementing this using gravity and a using a repelling force to keep the character at the desired distance? If so How would I go about doing this?



Answer



Unless you're going to have so much going on that the speed of trig functions is going to be a concern, I think focusing on a Polar system will be sufficient.



Storing the angle and distance-from-center for your sprite (Theta and R, respectively) will make the logic for moving based on Left, Right, Up, and Down understandable: Moving Left and Right would change your angle while Up and Down change the distance.


To draw the sprite, converting to Cartesian coordinates is pretty straight forward: x = R cos(Theta) and y = R sin(Theta) (plus some offset to rotate around the center of the screen/playing area/whatever).


Rotation of the sprite would simply be based on the Theta you're already storing. Either exactly equal to or plus a constant, depending on the initial orientation of the sprite.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...