Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (289)
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  
  rotating a 2D vector  (Read 1122 times)
0 Members and 1 Guest are viewing this topic.
Offline weston

Junior Member





« Posted 2004-12-01 21: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 Smiley

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

Junior Member





« Reply #1 - Posted 2004-12-01 21: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");
}
Offline ewills

Junior Member




Java skeletal animation systems rock!


« Reply #2 - Posted 2004-12-01 21: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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline weston

Junior Member





« Reply #3 - Posted 2004-12-01 22: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");
}
Offline kevglass
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Reply #4 - Posted 2004-12-02 06: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (60 views)
2013-05-17 21:29:12

alaslipknot (69 views)
2013-05-16 21:24:48

gouessej (99 views)
2013-05-16 00:53:38

gouessej (96 views)
2013-05-16 00:17:58

theagentd (107 views)
2013-05-15 15:01:13

theagentd (98 views)
2013-05-15 15:00:54

StreetDoggy (144 views)
2013-05-14 15:56:26

kutucuk (167 views)
2013-05-12 17:10:36

kutucuk (166 views)
2013-05-12 15:36:09

UnluckyDevil (175 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.099 seconds with 20 queries.