How to find strafe vectors?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
kornerr
Posts: 245
Joined: Thu Jul 06, 2006 9:57 am
Location: Russia, Siberia, Kemerovo
Contact:

How to find strafe vectors?

Post by kornerr »

In main loop I update camera position to match object position. I use FPS camera, so I have mouse movements cooked for me. But that doesn't suit for keys, and I have to manually move object with keys.
Well, I can move object forward and backward very easy, because I can just get camera position and target. But how will I get left or right strafe vectors?
Thanks.
Open Source all the way, baby ;)
OSRPG
kornerr
Posts: 245
Joined: Thu Jul 06, 2006 9:57 am
Location: Russia, Siberia, Kemerovo
Contact:

Post by kornerr »

it turned out that if (x, y) is my forward vector, then (-y, x) is the left vector :)
Open Source all the way, baby ;)
OSRPG
Mikenoworth
Posts: 78
Joined: Sat May 27, 2006 9:24 pm
Location: Logan, UT

Post by Mikenoworth »

That one Irr modder who make a library and has a funny stickfigure avatar has some good code for what you want. Here, I use it myself:

Code: Select all

	
irr::core::vector3df WorldUpVector = irr::core::vector3df( 0.0f, 1.0f, 0.0f );

	irr::core::vector3df cameraPosition = GetCamera()->getPosition();
	irr::core::vector3df cameraTarget = GetCamera()->getTarget();

	// Compute new forward vector 
	irr::core::vector3df forwardVector = cameraTarget - cameraPosition; 
	// Set the Y to 0 so the vectors are calculated on the XZ plane 
	forwardVector.Y = 0.0f; 
	forwardVector.normalize(); 

	// Compute new right vector 
	irr::core::vector3df rightVector = WorldUpVector.crossProduct ( forwardVector ); 
	rightVector.normalize(); 

	// Compute new up vector 
	irr::core::vector3df upVector = forwardVector.crossProduct( rightVector ); 
	upVector.normalize(); 


You can't just copy-n-paste-n-compile, but the basic ideas are there. Having these vectors makes it so much easier to implement your own camera movement. Gah I wish I could remember his nick, Spintz?
Stout Beer
kornerr
Posts: 245
Joined: Thu Jul 06, 2006 9:57 am
Location: Russia, Siberia, Kemerovo
Contact:

Post by kornerr »

Thanks, Mikenoworth.
But all this works only when camera was created as FPS one. When I create basic one, instead of moving left/right it turns around center :/
How to make it move, not turn?
Thanks.
Open Source all the way, baby ;)
OSRPG
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Ok lets say ur character is standing still at the beginning with a rotion of 0° degrees around the y(up)-axis. then u cpul dfor exampel say that:

0 0 1 is forward
0 0 -1 is backward
1 0 0 is strafe right
-1 0 0 is strafe left

this works as long you don't rotate your character but i guess u want to turn him as well therefor u use a matrix. simply do:

irr::core::matrix4 mat;
mat.setRotationDegrees(the rotation of the character around the Y-Axis)

mat.transform(forward);
mat.transform(backward);
mat.transform(strafe right);
mat.transform(strafe left);

and tada it works
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

That one Irr modder who make a library and has a funny stickfigure avatar has some good code for what you want
LOL, you like my stick figure!!!! :twisted:
Image
zehsound.dude
Posts: 4
Joined: Mon Jun 02, 2008 6:33 pm

Post by zehsound.dude »

Hello ppl

I realize this thread is a year old but I just started using Irrlicht and since this was confusing me and couldn't find it anywhere in the forums, I thought I should post it in case someone else is confused by it too. I also JUST figured out how to do this the way I wanted it to work.
But all this works only when camera was created as FPS one. When I create basic one, instead of moving left/right it turns around center :/
How to make it move, not turn?
all you do is move the camera target by the vectors that you move the camera position by.

so therefore...

Code: Select all

irr::core::vector3df WorldUpVector = irr::core::vector3df( 0.0f, 1.0f, 0.0f );

   irr::core::vector3df cameraPosition = GetCamera()->getPosition();
   irr::core::vector3df cameraTarget = GetCamera()->getTarget();

   // Compute new forward vector
   irr::core::vector3df forwardVector = cameraTarget - cameraPosition;
   // Set the Y to 0 so the vectors are calculated on the XZ plane
   forwardVector.Y = 0.0f;
   forwardVector.normalize();

   // Compute new right vector
   irr::core::vector3df rightVector = WorldUpVector.crossProduct ( forwardVector );
   rightVector.normalize();

   // Compute new up vector
   irr::core::vector3df upVector = forwardVector.crossProduct( rightVector );
   upVector.normalize(); 

  // Then to "slide" the camera to the right and change camera target to move with it
  GetCamera->setPosition( GetCamera->getPosition() + rightVector);
  GetCamera->setTarget(GetCamera->getTarget() + rightVector);
if you want to rotate the camera... you still gotta figure that out :wink:

ps: this is my first post evah! :)
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

You popped your post cherry by sharing knowledge? I think I love you. Let's hug. Good touch hug.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply