Moving Objekt in Rotation-Direction

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
blackb
Posts: 13
Joined: Mon Oct 26, 2009 2:25 pm

Moving Objekt in Rotation-Direction

Post by blackb »

Hi @ all,

I have got a little Problem with moving objects.
What I tried to do was turn the object by ->setrotation() and then move it to this direction.
I've read some tutorials about it and I know that I have to use sin/cos functions for it...but I did not understand how to do that. :oops:
I've tried some codes I've found here in the forum, but they did not work :-/

Code: Select all

if(receiver.IsKeyDown(irr::KEY_UP))
      {
         Charpos   = character->getPosition();
         charrotate = character->getRotation();

		 Charpos.X = Charpos.X + ( MOVEMENT_SPEED * cos(charrotate.Y*pi/180));
		 Charpos.Z = Charpos.Z - ( MOVEMENT_SPEED * sin(charrotate.Y*pi/180));
         character->setPosition(Charpos);
      }
Thats what I've got for moving forward, but the object is not moving -.-
I am using the Eventreceiver from the Moving Tutorial.

Can anyone see the mistake in this?

BB

EDIT:Now I understand this sin/cos functions, but they doesn't work any better...I have got improved my code a little bit (I think):

Code: Select all

if(receiver.IsKeyDown(irr::KEY_UP))
		{
			  pos.Z += cos(rot.Y)*speed;
			  pos.X += sin(rot.Y)*speed;
			  character->setPosition(pos);

		}
But It doesn't work...
Valmond
Posts: 308
Joined: Thu Apr 12, 2007 3:26 pm

Post by Valmond »

Okay maybe you are all in on it with the debugger,
but otherwise, does it work if you do like this?

Code: Select all

if(receiver.IsKeyDown(irr::KEY_UP))
      {
           pos.Z += 100.0f;
           pos.X += 100.0f;
           character->setPosition(pos);
           printf("up\n");
      }
If it does (and the printf writes to the output window) then maybe your speed variable isn't big enough (it's a float right?).

If it doesn't (move or print) then the problem might be elsewhere.
blackb
Posts: 13
Joined: Mon Oct 26, 2009 2:25 pm

Post by blackb »

It didn't move or print the text when I tried your code.
But I think the printf didn't work because of this in my code :D:

Code: Select all

#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
I tried this:

Code: Select all

if(receiver.IsKeyDown(irr::KEY_UP))
		{
			  pos.Z += cos(rot.Y)*MOVEMENT_SPEED;
			  pos.X += sin(rot.Y)*MOVEMENT_SPEED;
			
			  character->setPosition(pos);
			  device->setWindowCaption(L"HALLO");
		}
And the Caption changed, but the character did not move.
and my speed is an f32 variable (don't know if it is float or not, never saw the declaration of it :-/)

Here the declaration of my MOVING_SPEED variable:

Code: Select all

const f32 MOVEMENT_SPEED = 5;
I also tried these two:

Code: Select all

const f32 MOVEMENT_SPEED = 5.f;
const f32 MOVEMENT_SPEED = 5.5f;

because I don't really know how this .f works or what it is for. I tried other variable types,too (float,int,double).
I don't know why it is not workin at all :-/...

BB
Valmond
Posts: 308
Joined: Thu Apr 12, 2007 3:26 pm

Post by Valmond »

try like 500.0 or 5000.0, 5.0 might be small (it all depends of the setup of course).

Maybe the code is called only once every keystroke so when you hit 'up' you move just 5 units Once...
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Post by 3DModelerMan »

Try using vector3d<T>::rotationToDirection(); on your object's rotation angle vector to get the direction vector Then normalize. And finally multiply the direction vector into the movement.
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
blackb
Posts: 13
Joined: Mon Oct 26, 2009 2:25 pm

Post by blackb »

Code: Select all

core::stringw str = L"Irrlicht- [";
	str += "Position= ";
	str += character->getPosition().X ;
	str += ":"; 
	str += character->getPosition().Y ;
	str += ":";
	str += character->getPosition().Z;
	str += "   Rotation= ";
	str += character->getRotation().X ;
	str += ":"; 
	str += character->getRotation().Y ;
	str += ":";
	str += character->getRotation().Z;
	str += "]";

	device->setWindowCaption(str.c_str());
I use that code to display my position and rotation in the windowcaption, and the values in the position part are all 0.0000 all the time, so I don't think the MOVEMENT_SPEED is to small (I tried 5000 as value of course, but it did not work).
Try using vector3d<T>::rotationToDirection(); on your object's rotation angle vector to get the direction vector Then normalize. And finally multiply the direction vector into the movement.
I don't think that I did really understand what you mean. Could you explain it to me,please(Especially what "Normalize" is :oops: )?

Thanks
BB
Valmond
Posts: 308
Joined: Thu Apr 12, 2007 3:26 pm

Post by Valmond »

I don't think that I did really understand what you mean...
That is how to move "correctly" (right angle etc.), your problem seems (to start with) to be that the object doesn't move at all.

I guess a bit more code is needed to get what's wrong :)


ps. normalize means taking a vector and scale it so it's length equals one.
blackb
Posts: 13
Joined: Mon Oct 26, 2009 2:25 pm

Post by blackb »

I'm a little bit confused :-/
I never worked with vector3d before...allways used vector3df and don't know exactly what the difference is between them. :oops:

BB

EDIT: Problem is solved. For those who are interested in the code:

Code: Select all

const f32 MOVEMENT_SPEED = 0.015f;
const float PI=3.1415926535;

double grad2rad(double grad) 
{
   return (grad*PI/180);
}

core::vector3df rot=farn->getRotation();
core::vector3df pos=farn->getPosition();

if(receiver.IsKeyDown(irr::KEY_UP))
        {
              pos.Z += cos(grad2rad(rot.Y))*MOVEMENT_SPEED;
              pos.X += sin(grad2rad(rot.Y))*MOVEMENT_SPEED;
            
              farn->setPosition(pos);
        }

if(receiver.IsKeyDown(irr::KEY_DOWN))
      {
              pos.Z -= cos(grad2rad(rot.Y))*MOVEMENT_SPEED;
              pos.X -= sin(grad2rad(rot.Y))*MOVEMENT_SPEED;
            
              farn->setPosition(pos);
      }

if(receiver.IsKeyDown(irr::KEY_LEFT))
            nodeRotation.Y -= 4*MOVEMENT_SPEED;
else if(receiver.IsKeyDown(irr::KEY_RIGHT))
            nodeRotation.Y += 4*MOVEMENT_SPEED;
Thanks for all your help :)
Valmond
Posts: 308
Joined: Thu Apr 12, 2007 3:26 pm

Post by Valmond »

So what was it :D ?
blackb
Posts: 13
Joined: Mon Oct 26, 2009 2:25 pm

Post by blackb »

The position was overwritten in another part of the code :oops:

You should know, this code is just a great experiment and has grown from my first steps with Irrlicht...I think when I have got the code I need for the "real" project I will rewrite it all, this code is to chaotic :D
With this code I am just testing some methods and throw together what I need...copy and paste from here to there and back aggain...

I think I will sell it to a programmer who needs a reason for suizid :twisted:

BB
Valmond
Posts: 308
Joined: Thu Apr 12, 2007 3:26 pm

Post by Valmond »

NP and good luck (except maybe for the selling stuff ;) )
Post Reply