Best way to implement a harpoon / spear / arrow?

Hi all,

I am planning to add a harpoon /grappling hook to my current game.  My first step is to try and get the harpoon to fly correctly.  I have google around and found two options so far.

1. https://quangnle.wordpress.com/2012/12/18/corona-sdk-an-implementation-of-bow-shooting-without-physic-engine/

2. https://gist.github.com/HoraceBury/9544932

Both options work fine, but neither is perfect.  Option 1 looks a bit weird and option 2 works OK but seems extremely complicated.  Are there any other options people can recommend, or should I just go with option 2?

Thanks,

Craig

@Craig,

Sorry boss.  You’re going to have to use physics and/or do 2D math (as Horacebury is in #2).

In fact, you should just consign yourself to learning algebra, trig, rudimentary 2D math, and basic physics principles if you want to make 2D action games.  

Note: I looked at HoraceBury’s code and that is pretty tame actually (he uses helpers so you don’t even need to understand the longhand trigonometry).  I’m not saying this to shame you or any such thing.  I simply want to let you (and anyone else reading this) know that, unless you use a ‘do it all for me’ game making solution you’re going to need to learn this stuff.

A little more feedback to help breakdown the parts of making an arrow, spear, etc.  (There are many ways to do this)

To ‘fire’ the projectile, you need

  • <x,y> starting position
  • angle the projectile is initially facing
  • magnitude of initial velocity

given the above, you would:

  • create a display object using an image is facing up to represent 0-degrees (i.e. arrow pointing up == 0 degree)
  • add a dynamic physics body to the display object
  • rotate the display object to the ‘angle’ you selected.
  • convert the angle (using 2D math) into a unit-length vector. i.e. represent the angle as a vector.
  • scale the vector from the last step by the ‘magnitude’
  • set the linear Velocity (obj:setLinearVelocit( vx, vy )) where vx and vy are the <x,y> values from the vector you just scaled.

At this point, the ‘arrow’ will start moving and will fly in an arc.  The only part that is missing is, the arrow will NOT rotate to follow the current ‘arc’.  You’ll need to add an enterFrame listener to the ‘arrow’ display object that will rotate the arrow based on the objects current velocity.  This bit is tricky unless you’re familiar w/ 2D math.  Horace is doing something like this in his code, but his enterFrame listener handles all of the movement too.

I hope this helps a little. 

PS - Horace’s example is superior to my suggestion in the fact that he simulates ‘feather drag’ which really gives the arrow movement a nice ‘organic’ feel.

Awesome…  I’ll see what I can understand!!  I guess it was me being slightly lazy,  I was hoping for an easy option :)…

Thanks again,

Craig

I think that at one time or another we all look through the docs hoping that Corona have added a function for our specific needs:

Ah, here it is:

display.removeAllMemoryLeaksAndOptimiseAllOfMyCode(true)

@Craig,

Sorry boss.  You’re going to have to use physics and/or do 2D math (as Horacebury is in #2).

In fact, you should just consign yourself to learning algebra, trig, rudimentary 2D math, and basic physics principles if you want to make 2D action games.  

Note: I looked at HoraceBury’s code and that is pretty tame actually (he uses helpers so you don’t even need to understand the longhand trigonometry).  I’m not saying this to shame you or any such thing.  I simply want to let you (and anyone else reading this) know that, unless you use a ‘do it all for me’ game making solution you’re going to need to learn this stuff.

A little more feedback to help breakdown the parts of making an arrow, spear, etc.  (There are many ways to do this)

To ‘fire’ the projectile, you need

  • <x,y> starting position
  • angle the projectile is initially facing
  • magnitude of initial velocity

given the above, you would:

  • create a display object using an image is facing up to represent 0-degrees (i.e. arrow pointing up == 0 degree)
  • add a dynamic physics body to the display object
  • rotate the display object to the ‘angle’ you selected.
  • convert the angle (using 2D math) into a unit-length vector. i.e. represent the angle as a vector.
  • scale the vector from the last step by the ‘magnitude’
  • set the linear Velocity (obj:setLinearVelocit( vx, vy )) where vx and vy are the <x,y> values from the vector you just scaled.

At this point, the ‘arrow’ will start moving and will fly in an arc.  The only part that is missing is, the arrow will NOT rotate to follow the current ‘arc’.  You’ll need to add an enterFrame listener to the ‘arrow’ display object that will rotate the arrow based on the objects current velocity.  This bit is tricky unless you’re familiar w/ 2D math.  Horace is doing something like this in his code, but his enterFrame listener handles all of the movement too.

I hope this helps a little. 

PS - Horace’s example is superior to my suggestion in the fact that he simulates ‘feather drag’ which really gives the arrow movement a nice ‘organic’ feel.

Awesome…  I’ll see what I can understand!!  I guess it was me being slightly lazy,  I was hoping for an easy option :)…

Thanks again,

Craig

I think that at one time or another we all look through the docs hoping that Corona have added a function for our specific needs:

Ah, here it is:

display.removeAllMemoryLeaksAndOptimiseAllOfMyCode(true)