Collision of two objects

Hello everyone, I have a village in my game. I want the player to be able to move around the village without pushing the houses and people around when bumps into them. Here is some code for how I created an elder and his house and the player. As it is right now. The player completely passes through the elder. When the player bumps into the house it is detected but the player can move the house around by moving into it. I don’t know if it has anything to do with it but the player is an animated sprite. If more code is needed please let me know.

[code]
local elderHouse = display.newImage(“gumm_house_smooth.png”)
elderHouse.name = “elderHouse”
elderHouse:scale(0.45, 0.45)
elderHouse:setReferencePoint(display.CenterReferencePoint)
elderHouse.x = display.contentWidth * 0.5 + 25
elderHouse.y = display.contentHeight * 0.25 - 55
physics.addBody(elderHouse, “dynamic”, {density=1.0, friction=0.5, bounce=0.0, radius= 15})

local elder = display.newImage(“gumm_elder_40X40_2.png”)
elder.name = “elder”
elder:scale(0.35, 0.35)
elder:setReferencePoint(display.CenterReferencePoint)
elder.x = display.contentWidth * 0.5 - 35
elder.y = display.contentHeight * 0.25 - 25
physics.addBody(elder, “static”, {density=0.0, friction=0.0, bounce=0.0, radius= 15})
elder.isSensor = false

local function spawnPlayer( event )

gummMoving = newGummMovingSprite()
gummMoving.name = “gummMoving”

physics.addBody(gummMoving, “dynamic”, {density=1.0, friction=0.5, bounce=0.0, radius= 10})
gummMoving.isSensor = false

gummMoving.collision = onCollision
gummMoving:addEventListener(“collision”, gummMoving)
Runtime:addEventListener(“touch”, movePlayer)
gummMoving:play()
end [import]uid: 49863 topic_id: 28391 reply_id: 328391[/import]

Does your house have to be dynamic? That’s the reason why it’s moving. Make sure you add your player as dynamic and since the elder is static and not a sensor it shouldn’t go through him either. Sorry wasn’t able to read the whole thing, will check back in 30 minutes, let me know if that fixes anything [import]uid: 77199 topic_id: 28391 reply_id: 114622[/import]

Well I changed the house to static and my made my player dynamic. For some reason if the player touches the house now he starts back to where he was before the touch even happened in the location of the house. [import]uid: 49863 topic_id: 28391 reply_id: 114628[/import]

Does your player still go through the elder or did that get fixed? Also if your player goes back to the starting location on collision with the house that means it is most likely from something in your collision code that sends your player back to those coordinates. I can’t tell you exact since I don’t know your code. If you post more code I might be able to help you [import]uid: 77199 topic_id: 28391 reply_id: 114630[/import]

I notice that the density property of your elder is zero, which effectively means it has no mass. That may explain why your player is passing through it.

  • Andrew [import]uid: 109711 topic_id: 28391 reply_id: 114643[/import]

I changed density values and still having the same effect. Here is the code updated with the moving function.

[code]
local elderHouse = display.newImage(“gumm_house_smooth.png”)
elderHouse.name = “elderHouse”
elderHouse:scale(0.45, 0.45)
elderHouse:setReferencePoint(display.CenterReferencePoint)
elderHouse.x = display.contentWidth * 0.5 + 25
elderHouse.y = display.contentHeight * 0.25 - 55
physics.addBody(elderHouse, “static”, {density=0.0, friction=0.0, bounce=0.0, radius= 50})

local elder = display.newImage(“gumm_elder_40X40_2.png”)
elder.name = “elder”
elder:scale(0.35, 0.35)
elder:setReferencePoint(display.CenterReferencePoint)
elder.x = display.contentWidth * 0.5 - 35
elder.y = display.contentHeight * 0.25 - 25
physics.addBody(elder, “static”, {density=2.0, friction=0.0, bounce=0.0, radius= 15})
elder.isSensor = false


------------area for creating gumm sprite------------------------------------

function newGummMovingSprite()

gummMoving = sprite.newSprite(spriteSet2)
gummMoving:prepare(“default”)
gummMoving.isHitTestable = false
return gummMoving

end

local function movePlayer( event )

if event.phase ~= ended then
transition.to( gummMoving, { time=1500, x = event.x, y = event.y } )
–elseif gummMoving.y > 100 then gummMoving.y = 100
–gummMoving.velocity = 0
end

end

local function spawnPlayer( event )

gummMoving = newGummMovingSprite()
gummMoving.name = “gummMoving”

physics.addBody(gummMoving, “dynamic”, {density=1.0, friction=0.5, bounce=0.0, radius= 10})
gummMoving.isSensor = false
gummMoving.isBullet = true
gummMoving.collision = onCollision
gummMoving:addEventListener(“collision”, gummMoving)
Runtime:addEventListener(“touch”, movePlayer)
gummMoving:play()
end

[import]uid: 49863 topic_id: 28391 reply_id: 114681[/import]

It’s because of this line that collisions are disabled for the elder.

[lua]elder.isSensor = false[/lua]

Maybe you’ll need to change this one too:

[lua]gummMoving.isSensor = false[/lua] [import]uid: 144908 topic_id: 28391 reply_id: 114717[/import]

I changed these values and even removed them. It didn’t have an effect on the problem. [import]uid: 49863 topic_id: 28391 reply_id: 114765[/import]

OK I finally got the draw mode to start working. Now I can see that when I touch the screen over an object I don’t want the player to pass through it goes to the touch event then pushes the player outside of the collision boundary. But before it pushes the player away it lets the player go anywhere inside of the boundary. It’s has to be an issue with the way I move the player. [import]uid: 49863 topic_id: 28391 reply_id: 114802[/import]

That sir is an awesome piece of code. Thank you, it worked great once I applied a few changes.
Here is the changed code…

[lua]if event.phase == “began” then
movetx = event.x - gummMoving.x
movety = event.y - gummMoving.y
vmax = 100
moves = (movetx*movetx + movety*movety)
sfactor = (vmax^2/moves)^0.5
velx = movetx * sfactor
vely = movety * sfactor
gummMoving:setLinearVelocity(velx*3, vely*3)
end

if event.phase == “ended” then
gummMoving:setLinearVelocity(0, 0)
end
[import]uid: 49863 topic_id: 28391 reply_id: 114816[/import]

I’ve never really used transition.to to move my guy so it might be that. Try this code just to see if it’s that, if it is we can re-code it to work the way you want it.

local function movePlayer( event )  
movetx = event.x - gummMoving.x  
movety = event.y - gummMoving.y  
vmax = 100  
moves = (movetx\*movetx + movety\*movety)  
sfactor = (vmax^2/moves)^0.5  
velx = movetx \* sfactor  
vely = movety \* sfactor  
gummMoving:setLinearVelocity(velx\*3, vely\*3);   
 if event.phase == "ended" then  
 gummMoving:setLinearVelocity(0, 0)  
 end  
end  
Runtime:addEventListener( "touch", movePlayer )  

This moves where you touch but you have to hold it down.

Edited: Whoops fixed the mistype on “ended” and changed player to gummMoving. Glad you were able to change it by yourself as well.

The thing about this piece of code though is if they hold it down your player will continue moving towards that direction. If you add the “began” event then if the player moves his finger/touch the player won’t follow and just go in that direction till he ends the touch and re-touches. The best would probably be to add a “moving” event but I was too lazy to do that. If something critical is missing to your movement or would like it to work a little different feel free to let me know, best of luck [import]uid: 77199 topic_id: 28391 reply_id: 114810[/import]

Yeah I like the effects I am getting with the “began”. Was kind of funny though. When I removed the “began” the player started spinning and acting weird. I didn’t want the touch to be constant like that anyway. Again thanks for you help.

To the powers that be. This one is resolved. [import]uid: 49863 topic_id: 28391 reply_id: 114825[/import]