Sign in to follow this  
PedroReis

Moving the hero.

Recommended Posts

PedroReis    100
Hello everyone.
Here comes a newbie question:

I'm trying to make a mini-game.
In this game I need to move the Hero. My problem here is that I need to rotate the axis (I think) when I rotate the hero.
In other words, I need to rotate the axis of the hero, not the hero!
I'm using OpenGL.
I appreciate any kind of help.
Regards

Share this post


Link to post
Share on other sites
jyk    2094
Hm, not sure what you mean. Generally you'd store the angle of rotation of the character somewhere (assuming it's essentially a 2-d problem), and then create a transform matrix from the angle and position for uploading to OpenGL. (Or use glRotate*(), etc. if you're using the now-deprecated convenience functions.)

Is that what you're trying to do?

Share this post


Link to post
Share on other sites
PedroReis    100
Thanks for the reply.
[font=arial, sans-serif][size=2]I want to rotate the hero and move forward, but my problem is that if i use glrotatef() and then gltranslate() the hero turn for 1 side and then move for the "old front" of himself instead of going infront of hes new direction...[/size][/font]

Share this post


Link to post
Share on other sites
jyk    2094
[quote name='ocoiso' timestamp='1302732635' post='4798163']
Thanks for the reply.
[font="arial, sans-serif"][size="2"]I want to rotate the hero and move forward, but my problem is that if i use glrotatef() and then gltranslate() the hero turn for 1 side and then move for the "old front" of himself instead of going infront of hes new direction...[/size][/font]
[/quote]
Make sure you're applying the transforms in the correct order, and that you're moving the character along a direction vector that corresponds to the character's orientation.

If that doesn't help, perhaps you could post some of your code.

Share this post


Link to post
Share on other sites
PedroReis    100
I did not understand :S

I have this code (part of it).

.......
glTranslatef(x,y,z);
glRotatef(ang,0,1,0);
drawhero();
.......

and everytime I press 'w' key it increases the x variable ('s' key decreases). and everytime I press 'a' key it increseases ang variable ('d' key decreases).


thanks in advance.

Share this post


Link to post
Share on other sites
jyk    2094
That looks fine, but if you want the character to move in the direction it's facing, you'll need to use a little trig, e.g. (untested):

[code]// To move forward:
x += cos(rad(angle)) * speed * deltaTime;
z += sin(rad(angle)) * speed * deltaTime;[/code]
Note that you may need to re-arrange the code a bit depending on what conventions you're using. (Note also that in practice you'd want to use a user-defined 'vector' type of some sort for this, but in the meantime, the above should at least get you started.)

Share this post


Link to post
Share on other sites
PedroReis    100
hm. I think that is what I need.
I was doing something like this:

---
x+=0.1;
-----

I implemented your code, but it didnt work properly.
Btw, what is deltatime?

Share this post


Link to post
Share on other sites
jyk    2094
'deltaTime' is the elapsed time in seconds since the last frame or update. The purpose is to make the rate or motion independent of frame rate. If you're just trying to get the basics working, you can ignore it for the moment.

[quote]I implemented your code, but it didnt work properly. [/quote]
Post your code.

Share this post


Link to post
Share on other sites
PedroReis    100
Here it is:

[code]

case 'w': z+=sin(graustorad(ang))*0.2;
break;
case 's': z-=sin(graustorad(ang))*0.2;
break;


case 'a': x+=cos(graustorad(ang))*0.2;
break;
case 'd': x-=cos(graustorad(ang))*0.2;
break;
[/code]


graustorad is a degreetorad function

[code]

float graustorad(float val) {
return ((M_PI*val)/180);
}
[/code]

tyvm

Share this post


Link to post
Share on other sites
jyk    2094
Let's just keep it simple and start with forward motion only ('w'). When the 'w' key is pressed, you want to execute *both* of the lines of code I posted earlier (they're not intended to be split up as you have them there).

Try that and see what happens. (You may still find that your character doesn't move in the right direction, but it should get you closer.)

Share this post


Link to post
Share on other sites
PedroReis    100
Thanks for your help.
I have bad news. It still does not work properly.

now I have something like that:

[code]

case 'w': q+=cos(graustorad(ang))*0.2;
e+=sin(graustorad(ang))*0.2;
break;
[/code]

Regards

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this