How to throw stuff in direction of swipe?

Hi,

I’m making a prototype that you swipe and according to the direction and length of the swipe, it would throw some stuff, like how you do with Angry Birds but in that game you draw a sling and it will shoot in the direction of the sling being pulled back but in this game image the opposit and if you touch and swipe on a direction, it would shoot something.

I managed to get it working for most angles but it’s not OK yet, here’s what I’ve done so far:

 foodObject:applyLinearImpulse( event.x - event.xStart, event.y - event.yStart, foodObject.x, foodObject.y )

but it has two problems:

  1. it doesn’t work for downward angles

  2. It doesnt take account of the lenght of the swipe.

Thanks.

To take into account the length of the swipe, you can multiply the force you’re applying by the distance between the “swipe start” and “swipe end” (i.e. the magnitude of the 2D vector representing the swipe.)  You might also need to multiply by another constant factor to scale the force to what you think “feels right.”  (Think of the linear impulse you’re applying as a “force vector” and then you are just multiplying by scalar(s), which is how you scale vectors.)

As for the negative angle case, the easiest way to solve stuff like this is to take out some actual pen and paper and draw a 2D graph and do a “test case” in each of the 4 quadrants using the formula you’ve coded and see what answer you get.  This will usually help you “understand the problem” and figure out how you need to tweak each case.  (Sometimes you can come up with a general formula for all 4 cases, sometimes you need a separate positive and negative case, and sometimes you need a separate case for all 4 quadrants.  It just depends on exactly what problem you are solving!)

Also, remember your trig and SOHCAHTOA- sometimes it comes in handy with stuff like this.  (Although be careful since trig functions are computationally expensive, so make sure to avoid them in critical loops unless needed.)

To take into account the length of the swipe, you can multiply the force you’re applying by the distance between the “swipe start” and “swipe end” (i.e. the magnitude of the 2D vector representing the swipe.)  You might also need to multiply by another constant factor to scale the force to what you think “feels right.”  (Think of the linear impulse you’re applying as a “force vector” and then you are just multiplying by scalar(s), which is how you scale vectors.)

As for the negative angle case, the easiest way to solve stuff like this is to take out some actual pen and paper and draw a 2D graph and do a “test case” in each of the 4 quadrants using the formula you’ve coded and see what answer you get.  This will usually help you “understand the problem” and figure out how you need to tweak each case.  (Sometimes you can come up with a general formula for all 4 cases, sometimes you need a separate positive and negative case, and sometimes you need a separate case for all 4 quadrants.  It just depends on exactly what problem you are solving!)

Also, remember your trig and SOHCAHTOA- sometimes it comes in handy with stuff like this.  (Although be careful since trig functions are computationally expensive, so make sure to avoid them in critical loops unless needed.)