Detecting Collision - Left Side, Right Side of object.

I have an object that i want to transition left to right and back again ( this i can do )

if it is detected that it hits the screen edges i wish to change the direction of the object to go the other x direction.

I have a postCollision event but - should i do this in the onCollision event and pass the self parm?

but I would like to know how to detect the following details:

#1 what object I hit ( maybe somehting was dropping - or my player ran into the moving object )
#2 did I hit a wall, if so which wall, the left wall or the right wall.

And on which side of the object did the hit occur?

thanks in advance Larry

[import]uid: 11860 topic_id: 4459 reply_id: 304459[/import]

This is all off the top of my head, but its just to show the idea…

Checking collisions with the screen edges is just checking the position of the object.
Checking collisions with other objects is just a collision event.
[lua]function createMyObject( displayObj, objShape )

physics.addBody( displayObj, “static”, { friction=.1, bounce=0, density=.1, shape=objShape } )

displayObj.xdirection = 10

function displayObj:EnterFrame( event )
if (myObject.x <= 0) then xdirection = 2 end
if (myObject.x >= display.contentWidth) then xdirection = -2 end

myObject.x = myObject.x + xdirection
end

function displayObj:collision( event )
if (event.phase == “began”) then
if (event.other.x < displayObj.x) then
– object collided with was on left on this object
elseif (event.other.x > displayObj.x) then
– object collided with was on right of this object
end
end
end

displayObj:addEventListener( “collision”, displayObj )
displayObj:addEventListener( “enterFrame”, displayObj ) – this could also be done using a timer[/lua]
[import]uid: 8271 topic_id: 4459 reply_id: 14015[/import]

ok i’ll give it a try.

I like your use of xdirection - good idea, i was doing it the long way with an extra variable, i keep forgetting that LUA is dynamic on adding properties.

A last question if you have time for one ( ok well maybe more )

I am also trying to figure out how to tell if possible which side the collision was hit on ie… left, right, top, bottom

object 1 being static - object 2 hitting / collision into object 1 on one of it’s sides or top or bottom.

Thanks in advance

Larry [import]uid: 11860 topic_id: 4459 reply_id: 14047[/import]

If you look at the comments in the collision function you’ll see that’s already worked out.

Matt [import]uid: 8271 topic_id: 4459 reply_id: 14055[/import]

Sorry, i had a brain fart, and eyes were watering from the gas so i did not see those comments.

LOL thanks a bunch

Larry [import]uid: 11860 topic_id: 4459 reply_id: 14057[/import]