Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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 385 times)
0 Members and 1 Guest are viewing this topic.
Offline deadly72

Junior Member





« Posted 2013-03-30 06: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 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 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 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! Cheesy and if any other information is required let me know!

deadly72
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (24 views)
2014-04-15 18:08:23

BurntPizza (19 views)
2014-04-15 03:46:01

UprightPath (33 views)
2014-04-14 17:39:50

UprightPath (17 views)
2014-04-14 17:35:47

Porlus (33 views)
2014-04-14 15:48:38

tom_mai78101 (59 views)
2014-04-10 04:04:31

BurntPizza (117 views)
2014-04-08 23:06:04

tom_mai78101 (217 views)
2014-04-05 13:34:39

trollwarrior1 (184 views)
2014-04-04 12:06:45

CJLetsGame (191 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org 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‑gaming.org
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!