Im trying to build an isometric golf game. Im working on getting the bounce looking right… I have the movement of the bounce figured out to some extent… Im not sure how to “hit” the ball though and implement the bounce… The following code will make the ball bounce as you drag your finger on the screen… But how would I do this without the use of the touch listener… I tried using transition.to, translate and apply force to move the ball and have it bounce… but cant get it working…
[lua]
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 0)
–physics.setDrawMode( “hybrid” )
local ballShadow = display.newCircle(0, 0, 15)
local ball = display.newCircle( 100, 300, 35)
ball.x = display.contentCenterX - 500
ball.y = display.contentCenterY
ball.xScale = .75
ball.yScale = .75
physics.addBody(ball, “dynamic”, {denisty = .01, radius = 8, friction = 0})
ball.linearDamping = 1.1
ball.angularDamping = 1.1
ballShadow.x = ball.x
ballShadow.y = ball.y+25
ballShadow:setFillColor(0,0,0)
physics.addBody(ballShadow, “dynamic”, {denisty = 1, radius = 8, friction = 0})
ball.linearDamping = 1.1
ball.angularDamping = 1.1
dz = 1;
gravity=2
zPos = 0
local function touchCoor(event)
if event.phase == “began” then
xPos = event.x
yPos = event.y
zPos = 0
gravity = 2
dz = 30 - gravity
print(xPos)
print(yPos)
print(zPos)
x0 = event.x - ball.x
y0 = event.y - ball.y
end
if event.phase == “moved” then
print(“moved”)
dz = dz - gravity
zPos = zPos + dz
ball.x = (event.x - x0)
ball.y = (event.y - y0) - zPos
ballShadow.x = ball.x
ballShadow.y = event.y - y0 + 25
print(zPos)
if zPos <=0 then
zPos = 0
dz = dz * -0.9
if math.abs(dz) < 0.2 then
dz = 0
– stop further bounce
end
end
end
end
Runtime:addEventListener(“touch”, touchCoor)
function shoot(e)
if(e.phase == ‘began’) then
if markerVectorTimer then timer.cancel(markerVectorTimer) end
display.remove(guide) – Clears the previous line
display.remove(marker)
markerVectorTimer = timer.performWithDelay(250, function() markVector(e.x, e.y) end, 1000)
elseif(e.phase == ‘moved’) then
if markerVectorTimer then timer.cancel(markerVectorTimer) end
markerVectorTimer = timer.performWithDelay(250, function() markVector(e.x, e.y) end, 1000)
display.remove(marker)
–markVector(e.x,e.y)
elseif(e.phase == ‘ended’) then
if markerVectorTimer then timer.cancel(markerVectorTimer) end
display.remove(guide)
display.remove(marker)
– ball:applyForce((ball.x - e.x) * 0.01, (ball.y - e.y) * 0.005, ball.x, ball.y)
–transition.to(ball,{x=600, y=600, time = 10000})
end
end
–Runtime:addEventListener(“touch”, shoot)
[/lua]