SFML community forums

Help => General => Topic started by: StDH on April 18, 2014, 10:23:08 am

Title: [HELP] laser movement
Post by: StDH on April 18, 2014, 10:23:08 am
Well as topic already say i need to help with laser movement like on this video:
https://www.youtube.com/watch?v=haBWQQ8D-Gw (https://www.youtube.com/watch?v=haBWQQ8D-Gw)

as you cant see player and npc are moving with some angle and laser must move with them at some angle and speed, or otherwise it will be rocket.

and that: (me[angle] + npc[angle]) / 2 = laser[angle] -> i dont know if it is right, tell me if am i doing it wrong
i need to calculate angle + speed of laser [using sf::Sprite]
Title: Re: [HELP] laser movement
Post by: ChronicRat on April 18, 2014, 10:49:30 am
Maybe I misunderstood, but you need to get direction? From sf::Transformable you can get direction via matrix:
    this = something sf::Transformable.
        const float* m = this->getTransform().getMatrix();
        return sf::Vector2f(m[0], m[1]);
 
Title: Re: [HELP] laser movement
Post by: StDH on April 18, 2014, 11:04:20 am
i need to calculate the direction of laser from those two points
Title: Re: [HELP] laser movement
Post by: ChronicRat on April 18, 2014, 11:29:15 am
Well, as i remember it is - normalize(V2 - V1).
Title: Re: [HELP] laser movement
Post by: StDH on April 18, 2014, 11:34:53 am
thanks  ;) i will write you if it works, but i'll try it later
Title: Re: [HELP] laser movement
Post by: Grimshaw on April 18, 2014, 11:35:19 am
ChronicRat is right, normalize(V2 - V1) will give you the direction vector from V1 to V2.

If you want that in an angle, here's how you do it (radian): atan2(direction.y, direction.x), you can then convert to degrees if needed. :p
Title: Re: [HELP] laser movement
Post by: StDH on April 18, 2014, 11:44:42 am
ChronicRat is right, normalize(V2 - V1) will give you the direction vector from V1 to V2.

If you want that in an angle, here's how you do it (radian): atan2(direction.y, direction.x), you can then convert to degrees if needed. :p

it looks like one of my function in npc[movable struct]:

#define toDegrees 57.32f        //(180 / 3.14)

float gameEntityMove::coordsToAngle(sf::Vector2f start, sf::Vector2f end)
{
        #pragma warning(disable : 4715) // not all control paths return a value

        if (start.x != end.x && start.y != end.y)
                return std::atan2(start.y - end.y, start.x - end.x) * toDegrees;
        else
                return -1;
}

am i right ?
Title: Re: [HELP] laser movement
Post by: Nexus on April 18, 2014, 12:33:54 pm
If you don't want to reinvent the wheel, you can also use Thor.Vectors (http://www.bromeon.ch/libraries/thor/v2.0/doc/_vector_algebra2_d_8hpp.html), they provide already everything: angles, distances, normalization, ...

Computing the direction is then as simple as:
sf::Vector direction = thor::unitVector(b - a);

You usually don't want to work with angles unless you really need them, because the special cases (wraparound) are a bit annoying to handle.