Jump to content
  • Advertisement
Sign in to follow this  
Dustin Booher

Turning a Sprite based on its velocity (2D)

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

okay so ive been stuck on this for quite some time now, my goal is to take an object which has its position update with a velocity (a vector 2 that is added to its position vector2). and to rotate this object (over time) until it holds the same place as its goal. as illustrated below.

[attachment=6634:vel.png]

hopefully you understand what i mean here. I don't want the velocity to rotate to face the object immediately. I need to gather the required angle to rotate to split that up over time. and to guarantee the regardless of where the object starts, where the destination is, or what its velocity is, that
it will at one point or another intersect the object.

ive tried this countless times and countless ways in my main project but all methods have failed. usually what happens is the object continually rotates and rotates in wider and wider circles. so i made a new project to work on just this where i have 2 blocks (each 16x16) one to illustate the goal. and the other moves with a velocity of (1,0) so dead right. I dont understand how to obtain the rotation angle to create the matrix to accomplish this. Please help :(

Share this post


Link to post
Share on other sites
Advertisement
Hi!

So you basically want to move between waypoints and like to pass each waypoint from a certain direction, right? Something like starting at p1 with velocity d1 and then end up in p2 with velocity d2.

Okay, a common approach is to use a spline interpolation. A spline is a sequence of curves which are stuck together. In your case it is just one curve. But perhaps, you want your movement to continue at p2 towards another point p3 and still want everything to be smooth. So, for this reason I give you a solution that can manage this as well. smile.png

For animations you want three things from the final curve:

  • You want to visit each waypoint (this is called interpolation property).
  • You want the tangent to change smoothly (this is called C1 continuity), so no sharp corners.
  • You want the speed when you’re passing a waypoint to be continuous (this is called C2 continuity), so not abruptly accelerating or decelerating.

    Usually you are using cubic splines, since they have low polynomial degree. This prevents numerical issues and the curve will “behave”. Curves of high degree tend to make some weird detours.

    If you want to compute cubic interpolating C2 continuous splines you have to solve a linear system of equations (and you need some boundary conditions). Sure, you could do this for your animation, but perhaps it is too much effort to put in.
    So, instead you often give up the last of the three aforementioned constraints.

    Very common is the usage of cubic Hermite splines. Those are cubic polynomials, too, but they only provide you C1 continuity. If you distribute your waypoints well enough, you won’t get any problems. For a Hermite interpolation, you need two waypoints and for each waypoint a direction vector telling you from which direction you like to pass through it. XNA has a helper for that Vector3.Hermite(). Code is here (about line 659) if you are interested in it.

    So, this gives you the position at a certain time t in [0,1]. The cubic polynomial behind this is:
    [Formula]\mathbf{p}(t)=(2t^3-3t^2+1)\mathbf{p_1}+(t^3-2t^2+t)\mathbf{d_1}+(-2t^3+3t^2)\mathbf{p_2}+(t^3-t^2)\mathbf{d_2}[/Formula]

    For the velocity we take the derivative.
    [Formula]\mathbf{\dot{p}}(t)=(6t^2-6t)\mathbf{p_1}+(3t^2-4t+1)\mathbf{d_1}+(-6t^2+6t)\mathbf{p_2}+(3t^2-2t)\mathbf{d_2}[/Formula]

    From the interpolated velocity you can fairly easy compute the angle for your rotation matrix.
    [Formula]\alpha=atan2(v_y,v_x)[/Formula]

    That should do the trick. smile.png
    Hope that helps!

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!