#### Archived

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

# 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.

## 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 on other sites
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 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 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 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 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 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

this reply is to lemurion of course
[/edit]

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

##### Share on other sites
Just tried that one and that was the result I got when trying to do the whole thing in one if.

1. 1
2. 2
3. 3
4. 4
frob
11
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632172
• Total Posts
3004569

×