2

I need to rotate a character sprite according to the scope of the platform he (a rectangle) is standing or walking on. I have achieved the effect by rotating it according to the slope of the platform he is standing on. But there are two problems:

First, the rotation is not smooth. When the character walks from a flat platform onto a sloped one, it is rotated instantly and also because of that rotation it is pushed up by a little bit.

Second, once he walks off the slope and onto a flat platform again, I cannot rotate the character accordingly. This is because at that point the character would be colliding with both a flat platform and a sloped one (they are placed together), so for a few steps the character will be rotating crazily back and forth because two collision detection functions working together.

I have tried to solve it by detecting the facing of the character and try to figure out which point (left or right) of him touches something first, but it was not working, because I can only detect collision, but not "no collision" or "when character leaves some platform".

I have attached an illustration of the situation. Please help, thanks!

PS: Since I am not allowed to post images, please visit: https://i.stack.imgur.com/jqPjt.png for the image. Thanks.

dz902
  • 4,782
  • 38
  • 41
  • If you do no special handling at all, you would get the case on the left... no? What happens if you just let the engine do its thing? – iforce2d May 03 '12 at 04:24
  • @iforce2d: I have tried that...but the force I'm applying on it is originated from the center of the rectangle, no from the bottom, so when it hits a slope it will roll forward (instead of pulling backward). Another thing is this game is sort of a platform game, so I don't want the character to be freely rotatable. – dz902 May 03 '12 at 09:37

2 Answers2

2

Your rotation seems off. You need to be rotating around the base of the character rectangle. That would solve some of your problems, but not all.

The case where the character is in contact with 2 platforms needs to be handled. Currently I guess you're just extracting an angle directly from the platform and assigning that to the character. That's not going to cut it because you need arbitrary angles when you have different contact points.

Solution is to create a vector between the 2 contact points, then take the components of the vector x,y and call atan2(x, y). This will give the angle between the contacts in radians. Be sure you get the parameters the right way around! Game development requires you know trigonometry and vector math. What's happening here is we're creating an axis-aligned right-angled triangle with the hypotenuse being defined by the two contact points, then we flip the triangle around and get the angle using tan = opp/adj by swapping the x and y components.

Note that you need to be sure that the contact points are always given in a specific order, or you will end up rotating at a weird angle, because the triangle you create will be facing the other direction. You may need to do something like sort based on x coordinate first.

Tom Whittock
  • 4,081
  • 19
  • 24
1

An easy solution for this example is to do collision-detection on the corners, and not the whole object. You can rotate the object on the point that touch in such an angle that the point that cut only touch. This solution only work if you angle up, and not down. To solve that you also do collision detection on the sides. You then rotate on the touching corner in such an angle that the side just touch.

Gerhard Powell
  • 5,965
  • 5
  • 48
  • 59
  • Sorry cannot mark both as accepted answers (and I'm not yet able to upvote, will do when I get enough reputation). In box2d I actually cannot get exact points of contact by doing collision-detection on the whole object, as you said. I'm trying to put a few sensors at the corners of the object and use them to decide the rotation angle. Thank you. – dz902 May 07 '12 at 03:26