Moving the hero.

This topic is 2507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 on other sites
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 on other sites
[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...[/font]

Share on other sites

[font="arial, sans-serif"]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...[/font]

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 on other sites
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).

Share on other sites
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):

// To move forward: x += cos(rad(angle)) * speed * deltaTime; z += sin(rad(angle)) * speed * deltaTime;
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 on other sites
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 on other sites
'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.

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

Share on other sites
Here it is:

 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; 

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

tyvm

Share on other sites
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 on other sites
I have bad news. It still does not work properly.

now I have something like that:

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

Regards

Share on other sites
What are 'q' and 'e'?