4
\$\begingroup\$

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);
radians = Math.atan2(0 - pos.y, 540 - pos.x); //set radians
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
radians: -1.1776894171295331, degrees: -67.47663130103584
radians: -2.1756410640936705, degrees: -124.65504723907603
radians: -1.2244578042111383, degrees: -70.15626242087843

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

\$\endgroup\$
  • \$\begingroup\$ Why 540 - pos? Where does this come from? Try 0-pos.x... \$\endgroup\$ – Majte Oct 25 '15 at 16:36
  • \$\begingroup\$ As I said, Im using these values for testing. those values represent the bottom-mid of my screen \$\endgroup\$ – Green_qaue Oct 25 '15 at 16:44
  • \$\begingroup\$ does rotation need to be in non-radians? \$\endgroup\$ – Majte Oct 25 '15 at 17:05
  • \$\begingroup\$ @Majte unfortunately yes, as far as I know. \$\endgroup\$ – Green_qaue Oct 25 '15 at 17:55
  • \$\begingroup\$ 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. \$\endgroup\$ – Majte Oct 25 '15 at 18:20
1
\$\begingroup\$

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.

\$\endgroup\$
-1
\$\begingroup\$

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;
}
\$\endgroup\$
  • \$\begingroup\$ 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); } \$\endgroup\$ – sam hocevar Apr 24 '16 at 17:07

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.