I am creating a game and I have found a lot of tutorials on how to make a object move using accelemter. Now how would I be able to make it collide and recieve points [import]uid: 39840 topic_id: 10618 reply_id: 310618[/import]
Could you decribe what you’re wanting to achieve in more detail? I am not sure if I quite understand. If you are trying to make a game where the player collides with objects to increase their score than you should take a look at the “Simple Pool” example app here:
http://developer.anscamobile.com/content/simple-pool
[import]uid: 27965 topic_id: 10618 reply_id: 38603[/import]
What are are wanting to know is called “Collision Detection”. There are several different ways and if you search the forums for “Collision Detection” you should get a lot of hits that will guide you.
There are two, well three basic models.
-
Using Physics. If you use Corona SDK’s built-in physics you get collisions as events and you just have to set up an event handler to process the events. There are a ton of examples of this.
-
Ignoring Physics. My game OmniBlaster doesn’t use physics, but it uses the Accelerometer to move the player’s ship. If you don’t use physics, there are two basic ways to check for collision:
2a. Check for overlapping rectangles.
local function hasCollided(obj1, obj2)
if obj1 == nil then
return false
end
if obj2 == nil then
return false
end
local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax
return (left or right) and (up or down)
end
But if you’re using images that are more round in shape, that rectangle based check will cause false hits. Since many of my objects in my game are I use a 2nd collision detection method that is based on circular objects:
local function hasCollidedCircle(obj1, obj2)
if obj1 == nil then
return false
end
if obj2 == nil then
return false
end
local sqrt = math.sqrt
local dx = (obj1.x) - (obj2.x + 32);
local dy = (obj1.y) - (obj2.y + 32);
local distance = sqrt(dx\*dx + dy\*dy);
local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2)
if distance \< objectSize then
return true
end
return false
end
Most of the core code above was pulled from forum posts here! Credit to their original poster.
Rob [import]uid: 19626 topic_id: 10618 reply_id: 38612[/import]