1
\$\begingroup\$

I have a line perpetually pointed at the mouse and originating at the player, but the length is limited to a certain value.

 line and player

I also can create a circular object on demand in Box2D (specifically the love2d bindings). I would like to apply an impulse on the new object that will send it flying in the direction of the line, with force proportional to the length of the line (that specific proportion is not important). The object should not roll along that line like a cannonball in a cannon barrel, but it should begin its journey in the direction of the line, such that a user can intuitively direct the projectile (all my attempts* at solving this issue have led to some angles (45/135) having similar projectile directions as flat angles (0/180), which means the angle is not functional as an aiming mechanism). I suppose a successful implementation might look like this: arcs along line

I have been looking through tutorials and haven't found an answer. I see whisperings of ways to do it by simply supplying the angle (if I read the pages correctly) but Love2D's function does not support that.

The way in Love2D to apply a linear impulse is Body:applyLinearImpulse( ix, iy, x, y ) with ix and iy being the x and y components of the impulse, and x and y being the position to apply the impulse (default to origin of body).


*The best I have tried is to apply the distance of the line as the x/y components in applyLinearImpulse. This creates the awful failure I mentioned earlier where the angle of the line does not produce much difference in the angle of the projectile (but it does produce a tiny amount, with extremely diminishing returns as the line approaches a 90 degree angle / vertical line).

\$\endgroup\$
1
  • \$\begingroup\$ This is a well made and well answered post - thank you \$\endgroup\$
    – Natalo77
    Apr 11, 2020 at 23:26

1 Answer 1

1
\$\begingroup\$

The user bageltan on the love2d discord has supplied me with the answer: (line.x and line.y are the x and y positions of the end of the line and in my case player.x/player.y are the start of the line and where the projectiles spawn.)

ix = line.x - player.x

iy = line.y - player.y

You can multiply the sum of these by any number you choose to increase/decrease the force of the throw within the proportion. Here is an example showing it works, with the equations multiplied by 0.5.

Example of successful implementation

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .