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 https://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!