Smooth rotation

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
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Smooth rotation

Post by 3DModelerMan »

How can I change this code

Code: Select all

if ( receiver.IsKeyDown(KEY_UP) )
{
 render_mesh->setRotation( vector3df( 0 , cam_control->getRotation().Y-180 , 0 ) );
}

if ( receiver.IsKeyDown(KEY_RIGHT) )
{
 render_mesh->setRotation( vector3df( 0 , cam_control->getRotation().Y-90 , 0 ) );
}

if ( receiver.IsKeyDown(KEY_LEFT) )
{
 render_mesh->setRotation( vector3df( 0 , cam_control->getRotation().Y+90 , 0 ) );
}

if ( receiver.IsKeyDown(KEY_DOWN) )
{
 render_mesh->setRotation( vector3df( 0 , cam_control->getRotation().Y , 0 ) );
}

if ( receiver.IsKeyDown(KEY_RIGHT) && receiver.IsKeyDown(KEY_UP) )
{
 render_mesh->setRotation( vector3df( 0 , cam_control->getRotation().Y-135 , 0 ) );
}

if ( receiver.IsKeyDown(KEY_LEFT) && receiver.IsKeyDown(KEY_UP) )
{
 render_mesh->setRotation( vector3df( 0 , cam_control->getRotation().Y+135 , 0 ) );
}

if ( receiver.IsKeyDown(KEY_RIGHT) && receiver.IsKeyDown(KEY_DOWN) )
{
 render_mesh->setRotation( vector3df( 0 , cam_control->getRotation().Y-45 , 0 ) );
}

if ( receiver.IsKeyDown(KEY_LEFT) && receiver.IsKeyDown(KEY_DOWN) )
{
 render_mesh->setRotation( vector3df( 0 , cam_control->getRotation().Y+45 , 0 ) );
}
so that it would rotate my character smoothly between key presses?
I tried just rotating until it hit the value greater than what it should be but then adding an extra less than test made horribly buggy.
Should I use animators maybe?
Thanks :D .
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
sp00n
Posts: 114
Joined: Wed Sep 13, 2006 9:39 am

Post by sp00n »

ups, but as i understood of your code, when you've pressed UP and RIGHT for example:

Code: Select all

if ( receiver.IsKeyDown(KEY_UP) ) 
{ 
 render_mesh->setRotation( vector3df( 0 , cam_control->getRotation().Y-180 , 0 ) ); 
}
if ( receiver.IsKeyDown(KEY_RIGHT) ) 
{ 
 render_mesh->setRotation( vector3df( 0 , cam_control->getRotation().Y-90 , 0 ) ); 
}
...
if ( receiver.IsKeyDown(KEY_RIGHT) && receiver.IsKeyDown(KEY_UP) ) 
{ 
 render_mesh->setRotation( vector3df( 0 , cam_control->getRotation().Y-135 , 0 ) ); 
}
you'll get a -180 and -90 and -135 to your Y-coord. Right?
And about smooth - can you explain more without long verses like
until it hit the value greater than what it should be but then adding an extra less than...
?
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

sorry

Post by 3DModelerMan »

Sorry I'll explain better, I was in a hurry to finish last time. Right now it just suddenly sets the rotation. I want it to rotate until it gets to the angle it should be based on the keypress. if I just rotate it until the angle tests
greater than what it should be, it won't rotate from other angles. And if I add an extra less than in there to rotate the other way, when I do the other directions it gets all messed up.
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
hayate
Posts: 43
Joined: Mon Feb 16, 2009 9:38 pm
Location: Brescia, Italy

Re: sorry

Post by hayate »

3DModelerMan wrote:Sorry I'll explain better, I was in a hurry to finish last time. Right now it just suddenly sets the rotation. I want it to rotate until it gets to the angle it should be based on the keypress. if I just rotate it until the angle tests
greater than what it should be, it won't rotate from other angles. And if I add an extra less than in there to rotate the other way, when I do the other directions it gets all messed up.
Why don't you try coding your own idea and if it doesn't work write your code here to ask what have you done wrong? :roll:
Sorry for my awful english ^_^'
Image
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Re: sorry

Post by Acki »

hayate wrote:Why don't you try coding your own idea and if it doesn't work write your code here to ask what have you done wrong? :roll:
simply because it wouldn't be a typical 3DModelerMan-question then... :lol:
just search for his posts and you'll see... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Pyritie
Posts: 120
Joined: Fri Jan 16, 2009 12:59 pm
Contact:

Post by Pyritie »

I'm actually writing something like this right now. :o
Hive Workshop | deviantART

I've moved to Ogre.
zillion42
Posts: 324
Joined: Wed Aug 29, 2007 12:32 am
Location: Hamburg, Germany

Post by zillion42 »

3DModelerMan,

i really wasnt sure what to say, but then again somehow i felt sorry... now you've got a whole saturday night and sunday to get it working, considering the amount of time you're obviously spending, i think you should..
the thing is... :roll: the word 'smooth' somehow contradicts your code in every thinkable way...
And if you think about it, you should come to the same conclusion...
Now irrlicht renders how many times per second ?
So if you want to smoothly rotate something, how much do you have to rotate in each render (frame) ?

:?:

Obviously the answer is, on every frame you have to rotate only a very little amount...
try...

Code: Select all

if ( receiver.IsKeyDown(KEY_RIGHT) )
{
 render_mesh->setRotation( vector3df( 0 , cam_control->getRotation().Y + 0.2 , 0 ) );
} 
and see what happens...
Pyritie
Posts: 120
Joined: Fri Jan 16, 2009 12:59 pm
Contact:

Post by Pyritie »

Almost done here. I'll just add averaging then I'll be done. :3
Hive Workshop | deviantART

I've moved to Ogre.
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Every frame?

Post by 3DModelerMan »

I know that I should rotate by a tiny amount every frame
(I even have my rotation for other stuff time based). But what I need to know is how to stop it from rotating once it gets to a certain point. All the stuff I've tried made it go crazy and I deleted the code to keep trying so I can't post the code.
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
hayate
Posts: 43
Joined: Mon Feb 16, 2009 9:38 pm
Location: Brescia, Italy

Re: Every frame?

Post by hayate »

Acki wrote:Simply because it wouldn't be a typical 3DModelerMan-question then... :lol:
just search for his posts and you'll see... ;)
Unfortunately I know his typical question very well because I started reading this forum some months ago....and this makes me say:
3DModelerMan wrote:I know that I should rotate by a tiny amount every frame
(I even have my rotation for other stuff time based). But what I need to know is how to stop it from rotating once it gets to a certain point. All the stuff I've tried made it go crazy and I deleted the code to keep trying so I can't post the code.
The question contains the answer sometimes....keep trying and shows us some code, I assure you that you're not so far away from the (simple) solution
Sorry for my awful english ^_^'
Image
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Every frame?

Post by randomMesh »

3DModelerMan wrote:stop it from rotating once it gets to a certain point
Pseudocode:

Code: Select all

if (node is at certain point)
{
    dontRotateAnyMore = true;
}
or

Code: Select all

if (!node is at certain point)
{
    rotateNode();
}
Please note that the comparison of floats in C++ isn't as trivial as it seems to be.
A good reading might be this paper.
"Whoops..."
Pyritie
Posts: 120
Joined: Fri Jan 16, 2009 12:59 pm
Contact:

Post by Pyritie »

Hive Workshop | deviantART

I've moved to Ogre.
Post Reply