Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [HELP] laser movement  (Read 2844 times)

0 Members and 1 Guest are viewing this topic.

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
[HELP] laser movement
« on: April 18, 2014, 10:23:08 am »
Well as topic already say i need to help with laser movement like on this video:


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]
« Last Edit: April 18, 2014, 10:35:34 am by SHADOW_Dark_HACKER »
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

ChronicRat

  • Sr. Member
  • ****
  • Posts: 327
  • C++ programmer
    • View Profile
    • My blog
Re: [HELP] laser movement
« Reply #1 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]);
 

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: [HELP] laser movement
« Reply #2 on: April 18, 2014, 11:04:20 am »
i need to calculate the direction of laser from those two points
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

ChronicRat

  • Sr. Member
  • ****
  • Posts: 327
  • C++ programmer
    • View Profile
    • My blog
Re: [HELP] laser movement
« Reply #3 on: April 18, 2014, 11:29:15 am »
Well, as i remember it is - normalize(V2 - V1).

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: [HELP] laser movement
« Reply #4 on: April 18, 2014, 11:34:53 am »
thanks  ;) i will write you if it works, but i'll try it later
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: [HELP] laser movement
« Reply #5 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

StDH

  • Jr. Member
  • **
  • Posts: 56
  • {⛺.⛺}
    • View Profile
    • Worst ever ^^
Re: [HELP] laser movement
« Reply #6 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 ?
« Last Edit: April 18, 2014, 12:05:25 pm by SDH »
<StDH> Imagine a girl writing you this: My farts smell good
<Slipxy> married.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [HELP] laser movement
« Reply #7 on: April 18, 2014, 12:33:54 pm »
If you don't want to reinvent the wheel, you can also use Thor.Vectors, 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: