Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have been wanting to solve the issue of movement once and for all... with all that I have learnt and read through, should be a piece of cake one would have thought... , seems it isn't. Having rummaged through stackoverflow, I have found this link http://stackoverflow.com/questions/7312773/mathematical-vectors-and-rotations-topdown-java-game-dev-physics-problem and the bottom solution which explains how I exactly how I want acceleration to act as a 2D vector.

Moving on to the fact that the answer lies in the last post; that is having two vectors totaling to the acceleration vector, I am now faced with a simple yet unanswerable problem with the code I am using. The problem lies in the fact that considering an angle of 0 and 360 degrees both pointing north respectively, I cannot for the life of me figure out why moving up will not keep the angle constant to 0. (apart from the fact that this angle is given through Math.toDegrees(Math.atan2(y, x)))

If we take this code for example ( a direct result of the last thread on stackoverflow )

double secondsElapsed = deltaTime / 1000.0;

double forwardDirection = velocity.angle() + 90;
double leftDirection = forwardDirection + 90;

double forwardAccel = 0;
if (up)
   forwardAccel = 100;
if (down)
   forwardAccel = -100;

double leftAccel = 0;
if (right)
   leftAccel = 100;
if (left)
   leftAccel = -100;

double fDir = Math.toRadians(forwardDirection - 90);
double lDir = Math.toRadians(leftDirection - 90);

Vector2D forwardUnitVector = new Vector2D (Math.cos(fDir), Math.sin(fDir));
Vector2D leftUnitVector = new Vector2D (Math.cos(lDir), Math.sin(lDir));

Vector2D acceleration = forwardUnitVector.scale(forwardAccel);
acceleration.add(leftUnitVector.scale(leftAccel));

Vector2D deltaV = acceleration.scale(secondsElapsed);

velocity.add(deltaV);
position.add(velocity.scale(secondsElapsed));
angle = velocity.angle() + 90;

the angle and motion with the upwards press is set to give acceleration on the x axis (which is fine considering my angle is 90 degrees pointing east). Now realizing that we want a forwardUnitVector of (0,-1) (to get me poiting to the north) I was thinking something along the lines of a resulting degree of 270 (Math.sin(Math.toRadians(270)) = -1 which would be used along the y axis to create motion in the top direction. However, having set the forwardDirection to 360 which would give me the desired result of forwardUnitVector(0, -1) I am given a velocity and an angle which acts as a rotation instead of a simple magnitude on the y axis.

Can anyone please explain to me why my concept of having motion towards the north is flawed?

Thanks for bearing with me!

share|improve this question
add comment

1 Answer

A couple things are off. First, note that the "standard mathematical" coordinates are "X goes right, Y goes up." You are using "X goes right, Y goes south" There isn't anything wrong this this per-se, as long as you remember to translate appropriately.

Next, "standard math" libraries use radians for everything. You seem to be using degrees. Again, this is okay only if you remember to translate all units appropriately. For example, your left vector is definitely wrong because of the lines double forwardDirection = velocity.angle() + 90; double leftDirection = forwardDirection + 90; Since velocity.angle() returns a radian, adding 90 gives a meaningless value.

Also, you don't need fDir, lDir, forwardDirection, or leftDirection; you can calculate forwardUnitVector and leftUnitVector directly:

Vector2D forwardUnitVector = velocity; 
forwardUnitVector.normalize();
Vector2D leftUnitVector = new Vector2D(-fUnit.Y, fUnit.X); 
share|improve this answer
    
Hi Jimmy, thanks for the response, although I was thinking standard cartesian coordinates was X goes right, Y goes up? As for standard libraries, I guess I should have mentioned that the angle is returned in degrees, since I'm using return Math.toDegrees in the vector class for the angle. Thus, forward direction would be 0 + 90 ( always in degrees ) and leftdirection (0 + 90) same case, in degrees again. As far as the code you provided, I do believe that is a more proper way of obtaining and setting the direction, but I am unclear as to why the left unit vector is set to -y, x. –  user29061 Apr 2 '13 at 20:13
1  
oops, you are correct regarding X goes to the right. The point about radians versus degrees is that the line double forwardDirection = velocity.angle() + 90; is wrong since velocity.angle returns a radian measure. It doesn't matter that you call Math.toDegrees later on, you can't add 90 degrees to a radian angle. The leftUnitVector part is because to rotate a vector (x,y) by 90 degrees counterclockwise, it results in (-y,x). For example, (1,0) (right) rotated by 90 counterclockwise results in (0,1) (up), rotated again results in (-1,0) (left), etc. –  Jimmy Apr 2 '13 at 20:17
    
I'm sorry to have to insist about velocity.angle() returning in degrees.. this is my angle method in my vector class public double angle() { return Math.toDegrees(Math.atan2(y, x)); } as you can see, the angle is returned in degrees so I am able to play with it as angles inside the other class. I am although fully aware that I can not add degrees with radians, or radians with degrees. As far as normalizing the vector, should I return a (0,0) vector if the magnitude is zero? –  user29061 Apr 2 '13 at 20:48
    
okay. I was assuming you were using something like the Java2D Vector2D. Normalizing a zero vector is an undefined operation, but returning (0,0) is one way to handle it –  Jimmy Apr 2 '13 at 20:59
    
Alright, well that does make sense, except how will I handle scalling the acceleration vector of a zero vector velocity? Scaling the acceleration vector off of a normalized velocity vector which returns 0 will simply give a zero vector acceleration. What exactly am I missing? –  user29061 Apr 2 '13 at 22:05
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.