Hey Guys.
Simple question here.
For my game, I will need to detect collisions, but not have them collide.
Do I need to require physics for this?
Thanks! [import]uid: 50842 topic_id: 14596 reply_id: 314596[/import]
Hey Guys.
Simple question here.
For my game, I will need to detect collisions, but not have them collide.
Do I need to require physics for this?
Thanks! [import]uid: 50842 topic_id: 14596 reply_id: 314596[/import]
I am almost certain that you do, but i too would like to know for sure, maybe someone from ansca will chime in. [import]uid: 19620 topic_id: 14596 reply_id: 54009[/import]
You can write your own function to detect collisions.
You basically setup a function that gets called 30 times per second. Inside this function you have to check the x and y coordinates of all the objects on the screen and decide if they are close enough together to be colliding.
See this thread for suggestions on how to code this.
http://developer.anscamobile.com/forum/2010/10/29/collision-detection-without-physics [import]uid: 67839 topic_id: 14596 reply_id: 54013[/import]
Whilst it is possible to code your own collisions without physics, personally I would always use physics where possible. You would need to require physics and use the isSensor property to detect collisions but not effect the objects.
[import]uid: 67933 topic_id: 14596 reply_id: 54022[/import]
You can of course program your own collision detection method without using physics. However as mentioned above using the “isSensor” property as mentioned above would get you the exact results you are after. [import]uid: 84637 topic_id: 14596 reply_id: 54035[/import]
I use the collision function below if I don’t need physics in my project.
Very fast, but not good if you need precision collision, which 80%+ of the time you don’t.
Example of collision detection without requiring physics.
[code]
– Ball object
local ball = display.newImageRect(“ballSmall.png”, 12, 12)
ball.x = 240; ball.y = 160
ballSizeX = 12; ballSizeY = 12
– Paddle object
local leftPaddle = display.newImageRect(“paddleSmall.png”, 12, 60)
leftPaddle.x = 35; leftPaddle.y = 160
paddleSizeX = 12; paddleSizeY = 60
– Collision detection
local function collision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)
if (box1x > (box2x + box2w)) then return false
elseif ((box1x + box1w) < box2x) then return false
elseif (box1y > (box2y + box2h)) then return false
elseif ((box1y + box1h) < box2y) then return false
else return true
end
end
– Check if ball hit paddle
if collision(ball.x, ball.y, ballSizeX, ballSizeY, leftPaddle.x, leftPaddle.y, paddleSizeX, paddleSizeY) then
– if ball hit paddle then do something
end
[/code] [import]uid: 53445 topic_id: 14596 reply_id: 54052[/import]
@millerszone : Or … why not make life easier (Modified version of your example)
[code]
– Ball object
local ball = display.newImageRect(“ballSmall.png”, 12, 12)
ball.x = 240; ball.y = 160
– Paddle object
local leftPaddle = display.newImageRect(“paddleSmall.png”, 12, 60)
leftPaddle.x = 35; leftPaddle.y = 160
– Collision detection
local function collision(obj1, obj2)
if (obj1.x > (obj2.x + obj2.width)) then return false
elseif ((obj1.x + obj1.width) < obj2.x) then return false
elseif (obj1.y > (obj2.y + obj2.height)) then return false
elseif ((obj1.y + obj1.height) < obj2.y) then return false
else return true
end
end
– Check if ball hit paddle
if collision(ball, leftPaddle) then
– if ball hit paddle then do something
end
[/code] [import]uid: 84637 topic_id: 14596 reply_id: 54058[/import]