Collision Problem

I’m having a problem with collisions in my game, a dynamic object (Player) is sticking static objects,
this are the links with the images of the problem :

http://i49.tinypic.com/osx62s.png

http://i50.tinypic.com/2ntzcdv.png

http://i49.tinypic.com/2hn88i9.png

I have already sent an email to the support team and they showed me this link:
http://www.coronalabs.com/blog/2011/08/08/solutions-to-common-physics-challenges/

but I still can not solve the problem,
can someone help me please?
thank you,
[import]uid: 95474 topic_id: 30344 reply_id: 330344[/import]

Hey there!

I’m assuming that you character (the dynamic object) is being moved by pressing the arrow correct?

When moving the character maybe try updating his position in smaller increments but at faster intervals. that way the object isn’t popping in and out of the static object.

Your code for moving the character would be good to see.

Wish the best! [import]uid: 30414 topic_id: 30344 reply_id: 121583[/import]

Thank you for the help omegapatten !
how do I increase the velocity of the intervals in ‘Listener’?
this is my code :

-------------------------------------------------------------------------------->
local function update()
player.x = player.x + dx
end

Runtime:addEventListener(“enterFrame”, update)

local function move_right(event)
if event.phase == “began”
dx = 5
else
dx = 0
end
end

button_right:addEventListener(“touch”, move_right)

local function move_left(event)
if event.phase == “began”
dx = -5
else
dx = 0
end
end

button_left:addEventListener(“touch”, move_left)

----------------------------------------------------------------------------------------->

any suggestions? [import]uid: 95474 topic_id: 30344 reply_id: 121602[/import]

With any dynamic body you never want you use a hard coded X,Y or a transition.

You need to apply force to your object:

[lua]
myBody:applyForce( 500, 2000, myBody.x, myBody.y )[/lua] [import]uid: 7177 topic_id: 30344 reply_id: 121793[/import]

Just a side note: I think you actually can do direct x/y coordinate placement and receive a collision. You might just need to “flash” the object by re-declaring it as active (isBodyActive = true). Even if the body is already active, I think you can use this method to “remind” Corona to collision check it. Just a weird trick I stumbled upon… but I still wouldn’t rely on it without thorough testing, because I might have this wrong… you might need to turn the body off then on in direct sequence to “flash” its presence to the collision detection engine.

Absolutely it’s better to move physical objects via physical methods when you need collision detection. But this odd trick can still be useful in some cases.

Brent

[import]uid: 9747 topic_id: 30344 reply_id: 121804[/import]

Darkmod,
This solution is good, but I wanted the player to move in a smooth way, I tried to apply force on it, but it accelerates instead of maintaining a speed continues and is jumping even though I configured the “bounce = 0”.

could you still help me ?
thanks, [import]uid: 95474 topic_id: 30344 reply_id: 121832[/import]

Brent,

unfortunately setting the property “Body Active = true” did not work.
But I realized that when I I reduce the value of the variable “dx”, the object does not pass through the wall, but it is slow and I do not know how to set intervals faster than the object back to normal speed.

thanks, [import]uid: 95474 topic_id: 30344 reply_id: 121833[/import]

If you add 5 to body.x, it probably jumps inside another object. If you want to go 5 pixels ahead in a single frame, then add velocity to your body ( it requires the velocity in pixel/second, so if your game runs at 60 fps then set vx to 300).
In the next frame you can fix the body position if needed, but collisions will be detected as expected.

I’m using this technic in my game, and works very well. [import]uid: 88330 topic_id: 30344 reply_id: 121916[/import]

Macc,
I configured the “fps = 60” in the config file,
this worked in the Corona Simulator but was too slow in Android.
was that what I should do?

thanks, [import]uid: 95474 topic_id: 30344 reply_id: 121953[/import]

You mean the perfomance of our game is slow? If previously you used 30fps, you can still use it, just add 150 to vx instead of 300.
[import]uid: 88330 topic_id: 30344 reply_id: 122051[/import]

Macc,
The game works in “fps = 30”, but i still do not know which variable is “vx” that you are referring to.
I’m sorry to ask this.
Thank you, [import]uid: 95474 topic_id: 30344 reply_id: 122052[/import]

Jesseh:
http://docs.coronalabs.com/api/type/Body/setLinearVelocity.html

In your case (30fps):
myBody:setLinearVelocity( 150, 0 ) – right
myBody:setLinearVelocity( -150, 0 ) – left
[import]uid: 88330 topic_id: 30344 reply_id: 122064[/import]

Macc,
Firstly thank you for your attention;)
the problem of the collision has been resolved, but now gravity does not work well.
the player falls slowly and when I increase the value function keeps the speed, and the body does not accelerate with gravity.

I configured like this:
player:setLinearVelocity (dx, 0)

and put this function in the update() function above.
how do I configure “setLinearVelocity” without changing gravity?

Thank you again, [import]uid: 95474 topic_id: 30344 reply_id: 122093[/import]

You need to play around with some of the physics settings unlit you get what your looking for.

[lua]physics.setVelocityIterations( 24 )
physics.setPositionIterations( 10 )
physics.addBody( playerbody, “dynamic”, { friction=1, bounce=0, density=6 ,filter = { categoryBits = 1, maskBits = 1} } )[/lua]

and i would not get too crazy with the gravity … try adding more density to the player object. [import]uid: 7177 topic_id: 30344 reply_id: 122103[/import]

Hey there!

I’m assuming that you character (the dynamic object) is being moved by pressing the arrow correct?

When moving the character maybe try updating his position in smaller increments but at faster intervals. that way the object isn’t popping in and out of the static object.

Your code for moving the character would be good to see.

Wish the best! [import]uid: 30414 topic_id: 30344 reply_id: 121583[/import]

Darkmod,

Thanks for the tip, but gravity is still the problem in the method “setLinearVelocity.”
The player moves well side to side, but slowly falls.
And the method “applyForce” worked well, but the player starts slowly and then accelerates motion, I would like it moved around smoothly like in games: Mario, Sonic, Megaman … for example.

Thank you again. [import]uid: 95474 topic_id: 30344 reply_id: 122372[/import]

Thank you for the help omegapatten !
how do I increase the velocity of the intervals in ‘Listener’?
this is my code :

-------------------------------------------------------------------------------->
local function update()
player.x = player.x + dx
end

Runtime:addEventListener(“enterFrame”, update)

local function move_right(event)
if event.phase == “began”
dx = 5
else
dx = 0
end
end

button_right:addEventListener(“touch”, move_right)

local function move_left(event)
if event.phase == “began”
dx = -5
else
dx = 0
end
end

button_left:addEventListener(“touch”, move_left)

----------------------------------------------------------------------------------------->

any suggestions? [import]uid: 95474 topic_id: 30344 reply_id: 121602[/import]

Jesseh, here’s a quick mock up. By no means is it perfect but should get you going to the right direction. There are tons of physics options/settings I recommend reading up on them and get a basic understanding how to use them. By no means would I use my d-pad controller I would rate it as a F. Take note how I use velocity ‘setLinearVelocity vs applyForce’. Play around with all the setting to get a better understanding of the physics world. Friction,bounce,density,Force,Gravity…………. And so on.

Gl- Darkmod

— config.lua —
[lua]application = {
content = {
width = 320,
height = 480,
scale = “letterBox”,
fps = 30,

},

}[/lua]

– main.lua –
[lua]display.setStatusBar( display.HiddenStatusBar )

physics = require “physics”
physics.start(true)
–~ physics.setContinuous( false )
physics.setDrawMode (“hybrid”)
physics.setGravity( 0,15 )
physics.setVelocityIterations( 24 )
physics.setPositionIterations( 32 )
– Local VARS --------------------------------------------------
local posX, posY = display.screenOriginX, display.screenOriginY;
local SCRN_W, SCRN_H = 480 + (posX * -1) * 2, 320 + ( posY * -1)*2

– create bounds --------------------
local leftBounds = display.newRect( posX, posY,2,SCRN_H)
physics.addBody( leftBounds , “static”, { friction=0, bounce=0, density=0 , filter = { categoryBits = 1, maskBits =1 }} )

local rightBounds = display.newRect( 480 + (posX * -1) -2 , posY,2,SCRN_H)
physics.addBody( rightBounds , “static”, { friction=0, bounce=0, density=0 , filter = { categoryBits = 1, maskBits =1 }} )

local botBounds = display.newRect( posX ,318 ,SCRN_W,2)
physics.addBody( botBounds , “static”, { friction=2, bounce=0, density=0 , filter = { categoryBits = 1, maskBits =1 }} )

– create Player ---------------------
local player = display.newRect( 240, 160 ,32,64)
– Addbody
physics.addBody( player, “dynamic”, { friction=1, bounce=0, density=7, filter = { categoryBits = 1, maskBits = 1} } )
player.isSleepingAllowed = false
player.isFixedRotation = true
– boxes
local box1 = display.newRect( 300, botBounds.y - 32,32,32)
physics.addBody( box1, “static”, { friction=0.2, bounce=0, density=0, filter = { categoryBits = 1, maskBits = 1} } )

local letsMove

local function playerFrame(evt)

– Get state is on ground or floating
local vx, vy = player:getLinearVelocity()

– isGrounded
if vy == 0 then

if letsMove == “r” then
player:setLinearVelocity( 200, 0)
elseif letsMove == “l” then
player:setLinearVelocity( -200, 0)
end
– isFloating
else
if letsMove == “r” then
player:applyForce( 200, 0, player.x, player.y)
elseif letsMove == “l” then
player:applyForce( -200, 0, player.x, player.y)
end
end
end
local function buttonTouch(evt)

local phase = evt.phase
local _x, _y = evt.x, evt.y
local ID = evt.target.id

if (phase == “began”) then
if ID == “leftBut” then
letsMove = “l”
elseif ID == “rightBut” then
letsMove = “r”
elseif ID == “jumpBut” then
player:applyForce( 0, -4000 , player.x, player.y)

end
else
letsMove = nil

end

end

– create Movment Buttons ------------
local leftBut = display.newCircle( posX + 40, SCRN_H - 40, 24)
leftBut.id= “leftBut”
leftBut:addEventListener(“touch”, buttonTouch)
local rightBut = display.newCircle( posX + 95, SCRN_H - 40, 24)
rightBut.id = “rightBut”
rightBut:addEventListener(“touch”, buttonTouch)
local jumpBut = display.newCircle( posX + 67, SCRN_H - 80, 20)
jumpBut.id = “jumpBut”
jumpBut:addEventListener(“touch”, buttonTouch)


Runtime:addEventListener(“enterFrame”, playerFrame)[/lua]

[import]uid: 7177 topic_id: 30344 reply_id: 122388[/import]

With any dynamic body you never want you use a hard coded X,Y or a transition.

You need to apply force to your object:

[lua]
myBody:applyForce( 500, 2000, myBody.x, myBody.y )[/lua] [import]uid: 7177 topic_id: 30344 reply_id: 121793[/import]

Just a side note: I think you actually can do direct x/y coordinate placement and receive a collision. You might just need to “flash” the object by re-declaring it as active (isBodyActive = true). Even if the body is already active, I think you can use this method to “remind” Corona to collision check it. Just a weird trick I stumbled upon… but I still wouldn’t rely on it without thorough testing, because I might have this wrong… you might need to turn the body off then on in direct sequence to “flash” its presence to the collision detection engine.

Absolutely it’s better to move physical objects via physical methods when you need collision detection. But this odd trick can still be useful in some cases.

Brent

[import]uid: 9747 topic_id: 30344 reply_id: 121804[/import]