Impulse on Collision

I’ve been having some trouble with this and when I searched the forum everything I could find didn’t work. So with this code, there are objects falling from the sky, and the paddle at the bottom blocks them from reaching the ground. I am trying to make it so that when one of the objects makes contact with the paddle, an impulse is applied to that object to make it fly left or right off the side of the screen. You can see my comments of what I tried. Would it be better to make this as an animation? Anyone who can help me solve this, I will be happy to buy your app in the app store as a thank you :slight_smile:

[code]
display.setStatusBar(display.HiddenStatusBar)

– Load and start physics
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 5)
local gameLayer = display.newGroup()
local bulletsLayer = display.newGroup()
local enemiesLayer = display.newGroup()

local ball
local gameIsActive = true
local scoreText
local sounds
local score = 0
local toRemove = {}
local background
local paddle
local halfpaddleWidth
local timeInterval = 1000

local textureCache = {}
textureCache[1] = display.newImage(“paddle.png”); textureCache[1].isVisible = false;
textureCache[2] = display.newImage(“regBall.png”); textureCache[2].isVisible = false;
local halfballWidth = textureCache[2].contentWidth * .5

gameLayer:insert(bulletsLayer)
gameLayer:insert(enemiesLayer)

local function onCollision(self, event)

if self.name == “paddle” and event.other.name == “ball” and gameIsActive then

–table.insert(toRemove, event.other)
score = score + 10
scoreText.text = score

–I want this force to make the soccer balls fly off the screen at an angle so they
–don’t bounce right back up
–ball:applyLinearImpulse( 500, 2000, ball.x, ball.y )

–gameIsActive = false
end
end
–transition.to( ball, { alpha=0, time=1000, onComplete=onCollision} )
paddle = display.newImage(“paddle.png”)
paddle.x = display.contentCenterX
paddle.y = display.contentHeight - paddle.contentHeight

physics.addBody(paddle, “kinematic”, {bounce = 0})

paddle.name = “paddle”

paddle.collision = onCollision
paddle:addEventListener(“collision”, paddle)

gameLayer:insert(paddle)

halfpaddleWidth = paddle.contentWidth * .5

scoreText = display.newText(score, 0, 0, “HelveticaNeue”, 35)
scoreText:setTextColor(255, 255, 255)
scoreText.x = 100
scoreText.y = 25
gameLayer:insert(scoreText)


– Game loop

local timeLastball = 0

local function gamePlay(event)
if gameIsActive then
– Remove collided ball planes
for i = 1, #toRemove do
toRemove[i].parent:remove(toRemove[i])
toRemove[i] = nil
end

if event.time - timeLastball >= math.random(timeInterval, (timeInterval + 400) ) then
local ball = display.newImage(“regBall.png”)
ball.x = math.random(halfballWidth, display.contentWidth - halfballWidth)
ball.y = -ball.contentHeight

physics.addBody(ball, “dynamic”, {bounce = 0})
ball.name = “ball”

enemiesLayer:insert(ball)
timeLastball = event.time
end

end
end

Runtime:addEventListener(“enterFrame”, gamePlay)


– Basic controls

local function paddleMovement(event)

if not gameIsActive then return false end

if event.x >= halfpaddleWidth and event.x <= display.contentWidth - halfpaddleWidth then

paddle.x = event.x
end
end
paddle:addEventListener(“touch”, paddleMovement)

[/code] [import]uid: 7116 topic_id: 11006 reply_id: 311006[/import]

[lua]ball:applyLinearImpulse( 500, 2000, ball.x, ball.y )[/lua]

ball is not defined in your collision function…

use event.other instead of ball here…

[import]uid: 48521 topic_id: 11006 reply_id: 40055[/import]

Thanks very much! I really should have noticed that. Sometimes I can be stupid… Anyway, what might one of your apps be called so I can go ahead and purchase it? [import]uid: 7116 topic_id: 11006 reply_id: 40057[/import]

Don’t have any app…
May be one day I will have one…

[import]uid: 48521 topic_id: 11006 reply_id: 40058[/import]

Well let me know when you release one or need a favor :slight_smile: [import]uid: 7116 topic_id: 11006 reply_id: 40060[/import]