object goes through wall

My player object is able to move through the the wall (with physics property “static”).

However if the player object is sitting on top of the wall, it works as intended (i.e. does not fall through).

Any thoughts? [import]uid: 59249 topic_id: 22004 reply_id: 322004[/import]

How are you moving the player? Do you have code we can have a look at? [import]uid: 52491 topic_id: 22004 reply_id: 87465[/import]

I’m moving the player using ‘transition.to’
Here is a sample code, the player object moves through the ‘block’ object.

_W = display.contentWidth;
_H = display.contentHeight;

local physics = require(“physics”);
physics.start();
physics.setDrawMode(“hybrid”);
local player = display.newCircle(50, 100, 20);
player:setFillColor(10, 50, 255);
local floor = display.newRect(0,0, _W, 1);
floor:setReferencePoint(display.CenterReferencePoint);
floor.x = _W/2;
floor.y = _H - 20;
floor.type = “ground”;

local block = display.newRect(10, 20, 50, 50);
block:setFillColor(255, 255, 255);
block:setReferencePoint(display.CenterReferencePoint);
block.x = 200;
block.y = _H - 50;
physics.addBody(floor, “static”, {bounce=0.2});
physics.addBody(block, “static”);
physics.addBody(player, {density=1.0, friction=1.0, radius=20});

–Move player object to ‘event.x’ location
local function onTouch(event)
transition.to(player, {time=700, x = event.x});
end
Runtime:addEventListener(“touch”, onTouch) [import]uid: 59249 topic_id: 22004 reply_id: 87471[/import]

Ah I see, transitioning - you would want to put a collision listener on the player to cancel the transition when a collision occurred. (That’s one option.)

Another option depending on what you’re going for might be using movement arrows and avoiding transitions by just changing x/y. [import]uid: 52491 topic_id: 22004 reply_id: 87495[/import]

I’ve tried to cancel the transition on collision, but this does not seem to work.

See my code below;

_W = display.contentWidth;
_H = display.contentHeight;

local physics = require(“physics”);
physics.start();
physics.setDrawMode(“hybrid”);
local player = display.newCircle(50, 100, 20);
player:setFillColor(10, 50, 255);
local floor = display.newRect(0,0, _W, 1);
floor:setReferencePoint(display.CenterReferencePoint);
floor.x = _W/2;
floor.y = _H - 20;
floor.type = “ground”;

local block = display.newRect(10, 20, 50, 50);
block.type = “wall”;
block:setFillColor(255, 255, 255);
block:setReferencePoint(display.CenterReferencePoint);
block.x = 200;
block.y = _H - 50;
physics.addBody(floor, “static”, {bounce=0.2});
physics.addBody(block, “static”);
physics.addBody(player, {density=1.0, friction=1.0, radius=20});
debugText = display.newText(“waiting for collision”, 0, 0, native.SystemFont, 20);
debugText.x = 100;
debugText.y = 50;
–Move player object to ‘event.x’ location
local function onTouch(event)
trans = transition.to(player, {time=700, x = event.x});
end
function player:collision(e)
if(e.phase == “began”) then
if(e.other.type == “wall”) then
transition.cancel(trans);
debugText.text = “collided with wall”
end
elseif(e.phase == “ended”) then
debugText.text = “waiting for collision”;
end
end

player:addEventListener(“collision”, player);
Runtime:addEventListener(“touch”, onTouch)
[import]uid: 59249 topic_id: 22004 reply_id: 87644[/import]

I would make your player “dynamic” and move your player object using applyForce. It does take a lot of trial and error to get all the physics properties right for the object. This is how I control my player in the game I’m working on and it works great. [import]uid: 38820 topic_id: 22004 reply_id: 87654[/import]

Glenn’s suggestion is solid; it would produce a better end result than collision/pre-collision checks which is what you’d likely use if using transition.to(). [import]uid: 52491 topic_id: 22004 reply_id: 87777[/import]

Thanks guys, I’ll reprogram it to use ‘applyForce’ instead of transitions.
[import]uid: 59249 topic_id: 22004 reply_id: 88157[/import]