Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (538) Games in Android Showcase (132) games submitted by our members Games in WIP (600) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 2D topdown acceleration vectors  (Read 511 times) 0 Members and 1 Guest are viewing this topic.

Junior Devvie

 « Posted 2013-03-30 05:01:01 »

Hi folks, been a while , quite a while actually, a few years I'd say ... life got in the way of programming what can I say.. .  Anyway I came across an old unfinished project, a 2d topdown car game I was building... remembering a few of my topics http://www.java-gaming.org/topics/car-rotation-problem-in-top-down/23817/60/view.html and http://www.java-gaming.org/topics/mathematical-vectors/23930/view.html, and well I must say that now I have been wanting to get a move on with the project; that is now that I have some free time on my hands! (work isn't being as invasive )

So I wanted to come back and 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 the case.  Having rummaged through my old thread, I remembered and found a link http://stackoverflow.com/questions/7312773/mathematical-vectors-and-rotations-topdown-java-game-dev-physics-problem which explains how I wanted 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 (as i recalled being told a few times in my earlier posts on JGO), I am now faced with a simple yet unanswerable problem with the code I have used.  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 )

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31 `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 a result of forwardUnitVector(0, -1) I am given a velocity and an angle which acts as a rotation and not a simple magnitude.

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

Thanks for bearing with me through this post! and if any other information is required let me know!