Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (682) Games in Android Showcase (196) games submitted by our members Games in WIP (750) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 rotating a 2D vector  (Read 1970 times) 0 Members and 1 Guest are viewing this topic.
weston
 « Posted 2004-12-01 20:03:52 »

I'm pretty new to vector math, I've just learned the basics (adding, subtracting, dot product, normalizing), and I've decided that I want entities in my game engine to store a direction vector and a single movement speed. Before this I had always stored seperate x/y velocities, x/y acceleration, etc. Every example I had seen up until recently seemed to use this pattern as well.

What I'm wondering is how I can rotate this direction vector. I have a variable that stores a rotation value in radians, and I rotate my sprite to this value, but I need to alter the entity's direction vector to match it so that I can give its direction vector to projectiles that is spawns. If you want the specifics, its a gun that I'm rotating and I want to give the gun's direction vector to its bullets when it fires. Right now I just have a value in radians, but I'm not sure how to alter the direction vector to match this.

thanks for any help or even a good link

for(int i = 1; i > 0; i++)
System.out.println(i+" cups of java downed");
weston
 « Reply #1 - Posted 2004-12-01 20:14:42 »

I think I may have solved it, but please let me know if this is right!

seems to me like the x value of my direction vector would be the sine of the rotation value I'm storing (in degrees though?),  and the y value would be the cosine of that same angle. I was also thinking that this would give me an already normalized vector, but I'm not sure if thats true... or if what I suggested is even correct.

for(int i = 1; i > 0; i++)
System.out.println(i+" cups of java downed");
ewills

Junior Devvie

Java skeletal animation systems rock!

 « Reply #2 - Posted 2004-12-01 20:31:04 »

Yup.  That's correct.  The resulting vector is normalized!

Edit:
You'll have to convert degrees to radians to call Math.sin and Math.cos.  To do this, simply multiply the angle in degrees by (float)Math.PI/180.0f.
weston
 « Reply #3 - Posted 2004-12-01 21:30:03 »

I've come across another problem already, acceleration. I'm before using vectors I would do something like:

xVol += xAccel*deltaSeconds;
yVol += yAccel*deltaSeconds;

x += xVol*deltaSeconds;
y += yVol*deltaSeconds;

but since I am now using:

x += direction.x*speed*deltaSeconds;
y += direction.y*speed*deltaSeconds;

I'm not sure how to handle acceleration. It seems like I will need another vector to represent the direction of the acceleration, and perhaps yet another variable to represent the magnitude of the acceleration. But I'm not sure if I should just use the un-normalized acceleration-direction vector instead. Then would acceleration look like this?:

direction.x += accelDirection.x*deltaSeconds;
direction.y += accelDireciton.y*deltaSeconds;

speed += accelDirection.length()*deltaSeconds;

will this work and is it the best way?

for(int i = 1; i > 0; i++)
System.out.println(i+" cups of java downed");
kevglass

« JGO Spiffy Duke »

Medals: 272
Projects: 25
Exp: 18 years

Coder, Trainee Pixel Artist, Game Reviewer

 « Reply #4 - Posted 2004-12-02 05:36:24 »

The problem is you're using speed and not velocity. Your speed should also be an tuple (i.e. x and y).  Then you just add your direction multiplied by some thrust modifier to it when the user thrust in the current direction.

Kev

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 orrenravid (134 views) 2016-07-16 03:57:23 theagentd (202 views) 2016-07-11 14:28:54 Hydroque (285 views) 2016-07-06 05:56:57 Hydroque (432 views) 2016-07-03 08:52:54 GrandCastle (318 views) 2016-07-01 09:13:47 GrandCastle (313 views) 2016-07-01 09:09:45 CopyableCougar4 (368 views) 2016-06-25 16:56:52 Hydroque (357 views) 2016-06-22 02:17:53 SwampChicken (327 views) 2016-06-20 13:22:57 SwampChicken (280 views) 2016-06-20 13:22:49
 Spasi 27x Brynn 23x orangepascal 22x TritonDreyja 22x Archive 22x orange451 20x DavidBVal 19x theagentd 19x Slyth2727 17x Opiop 16x EgonOlsen 14x KaiHH 12x ags1 11x LiquidNitrogen 11x CoDi^R 11x Phased 8x
 Making a Dynamic Plugin Systemby Hydroque2016-06-25 00:13:25Java Data structures2016-06-13 21:22:09Java Data structures2016-06-13 21:20:42FPS Camera Tutorialby Hydroque2016-05-22 05:40:58Website offering 3D Models specifically for games for freeby vusman2016-05-18 17:23:09Website offering 3D Models specifically for games for freeby vusman2016-05-09 08:50:56Website offering 3D Models specifically for games for freeby vusman2016-05-06 11:10:21Website offering 3D Models specifically for games for freeby vusman2016-04-29 12:56:17
 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