Jump to content
  • Advertisement
Sign in to follow this  
PedroReis

Moving the hero.

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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
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...[/font]

Share this post


Link to post
Share on other sites

Thanks for the reply.
[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 this post


Link to post
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).


thanks in advance.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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]
Post your code.

Share this post


Link to post
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;



graustorad is a degreetorad function



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


tyvm

Share this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!