Tap ball.. and linearImpulse

Hello everyone,

I have this simple question, yet I haven’t been able to solve it. I have created an object and applied physics to it. Also, when I tap it, I apply the applyLinearImpulse function like the following:

function moveBall (event)  
 ball:applylinearImpulse ( 0, -.2, event.x, event.y)  
end  
  
Runtime:addEventListener("tap", moveBall)  

It all works fine. Now, I am trying to make it so that the ball bounces in the opposite direction of its tapped coordinates. Meaning, if you tap at the bottom of the object ( balloon in this case) it will go up, and if you tap the left side, it will go right…vice versa.

Any help will be highly appreciated.

Thanks.

[import]uid: 75258 topic_id: 26564 reply_id: 326564[/import]

Paulo my brother,

I do not know how to solve it exactly as you need it, but if you have time there and wanna talk something about (exchange ideas) via Skype, am on there now my friend.

Success!

A hug,
Rodrigo. [import]uid: 89165 topic_id: 26564 reply_id: 107719[/import]

Rodrigao brother!!!
Yes, I am going to leave to do something for about 2 hours, but when I get back and you are still online, I will call you on skype. Yes?
Talk to you soon, mermao!!

[import]uid: 75258 topic_id: 26564 reply_id: 107720[/import]

Hi there,
This should be relatively easy. :slight_smile: When the user taps the balloon, you just compare the balloon’s “center” X/Y with the X/Y where the tap occurred. Then you can subtract the second pair of coordinates from the first pair, and apply a linear impulse to push it in the opposite direction.

For example:
touch point is: 50, 150
balloon center (X,Y) is: 100, 100
X difference: 50-100 = -50
Y difference: 150-100 = 50
impulse factors (X,Y) = -50, 50
each multiplied by -1 to “reverse” the direction = 50,-50
each multiplied by 0.001 = 0.05,-0.05 ***
***(to reduce the factor, because applyLinearImpulse() is usually a VERY sensitive API that uses small fractional factors of force).

That should do it! If you don’t want the impulse to be more powerful depending on how far the user taps from the balloon’s center, you could just work out some “ceiling” math to cap the values at a certain threshold, thus it would only use the pure angular relation between the touch and the balloon center.

Brent Sorrentino
Ignis Design
[import]uid: 9747 topic_id: 26564 reply_id: 107721[/import]

Nice one @Ignis!
Thanks for sharing these nice tips. .)

I hope it helps you our Paulo!
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 26564 reply_id: 107728[/import]

Thanks @Ignis!! You are right! It’s so simple now that you have mentioned it! hah! Thank you.
Thank you, @Rodrigo for your awesomeness too!!

Ainda temos que nos falar no skype, meu irmao! Te ja!!

Cheers,

Paulo [import]uid: 75258 topic_id: 26564 reply_id: 107809[/import]

Nice you got it Paulo!

Very kind of @Brent.
PS: Vamos nos falar sim e conversar sobre algum release nosso aí na B&N. :slight_smile:
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 26564 reply_id: 107812[/import]