Is there a way to do collision detection without the overhead of the whole physics engine? Also, is there a way to do pixel-perfect collision detection? [import]uid: 10327 topic_id: 3206 reply_id: 303206[/import]
Sure why not- you know the coords of the objects so you an just compare coords, then if they overlap and they are objects that should collide with each other, call some event handler. Of course, you have to implement gravity, friction, etc all by yourself.
I dont know about “pixel-perfect” but you can definitely pass a description of a shape (by its points) into the definition of a physical body.
[import]uid: 9562 topic_id: 3206 reply_id: 9459[/import]
@Hookflash: There is no pixel perfect collision detection in Corona. Collision detection without the the physic engine? Sure, via your LUA code. [import]uid: 5712 topic_id: 3206 reply_id: 9460[/import]
Simple techniques, check if the rectangles (bounding spaces) of your object intersect, in Objective-C you have
CGRectIntersect (rect1, rect2)
You can implement that in Corona by checking the co-ordinates of the rectangles, if they overlap
If your objects are non rectangular and are circular, then Pythagoras (Great man for gamers) theorem is there.
example
local sqrt = math.sqrt
local dx = ball.x - player.x;
local dy = ball.y - player.y;
local distance = sqrt(dx\*dx + dy\*dy);
if distance \< 32 then
--Collided
end
Well, I hope that gives you enough to work with…
cheers,
Jayant C Varma
[import]uid: 3826 topic_id: 3206 reply_id: 9538[/import]
One additional note. If your objects are in different groups, the x/y values will be relative to the group and not the global screen coordinates. You can use object.stageBounds to find the x/y value in the global space.
-Tom [import]uid: 7559 topic_id: 3206 reply_id: 9707[/import]
Hey not sure if anyone is still looking at this thread but I too am trying to get some simple collision detection working without using the physics engine.
I have objects moving down the screen and have a movable object towards the bottom of the screen you move around with the accelerometer. All I want to do is detect when the objects moving down the screen intersect with the object you are moving with the accelerometer. I tried using the code above but I guess I am confused on where to implement it, I am new to Corona and Lua (only been using it for about a week).
Here is the code I have so far:
-- Get rid of the status bar
display.setStatusBar( display.HiddenStatusBar )
-- So the screen stays active
system.setIdleTimer( false )
movable\_object = display.newRect( 200, 0, 50, 50 )
movable\_object:setFillColor( 255, 255, 255 )
movable\_object.x = 240
movable\_object.y = 210
local function movebox()
xcord=math.random(80, 375)
square = display.newRect( 200, 0, 50, 50 )
square:setFillColor( 0x31, 0x5a, 0x18 )
square.x = xcord
square.y = -25
local w,h = display.contentWidth, display.contentHeight
local transitiontime=3800
local delaytime=math.random(300, 3000)
transition.to( square, { time=transitiontime, transition=easing.linear, delay=delaytime, y=(h+50), onComplete=movebox } )
end
xcord=math.random(80, 375)
local square = display.newRect( 200, 0, 50, 50 )
square:setFillColor( 0x31, 0x5a, 0x18 )
square.x = xcord
square.y = -25
local w,h = display.contentWidth, display.contentHeight
local transitiontime=3800
local delaytime=math.random(300, 3000)
transition.to( square, { time=transitiontime, transition=easing.linear, delay=delaytime, y=(h+50), onStart=movebox } )
function onTilt( event )
--moves object
movable\_object.x = (movable\_object.x - event.yGravity \* 40)
--so object doesn't go off screen
if (movable\_object.x \< 25) then
movable\_object.x = 25
end
if (movable\_object.x \> 455) then
movable\_object.x = 455
end
local sqrt = math.sqrt
local dx = movable\_object.x - square.x;
local dy = movable\_object.y - square.y;
local distance = sqrt(dx\*dx + dy\*dy);
if distance \< 32 then
transition.cancel(square)
end
end
system.setAccelerometerInterval( 65 )
Runtime:addEventListener( "accelerometer", onTilt )
Thanks in advance for any help you can give,
-Clark [import]uid: 5786 topic_id: 3206 reply_id: 10641[/import]
Hi Clark,
I have not run your code, but from the top of my head.
- Have a flag that states that your square has collided and hence must stop moving.
You can calling the MoveBox() method and when the transition ends, you are calling it again and it keeps repeating.
Now in the accelerometer method, set a flag, eg.
hasCollided = true
in the moveBox(), have the first line
if hasCollided then return end
and before the function definition of MoveBox, place a line
local hasCollided = false
cheers,
Jayant C Varma [import]uid: 3826 topic_id: 3206 reply_id: 10642[/import]
Thanks Jayant for your quick reply, I will try to implement your suggestions, still trying to figure everything out with Corona.
Just to clarify I am calling the movebox() method over and over to continually create squares at random x coordinates and move them down the screen. For the point of this demonstration the purpose of this game would be to move your object (the white square) to intersect with the green squares that are falling down to gain points and each time you do that the transition ends (and maybe that square disappears?) but the movebox() method continues to create the moving of the squares down the screen so you can intersect with more squares to gain more points, does that make sense at all?
Thanks,
-Clark
[import]uid: 5786 topic_id: 3206 reply_id: 10645[/import]
Ah, OK!!
Sometimes it is not very clear on what someone is trying to do.
so, if I get that right
-
You have squares that keep falling off the top (let’s say) of the screen to the bottom, these are what you refer to as the Green Squares
-
You have a White Square that is controlled by the accelerometer that should intersect, or almost let’s say collect the green squares.
-
When the Green and the White square collide, you want to increase the score and hide the green square or take it off the screen.
In my opinion, here’s how I would approach it,
- Have a onEnterFrame event (the heartbeat of the game)
which will determine if the game is running or over
and will keep spawning the green boxes at a random rate, these will have a transitionTo function that will move them slightly at random speed, and onComplete will run the function CheckForCollision
-
the Accelerometer handling routine (you already have that)
will move the white square -
The CheckForCollision Routine will check that the green square and the white square have collided or not, the movement specified has to be such that the squares should not have passe each other significantly before the onComplete fires. This will if the squares collide, removeSelf and increase the score, otherwise it will move itself again for a small distance at the end of which it will spawn CheckForCollision onComplete.
Does that help/make sense?
You could also have the checkForCollision in the whiteBoxMove, but this in my opinion is more CPU intensive as you will check for every square at every movement, where as the way suggested above, the square that completes the movement is checked against the white square only.
cheers,
Jayant C Varma
[import]uid: 3826 topic_id: 3206 reply_id: 10652[/import]
That makes perfect sense, I didn’t even think of doing it that way but it makes perfect sense, thanks for your help! [import]uid: 5786 topic_id: 3206 reply_id: 10690[/import]
you are welcome
?
cheers,
Jayant C Varma [import]uid: 3826 topic_id: 3206 reply_id: 10710[/import]
Hello,
following the thread about collision without physics… Is there any way to have collision detection with a polyline object? If yes, is there anywhere I could check a sample? I’m new in Corona and have no idea what LUA is.
Thank you [import]uid: 40363 topic_id: 3206 reply_id: 25757[/import]
Don’t know anything about polylines, sorry. For rectangular shapes I wrote this function to do simple collision detection without physics:
http://developer.anscamobile.com/code/flashs-hittestobject-emulated-using-contentbounds
For circular shapes, get the distance between them as jayantv described. [import]uid: 12108 topic_id: 3206 reply_id: 26109[/import]
I was looking for something similar like a month ago, i was looking for a collision event without bouncing or without any physics behaviour by default, after searching a bit, i found the atribute “Sensor” in the physics engine.
A sensor is an object listening for a collision event with other sensor/physicsbody, the object can launch collision events, but wont act as a body, so it wont bounce with other objects, the objects collisioning a sensor won’t bounce either, that way i could made some “coins” in the map, so my main character can pick them =), is a good way to make coins, items, special zones in the map and others.
Finally i ended using some atributes of the physics engine to make my game more realistic, and for all the other objects used sensors.
Hope this helps someone.
http://developer.anscamobile.com/content/game-edition-physics-bodies#Sensors
Greetings
Joe [import]uid: 53575 topic_id: 3206 reply_id: 43919[/import]
When checking for distances with Pythagoras’s theorem, don’t bother to take the square root. It’s a waste of processor. Instead, compare to the squared distance.
For instance, if your circles are 10 pixels in radius, then check for a squared distance of 100 or less. [import]uid: 75168 topic_id: 3206 reply_id: 53476[/import]
Wouldn’t it be nice if a collision only mode was added into the API. I tend to only use the collision functionality of the physics engine. Dont really fancy reinventing the wheel. [import]uid: 118379 topic_id: 3206 reply_id: 93534[/import]
As for the logic of collision detection, lets say if I had a 100 objects on the screen moving around, would I test to see if every objects coordinates was the same as another’s. Seems like some pretty big calculations to iterate over on every frame???
100 x 100 = 10000. That’s alot!!!
Probably will never have that many at one time but still its got to be a hard thing to implement. Would be great if we could do it with API.
[import]uid: 118379 topic_id: 3206 reply_id: 93537[/import]
More complex than collision detection is collision response. While collision detection give you a yes/no answer as to whether two sprites overlap, collision response tells you what to do about it. For example if your sprite walks into a wall you need to push him back out of the wall. If he falls down and hits the floor he needs to stop falling.
This is a major blind spot in corona but a good algorithm is described here
http://www.metanetsoftware.com/technique/tutorialA.html
[import]uid: 84768 topic_id: 3206 reply_id: 93548[/import]
Thx that was a very in depth example, found out what i needed. Essentially line intersection points is all i wanted for my game, can now have pixel perfect placement of explosions/bullet hits. Also handy for lines.
Use physics engine as per norm then when needed test for line intersection. Happy for now. [import]uid: 118379 topic_id: 3206 reply_id: 94389[/import]
@JayantV
the local sqrt = math.sqrt function for collision detection above saved me from a world of headaches…
thanks!!! [import]uid: 124116 topic_id: 3206 reply_id: 123140[/import]