bounce in response to touch? (tap left bounce to right)

i have a ball. i have it so when you tap it it bounces. its ALL good. nice spinning and nice damping :)

HOWEVER i can't make it bounce horizontally.. only up and down.
i've seen the balloon video on youtube 6 times already but its not working for me.

basically i just want the ball to bounce in the opposite direction of where i'm touching it.

i'm sure theres a way of doing this :p

and BTW i don't mean just up down left right. i want it to bounce like it would in the real world. up down ;eft right upper left, lower right, almost in the lower right, etc etc etc. basically real physics. the video seems to have done it not sure why i can't replicate it....

here is my code...

local function onTap(event)
if "began" == event.phase then
beachball:applyLinearImpulse( 0 , -0.2, event.x, event.y )
elseif "ended" == event.phase then
end
end
beachball:addEventListener("touch", onTap)

i have it "onTap" because i don't want infinite bounces, only on TAP (began)

can some one help me out? thanks!

try different value for x and y in
beachball:applyLinearImpulse( x , y, event.x, event.y )

i did but it responds to what i write there. i want the ball to bounce to the opposite on my "Tap". like in real life :/

u tap a ball on the bottom it bounces up. tap it on the right it goes left....

try this

1
2
3
4
5
6
7
8
9
10
11
function onTouch(event)
        if event.phase == "began" then
        --We modify the y-coordinate of the event, so it is more under the ball
        event.y = event.y+20
        --Now apply some physic force to the ball reagrding the relative position
        --of the touch coordinates and the ball coordinates.
        beachball:applyForce( (beachball.x - event.x)*20, (beachball.y - event.y)*20, beachball.x, beachball.y )
        end
end
 
beachball:addEventListener("touch",onTouch)

oh man thank you Sooooooo much!!!!!!! this made my day. it worked! i just had to edit the force. it was TOO much lol ball was going everywhere!

a THOUSAND thank yous!

you know, i'm starting to understand lua more and more :) thanks again!

EDIT: hmm... it doesn't rotate anymore. it just bounces but it doesn't spins. before it would spin and slowly slow down (through angular damping)

EDIT 2: ok i fixed that with adding angularimpulse but it only spins to the right. (clockwise) no matter where i tap it. my first code would make the ball spin to its direction.

views:1259 update:2011/10/15 21:01:16
corona forums © 2003-2011