Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

tgn376j

Movement problem in C++.

This topic is 5234 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

This is my first post here and it will be fun the see how fast you can crack this little problem of mine: I am creating a actionpacked game with a game engine simular to Blood Omen: Legacy Kain. It all works fine with the layers and so on(using CDX to wrap DirectX). Now heres where I have messed up. All the character is supossed to be turning with the same funtion but I dont get it to work. So here is the layout. Every guy has 8 positions: 1:Up 2:Diagonal RightUp 3:Right 4:Diagonal RightDown 5:Down 6:Diagonal LeftDown 7:Left 8:Diagonal LeftUp When you press RIGHT the guy shall turn his body around so he faces right and then walk. But he shall turn the fastest way. For example: If he is at postion 7:left and we press UP he should rotate to the right(+1) and not to the left(-1). I really want the whole thing to be with only ONE ''if''. Something like this: if (x - y = 1) { rotate(left); } else { rotate (right); } x where he is, y where he wants to get. I know this isnt the right formula to use and that is what I want you to help me with or if you have another solution.

Share this post


Link to post
Share on other sites
Advertisement
Here''s the first thing I came up with. I doubt you''re going to get anything to fit within a single nice looking if statement.
if (x > y)
{
if (x - y > y + 8 - x)
rotate(right);
else
rotate(left);
}
else if (y > x)
{
if (y - x > x + 8 - y)
rotate(left);
else
rotate(right);
}
The idea is that if the old direction (x) is greater than the new direction (y), then typically you would turn left. However, you need to check for the wrap-aroud case, and simply adding 8 to the y makes it equal to the same location, but greater than x. You can then comare the distances between x and y in one direction, and then the other. Whichever direction is shorter, take that one. The same goes for when y > x, but in just the opposite direction.

Share this post


Link to post
Share on other sites
Worked like a charm. Even cleaner code then I wrote(almost a page...) Tnx alot.

[edited by - tgn376j on April 20, 2004 9:45:52 AM]

Share this post


Link to post
Share on other sites
And cleaning up the math a bit (just some basic algebra on the internal conditions):

if (x > y){
if (x - y > 4)
rotate(right);
else
rotate(left);
}
else if (y > x){
if (y - x > 4)
rotate(left);
else
rotate(right);
}


shmoove

Share this post


Link to post
Share on other sites
wouldn''t

if(x - y > 4 || y - x > 4)
rotate(right);
else
rotate(left);

do the same thing with only one if ?

hope that helps !
Matt

Share this post


Link to post
Share on other sites
This is amzing, in just one hour all this great response.

[edited by - tgn376j on April 20, 2004 10:47:02 AM]

Share this post


Link to post
Share on other sites
No. Try y=8 and x=1 for instance. The original rotates left and your examples rotates right.
It can be done in one if with a more complicated condition, but that would have no advantages, and it would make the code uglier.

shmoove

[edit]
this reply is to lemurion of course
[/edit]

[edited by - shmoove on April 20, 2004 10:47:38 AM]

Share this post


Link to post
Share on other sites

  • 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!