1
\$\begingroup\$

I use an angle to set the velocity (or direction if you prefer) of my sprite, however, I want the sprite to be heading in the same angle as I'm moving, rotating according to that angle.

How I set angle and velocity:

angle = (float)Math.atan2(0 - pos.y, 220 - pos.x); //using bottom mid of screen as destination for testing

//in update
pos.x += 200 * Math.cos(angle) * delta;
pos.y += 200 * Math.sin(angle) * delta;
sprite.setPosition(pos.x, pos.y);

//in render
sprite.draw(batch);

is there any way to use this value in LibGdx's "sprite.rotate()" function? I can't do "sprite.rotate(angle)". Can I do this some other way? Can I convert my angle-value somehow to degrees? I'm stuck.

I tried

sprite.rotate((float)Math.toDegrees(angle));

but that gave rly weird results, the following image is moving down and to the right

img

\$\endgroup\$
  • \$\begingroup\$ "rly wierd results" how? That says nothing about the result you got. \$\endgroup\$ – ratchet freak Oct 10 '15 at 0:41
  • \$\begingroup\$ Hmm, hard to explain, Ill post an image \$\endgroup\$ – Green_qaue Oct 10 '15 at 0:41
  • \$\begingroup\$ Is it the same as in stackoverflow.com/q/27345132/731620 ? \$\endgroup\$ – ratchet freak Oct 10 '15 at 0:45
  • \$\begingroup\$ I tried using sprite.setRotation((MathUtils.radiansToDegrees * angle)); but getting wrong result still. I dont actually rotate it in update, since it never changes direction I set the angle in the constructor, after the position has been set. \$\endgroup\$ – Green_qaue Oct 10 '15 at 0:51
  • \$\begingroup\$ @ratchetfreak added an image \$\endgroup\$ – Green_qaue Oct 10 '15 at 0:53
1
\$\begingroup\$

rotate takes I believe a relative rotation while angle is an absolute rotation. In other words take the difference between the current angle and the wanted angle and pass that to rotate.

Or pass just the angle into setRotation.

\$\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.