collision detection

how to make collision detection like this picture

22206999.png

Since they are both equal sized squares it’s easy:

[lua]

local deltaX, deltaY

    deltaX = math.abs( obj1.x - obj2.x )

    deltaY = math.abs( obj1.y - obj2.y )

    if( deltaX < obj1.width ) then

        if( deltaY < obj1.height) then

            – Objects are touching / overlapping

        end

    end

[/lua]

If the squares are different sizes, then the code above would need a minor change. If the objects are not squares or have transparency, it starts to get more difficult.

can you explain it I can’t understand the code or can you add a objects in your code

Thanks

obj1 is object1, and obj2 is object2 (the squares).

deltaX and deltaY are the calculated distance of the objects from each other in each dimension (x and y). Absolute value is used because if object2 is to the left of object1, the delta value is negative.

Finally, the first if statement tests to see if the x distance (deltaX) is less than the width of the object. If true, then the next if statement checks to see if the y distance (deltaY)  is less than the height of the object(s). If both are true, then the objects must be touching or overlapping.

Hi @martinlim520,

Are you interested in using physics-based collisions? If so, you can begin with the physics guides here:

http://docs.coronalabs.com/guide/index.html

Regards,

Brent

Or for non-physics:  http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

can I use the physics-based collisions in Tetris style game

You can but it will probably cause more problems than it solves.  In tetris the pieces don’t behave like physics.  They fall at a fixed rate.  Yes, speed goes up as you play.  Gravity increases fall rate, so while your block will start up at one speed, by the time it’s at the bottom it will be going considerable faster.  Objects under gravity accelerate.  Tetris, they do not.   Next physics objects are designed to have their mass (density, size) and elasticity (bounce) impact what happens when two physics objects collide.  Tetris peices do not bounce.  You therefore have to counteract almost everything Physics does to detect if the two pieces have intersected.  

Tetris doesn’t even really need collision detection.  It needs an array of numbers that indicate if a space is empty, or has a particular color piece.  Then on each frame when you get read to move the piece down a block you look to see if where it’s going has room for all the blocks are coming its way.  If those cells in the array are not 0, then you stop moving that block.

Do you know where have the example or article about it .

An article about what?  Four posts up is a blog post on non-physics collision detection.

Since they are both equal sized squares it’s easy:

[lua]

local deltaX, deltaY

    deltaX = math.abs( obj1.x - obj2.x )

    deltaY = math.abs( obj1.y - obj2.y )

    if( deltaX < obj1.width ) then

        if( deltaY < obj1.height) then

            – Objects are touching / overlapping

        end

    end

[/lua]

If the squares are different sizes, then the code above would need a minor change. If the objects are not squares or have transparency, it starts to get more difficult.

can you explain it I can’t understand the code or can you add a objects in your code

Thanks

obj1 is object1, and obj2 is object2 (the squares).

deltaX and deltaY are the calculated distance of the objects from each other in each dimension (x and y). Absolute value is used because if object2 is to the left of object1, the delta value is negative.

Finally, the first if statement tests to see if the x distance (deltaX) is less than the width of the object. If true, then the next if statement checks to see if the y distance (deltaY)  is less than the height of the object(s). If both are true, then the objects must be touching or overlapping.

Hi @martinlim520,

Are you interested in using physics-based collisions? If so, you can begin with the physics guides here:

http://docs.coronalabs.com/guide/index.html

Regards,

Brent

Or for non-physics:  http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

can I use the physics-based collisions in Tetris style game

You can but it will probably cause more problems than it solves.  In tetris the pieces don’t behave like physics.  They fall at a fixed rate.  Yes, speed goes up as you play.  Gravity increases fall rate, so while your block will start up at one speed, by the time it’s at the bottom it will be going considerable faster.  Objects under gravity accelerate.  Tetris, they do not.   Next physics objects are designed to have their mass (density, size) and elasticity (bounce) impact what happens when two physics objects collide.  Tetris peices do not bounce.  You therefore have to counteract almost everything Physics does to detect if the two pieces have intersected.  

Tetris doesn’t even really need collision detection.  It needs an array of numbers that indicate if a space is empty, or has a particular color piece.  Then on each frame when you get read to move the piece down a block you look to see if where it’s going has room for all the blocks are coming its way.  If those cells in the array are not 0, then you stop moving that block.

Do you know where have the example or article about it .

An article about what?  Four posts up is a blog post on non-physics collision detection.