Rotate sprite along the angle between two objects

Im trying to make flames follow my object, I set the direction for my moving object using the points between it and another object (using static destination for testing).

No matter what I do I cannot use the radians (which I use to set the direction), translate them to degrees, then rotate my flames so it looks like they are following it (like a comet).

What I try:

sprite.setPosition(pos.x, pos.y);
degrees = radians * 180 / MathUtils.PI; //calculate degrees
fireSprite.setPosition(sprite.getX(), sprite.getY());
fireSprite.setRotation((float)degrees); //rotate according to degrees


I move the object like this:

pos.x += 200 * Math.cos(radians) * delta;
pos.y += 200 * Math.sin(radians) * delta;


Am I missing something in the math? I feel like it should work.

An example of result from calculations:

radians: -1.8745297149164084, degrees: -107.4026382478209


The flames face the wrong direction, and not in a any sort of pattern I can use to debug my problem.

• Why 540 - pos? Where does this come from? Try 0-pos.x... – Majte Oct 25 '15 at 16:36
• As I said, Im using these values for testing. those values represent the bottom-mid of my screen – Green_qaue Oct 25 '15 at 16:44
• does rotation need to be in non-radians? – Majte Oct 25 '15 at 17:05
• @Majte unfortunately yes, as far as I know. – Green_qaue Oct 25 '15 at 17:55
• you are calculating the angle between the bottom mid and the position of the object. How is this supposed to follow your object like a comet trait? You need to set the rotation to be the atan2 of the velocity vector of your moving object. – Majte Oct 25 '15 at 18:20

You can try taking the vector from the ship (or whatever you have) to the target position, then get the angle of that. libGDX's Vector2 class has most of the methods you need.

Something like this might work for you;

// Get these from where ever you have them
Vector2 shipPosition = new Vector2(100, 100);
Vector2 targetPosition = new Vector2(0, 540);

// Calculate the vector from the ship to the target
Vector2 fromShipToTarget = new Vector2(targetPosition).sub(shipPosition);

// Get the angle of the ship-to-target
float angle = fromShipToTarget.angle();

// Set the origin so that the sprite is rotated around its center, not its corner
fireSprite.setOrigin(fireSprite.getWidth() / 2.0f, fireSprite.getHeight() / 2.0f);
fireSprite.setPosition(shipPosition.x, shipPosition.y);
fireSprite.setRotation(angle);


The angle method on Vector2 will give you the angle of the vector towards the positive y-axis so if you have two points (0, 0) and (1, 1) the above example will yield a vector of 45 degrees.

Here is the code I use to get the angle of a 2d vector, hopefully it is what your are looking for. (using our own library functions, most of which should be obvious)

float zVec2f::getAngle () const
{
//straight up, 0.0f degrees
zVec2f up(0.0f, -1.0f);

//angle between up vector and our vector
zVec2f normal = *this;
if ( normal.normaliseSafe() ) // returns true if it was possible to normalize vector
{
float angle = acosf(zDot(normal,up));

//compensate for the fact that acos only
//gives us 0->Pi (mirror along 180 degree line)
if ( x < 0.0f)
{
angle += (2.0f * (zPIf - angle));
}
zASSERT(angle>=0.0f);
zASSERT(angle<=zPI2f);

return angle;
}
return 0.0f;
}

• This function reinvents the wheel and is really too long. I can’t recommend anyone uses it. It can be rewritten in one single line: float zVec2f::getAngle () const { return atan2f(this->x, - this->y); } – sam hocevar Apr 24 '16 at 17:07