Physics

Hi All

would like a character walk around and have collisions with static walls, but if i set my character to static they have no visible collisions, but if i set my character to dynamic the collisions are great but my sprite rotates as in collides?

– Physics Engine 
local physics = require “physics”
physics.start()
physics.setGravity(0, 0)

 

local player = display.newImage(“beer.png”)–display.newRect(display.contentWidth*0.5,display.contentHeight*0.5,32,32)
player:setFillColor(53, 115, 47)
player.name = “player”
player.x = 100
player.y = 100
–physics.addBody(player, {isSensor = true})
physics.addBody( player, “dynamic” , { density=0.0, friction=0, bounce=0 } )
–player:setStrokeColor(180, 180, 180)

 

local walls = display.newGroup()

 

local function AddWall(x,y,w,l)

 

walls:toFront()

 

local wall = display.newRect(x,y,w,l) --display.newImage(“beer.png”)
wall.name = “wall”
–wall.x = math.floor(math.random() * (display.contentWidth - 32))
–wall.y = math.floor(math.random() * (display.contentHeight - 32))
physics.addBody(wall, “static”, {density = 0.0, friction = 0, bounce = 0})
walls.insert(walls, wall)
end

 

AddWall(50,50,32,32)

 

local posx = 0
local posy = 0

 

local function MovePlayer()

 

player.x = player.x + posx
player.y = player.y + posy

 

if player.y < 0 then
player.y = 100
level2()
end

 

end
Runtime:addEventListener( “enterFrame”, MovePlayer );

 

local left = false
local right = false

 

local function ControlPlayer(event)

 

 if event.x < player.x then
 left = true
 right = false
 posx = -3
 elseif event.x > player.x then
 right = true
 left = false
 posx = 3
 else
 posx = 0
 end

 

 if event.y < player.y then
 posy = -3
 elseif event.y > player.y then
 posy = 3
 else
 posy = 0
 end
 
if event.phase == “ended” then
posx = 0
posy = 0
left = false
right = false
end

 

end
Runtime:addEventListener( “touch”, ControlPlayer )

You can set isFixedRotation to true to keep the physics objects from rotating. Remember to declare it after your create the physics object:

http://docs.coronalabs.com/api/type/Body/isFixedRotation.html

Thank you Panc, any idea how to stop the object do that bounce back to the non colliding position?

Can you explain a bit more about your second issue? I’m not familiar with what you mean by “bounce back to the non colliding position”.

hi Panc

my player - player, “dynamic” , { density=0.0, friction=0, bounce=0 } )

enters the static wall approx 3 pixels, i am moving player 3 pixels at a time

and when collision registers the player is then moved back(causes a bounce back) from inside the wall so it does not collide anymore.

can i stop the player dead when he comes in contact with the wall?

am i using wrong method of moving? would transition stop the object the instant it collides.

i am trying to create a usable collision for statics and moving objects, classic sprite to sprite collision but using the physics to check this for me rather than write myselfe.

thx

If you’re going for classic top-down point-and-click RPG movement (which it seems you are) using the physics engine is fine, albeit a bit noisy when considering it’s a Runtime event. If you’re OK with that, then the physics option for detecting collisions is fine. I’d suggest checking out the pre-collision “contact” event, which will allow you to identify if an object is about to collide with another object:

http://www.coronalabs.com/blog/2012/11/27/introducing-physics-event-contact/

You can set isFixedRotation to true to keep the physics objects from rotating. Remember to declare it after your create the physics object:

http://docs.coronalabs.com/api/type/Body/isFixedRotation.html

Thank you Panc, any idea how to stop the object do that bounce back to the non colliding position?

Can you explain a bit more about your second issue? I’m not familiar with what you mean by “bounce back to the non colliding position”.

hi Panc

my player - player, “dynamic” , { density=0.0, friction=0, bounce=0 } )

enters the static wall approx 3 pixels, i am moving player 3 pixels at a time

and when collision registers the player is then moved back(causes a bounce back) from inside the wall so it does not collide anymore.

can i stop the player dead when he comes in contact with the wall?

am i using wrong method of moving? would transition stop the object the instant it collides.

i am trying to create a usable collision for statics and moving objects, classic sprite to sprite collision but using the physics to check this for me rather than write myselfe.

thx

If you’re going for classic top-down point-and-click RPG movement (which it seems you are) using the physics engine is fine, albeit a bit noisy when considering it’s a Runtime event. If you’re OK with that, then the physics option for detecting collisions is fine. I’d suggest checking out the pre-collision “contact” event, which will allow you to identify if an object is about to collide with another object:

http://www.coronalabs.com/blog/2012/11/27/introducing-physics-event-contact/