Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (764)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D topdown acceleration vectors  (Read 1067 times)
0 Members and 1 Guest are viewing this topic.
Offline deadly72

Junior Devvie

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

Hi folks, been a while Grin, quite a while actually, a few years I'd say Wink... life got in the way of programming what can I say.. Cool.  Anyway I came across an old unfinished project, a 2d topdown car game I was building... remembering a few of my topics and, 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 Grin)

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... Undecided, seems it isn't the case.  Having rummaged through my old thread, I remembered and found a link 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 )

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);
Vector2D deltaV = acceleration.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! Cheesy and if any other information is required let me know!

Pages: [1]
  ignore  |  Print  

EgonOlsen (591 views)
2018-06-10 19:43:48

EgonOlsen (694 views)
2018-06-10 19:43:44

EgonOlsen (478 views)
2018-06-10 19:43:20

DesertCoockie (831 views)
2018-05-13 18:23:11

nelsongames (1026 views)
2018-04-24 18:15:36

nelsongames (1083 views)
2018-04-24 18:14:32

ivj94 (1664 views)
2018-03-24 14:47:39

ivj94 (590 views)
2018-03-24 14:46:31

ivj94 (1501 views)
2018-03-24 14:43:53

Solater (568 views)
2018-03-17 05:04:08
Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46

Deployment and Packaging
by philfrei
2018-08-19 23:53:08

Deployment and Packaging
by philfrei
2018-08-19 23:50:04

Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!