0
\$\begingroup\$

I'm trying to handle slopes in a tiles based world. I'm using basic linear interpolation to find the Y coordinate of the character based on his X position. But i have a problem, when the character move up or down the slopes the sprite is all jittery, it doesn't move smoothly and i can't figure out why.

I don't use a "true physics engine" all the engine does is AABB collision and tell when a collision occurs and let you handle the reaction. When the character is on a slope no "gravity" (Y velocity) is applied the Y position is adjusted by hand using the linear interpolation result.

Here is the linear interpolation function that i use :

float InterpolateY(Vector2 point1, Vector2 point2, float x)
{
    float x1 = point1.X;
    float y1 = point1.Y;

    float x2 = point2.X;
    float y2 = point2.Y;

    float y = y1 + (x - x1) * (y2 - y1) / (x2 - x1);

    return y;
}

I've made a webm to show you the problem (ignore the fact that the sprite sink into other tile it doesn't matter for the moment)

Link to the webm : https://webmshare.com/play/vPXLD

\$\endgroup\$
  • \$\begingroup\$ Hey mate. I don't know the exact answer, but when I saw your question it reminded me of this tutorial I read a while back. They have a section on implementing slopes which you might find helpful. It doesn't show a lot of code but it does describe in some detail how to implement them correctly. higherorderfun.com/blog/2012/05/20/… \$\endgroup\$ – craftworkgames Apr 27 '18 at 6:52
  • \$\begingroup\$ Judging from the example value of InterpolateY is rounded to nearest integer, is that correct? Can you show of more of the InterpolateYs context (input, handling output etc)? \$\endgroup\$ – wondra Apr 27 '18 at 11:00
  • \$\begingroup\$ @wondra thats exact, i posted an answers to my own questions, thank you for your help :) \$\endgroup\$ – GahrbajKahn Apr 28 '18 at 12:34
0
\$\begingroup\$

I suspect that the engine is doing that. Basically the character is colliding but is getting pushed away from the collider of the ground.
When I used Unity similiar problems occured.
I think its a Physics/Engine Problem

\$\endgroup\$
  • \$\begingroup\$ The engine does nothing, all it does is return true or false if it collide or not \$\endgroup\$ – GahrbajKahn Apr 26 '18 at 14:11
  • \$\begingroup\$ Sorry i haven't read carefully enough. I am not sure about how much you coded yourself. But if you have coded the Input manager... Could it be that you have the polling issue? I mean are you polling the keyboard input and then applying the movement or are you polling and storing and then applying the simulation? Basicly you might have issues from running the simulation/rendering each time after you poll from your keyboard. I remember that i used to have similar issues when not storing the keyboard inputs. That might be a possibility \$\endgroup\$ – RIJIK Apr 26 '18 at 18:33
  • \$\begingroup\$ Another thing i just noticed is: if your green point is supposed to be the origin, it seems to me that for example at around 0:21 in your video the origin seems to move below the collision box and on the slopes the point is always slightly behind the center. Maybe that might have a connection to the problem \$\endgroup\$ – RIJIK Apr 26 '18 at 18:43
0
\$\begingroup\$

I found the problem, it was pretty obvious actually. The position of the character was calculated using a motion vector and the motion.X used in the interpolation was a float number so the result was also a float number (ex : 125.5). The problem happen when i put my character to the Y position calculated (a float) on the screen, the screen coordinate are integers so the Y value get rounded (ex : 125.5 -> 125) thats why i got this jittery effect.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.