How to apply collision for dynamic without grativity and dynamic witn grativity

Hi,
As My understand about collision detection seem like below.

  • Static bodies do not collide with other static or kinematic bodies
  • Kinematic bodies do not collide with other static or kinematic bodies.
  • Dynamic body can collide with all body types

My Problem as below

I’ve 1 Player and 2 object

I need Player 1 collide with Object 1 and 2 and follow condition below

  • Player 1 moved manually by the user
  • Object 2 moved manually by coding
  • Object 3 moved by gravity
  • Player 1 and Object 1 don’t need apply gravity, But Object 2 apply with gravity.

Do you have solution way or suggestion for above condition?

My code same as below

...  
setGravity(0,9.8)  
  
--Player 1 moved manually by the user  
physics.addBody( Player1, "Dynamic", { density=1.6, friction=0.5, bounce=0.2, shape=Player1Shape } )  
  
-- Object 2 moved manually by coding  
physics.addBody( Object1, "Kinematic", { density=1.6, friction=0.5, bounce=0.2, shape=Object1Shape } )  
  
--Object 2 moved by gravity  
physics.addBody( Object2, "Dynamic", { density=1.6, friction=0.5, bounce=0.2, shape=Object2Shape } )  

Thank in advance
Mate [import]uid: 58733 topic_id: 13471 reply_id: 313471[/import]

It seems as though you want to be able to have a collision on a body that would not normally work in your specific collision scenario, correct?

If so, have you considered using isSensor? [import]uid: 52491 topic_id: 13471 reply_id: 49440[/import]

Yes, Peach

isSensor? Do you have guideline or example for me.
Because I never use this before?

Thanks
Mate [import]uid: 58733 topic_id: 13471 reply_id: 49447[/import]

“isSensor” will tell your physics object to register the collision, but not make the 2 objects physically react. Check the API listing for full info.

It’s a handy way to check for collisions using physics, without sacrificing control.

yourObject.isSensor = true  

Hope that helps, let me know if you’re still stuck.

[import]uid: 67933 topic_id: 13471 reply_id: 49474[/import]

I also have the problem. I have a kinetic object (I don’t want it affected my gravity) but when it moves to the end of the screen (I set up a static object rectangle just offscreen) it does not register the collision.

Even the isSensor didn’t work for me. [import]uid: 22392 topic_id: 13471 reply_id: 49481[/import]

Is this the kind of thing you were looking for?

[code]
–hides iphone status bar
–display.setStatusBar(display.HiddenStatusBar)

physics = require(“physics”)
physics.start()
physics.setGravity(0,9.8)

physics.setDrawMode( “hybrid” ) – uncomment to see physics interactions.

–physics.setDrawMode( “hybrid” ) – uncomment to see physics interactions.

–cache content dimensions and math.random for better performance
_H = display.contentHeight;
_W = display.contentWidth;
mRand = math.random;
terrainspeed = 5
maxCrater = 5

tsmod = 0
craterRate = 1000
craterMod = 0
local centerX = display.contentWidth/2;

local hitdet = display.newRect(0, 0, _W, 5)
hitdet.x = _W / 2
hitdet.y = _H - 5
hitdet:setFillColor(0, 0, 255)
physics.addBody(hitdet, “static”, {density = 1.0, friction = 0, bounce = 0, isSensor =true})
hitdet.myName = “hitDet”

local buggy = display.newRect(0, 0, 30, 30)
buggy.x = _W / 2
buggy.y = _H / 3 + _H / 3
buggy.strokeWidth = 3
buggy:setFillColor(140, 140, 140)
buggy:setStrokeColor(180, 180, 180)

function tsmodifier() --picks a random value by which to modify terrainspeed
tsmod = mRand(0, 10)
–print (tsmod)
end

function spawnCrater() – spawns a crater, checks its y position and moves it if necessary.
local crater = display.newCircle (mRand(0, _W), _H / _H, mRand(30, 100))

physics.addBody( crater, { friction=0, bounce=0, radius = crater.radius} )
crater.isSensor = true
crater.myName = “Crater”

local function moveCrater() – moves the crater
if crater then
if crater.y < _H + crater.contentHeight then
crater.y = crater.y + terrainspeed + tsmod
end
end
end

–Timer2 = timer.performWithDelay(10,moveCrater, 0)

end

local function onLocalCollision( self, event )
if ( event.phase == “began” ) then

print( self.myName … ": collision began with " … event.other.myName )
local function removecrater()
event.other:removeSelf()
end

Timer4 = timer.performWithDelay(1,removecrater, 1)

elseif ( event.phase == “ended” ) then

print( self.myName … ": collision ended with " … event.other.myName )

end

end

–crater.collision = onLocalCollision
–crater:addEventListener( “collision”, hitdet )

hitdet.collision = onLocalCollision
hitdet:addEventListener( “collision”, hitdet )
–]]
Timer1 = timer.performWithDelay(craterRate, spawnCrater, 0)
Timer3 = timer.performWithDelay(5000,tsmodifier, 0)
[/code] [import]uid: 67933 topic_id: 13471 reply_id: 49640[/import]

Thanks Spider, But I’ve few question about your code

As my understand
Object “hitdet”(static) : will be apply with Gravity
Object “crater”(dynamic): will not apply with Gravity
Am i correct?
I’ll try to use your code, I’m not sure it will be work for me.
Let me try on today. :slight_smile:

Mate [import]uid: 58733 topic_id: 13471 reply_id: 49654[/import]

Hitdet is short for hit detection. It is a static object which waits at the bottom of the screen for other objects to collide with it. When they do, they are deleted.

The craters have gravity applied to make them fall.

Why don’t you think your code will work? If you set up your 3 objects to move as you initially say, use “isSensor=true” to enable collisions and follow the collision detection example from my code, there’s no reason you can’t acheive what you say you want to. Unless I’m missing something.

[import]uid: 67933 topic_id: 13471 reply_id: 49758[/import]

Hi Spider thanks for your explain
Yes you are correct but my scenario same as below

I’ve 3 Objects.
-Hitdet : assume Player1
-craters : assume object1
-buggy : assume object2.

I need object 1 have gravity.
and Player,Object2 not need gravity.

Collision condition
Player1 collide with object 1 and object 2.

Can you suggest about how to apply with your code?
yesterday I try to apply your code but static object(Player1 and Object2) can’t collide same condition above.

Thanks

Mate [import]uid: 58733 topic_id: 13471 reply_id: 49859[/import]

Here, I think this is what you’re looking for?

Player1 = blue box
Object1 = red box
Object2 = green rectangle.

You can drag the blue and red boxes around to play with it.

physics = require("physics")  
physics.start()  
physics.setGravity(0,9.8)  
   
\_H = display.contentHeight;  
\_W = display.contentWidth;  
mRand = math.random;  
local player1 = display.newRect(0, 0, 50, 50)  
player1.x = \_W / 2  
player1.y = \_H / 2  
player1:setFillColor(0, 0, 255)  
physics.addBody(player1, "kinematic", {density = 1.0, friction = 1, bounce = 0.2})  
player1.myName = "player1"  
  
local function dragplayer1 (event)  
player1.x = event.x  
player1.y = event.y  
end  
player1:addEventListener ("touch", dragplayer1)  
  
local object1 = display.newRect(0, 0, 50, 50)  
object1.x = \_W / 2  
object1.y = \_H / \_H - 50  
object1:setFillColor(255, 0, 0)  
physics.addBody(object1, {density = 1.0, friction = 1, bounce = 0.2})  
object1.myName = "object1"  
  
local function dragObject1 (event)  
object1.x = event.x  
object1.y = event.y  
end  
object1:addEventListener ("touch", dragObject1)  
  
local object2 = display.newRect(0, 0, \_W, 5)  
object2.x = \_W / 2  
object2.y = \_H - 5  
object2:setFillColor(0, 255, 0)  
physics.addBody(object2, "kinematic", {density = 1.0, friction = 1, bounce = 0.2})  
object2.myName = "object2"  

Let me know if that’s not what you wanted.

Cheers

Dan

EDIT: Ok, I see the problem now. Can you let me know if this is what you wanted, apart from player1 not registering collisions with Object2?

I presume you wanted to test if player1 has gone off the bottom of the screen? If so, there are other ways to do this. Add the following code to what I posted above.

function gameover()  
 if player1.y \> \_H then  
 print ("GAME\_OVER")  
 end  
end  
  
Runtime:addEventListener("enterFrame", gameover)   

Dan [import]uid: 67933 topic_id: 13471 reply_id: 49865[/import]

?Hi Spider
Thanks again for your help, But I’ve 1 question for lasted code.
Do you have collision detect function? I do not see in lasted code.
Can you help provide solution for collision detect for Player 1 collision with Object 1 and Object 2, Do you have the solution for collision detect for

  • player 1 collision(no gravity) with object 1(no gravity)
  • player 1 collision(no gravity) with object 2(have gravity)

Because my game i’ve
1 Player : Player 1
2 Enemies: Object 1 and Object 2
Object 1 not need gravity but Object 2 need to have gravity.
Sorry again :slight_smile:
Mate [import]uid: 58733 topic_id: 13471 reply_id: 49898[/import]

Hi

this is the code for collision detection from my earlier example

local function onLocalCollision( self, event )  
 if ( event.phase == "began" ) then  
   
 print( self.myName .. ": collision began with " .. event.other.myName )  
 local function removecrater()  
 event.other:removeSelf()  
 end  
  
 Timer4 = timer.performWithDelay(1,removecrater, 1)   
  
 elseif ( event.phase == "ended" ) then  
   
 print( self.myName .. ": collision ended with " .. event.other.myName )  
   
 end  
 --------------------------------------------------------------------------------------------  
end  

In my last example there is not a specific collision detection function, the objects are physical bodies, so react with each other automatically.

I’m sorry but I do not have a work around for detecting collisions between player1 (kinematic) and object2 (static) because Box2d, the physics engine does not detect collisions between kinematic and static objects.

I’ve played with it a little more and this is what I’ve got. If this isn’t what you want then I’m not sure I understand what you’re asking.

physics = require("physics")  
physics.start()  
physics.setGravity(0,9.8)  
   
\_H = display.contentHeight;  
\_W = display.contentWidth;  
mRand = math.random;  
local player1 = display.newRect(0, 0, 50, 50) --blue  
player1.x = \_W / 2  
player1.y = \_H / 2  
player1:setFillColor(0, 0, 255)  
physics.addBody(player1, "kinematic", {density = 1.0, friction = 1, bounce = 0.2})  
player1.myName = "player1"  
  
local function dragplayer1 (event)  
player1.x = event.x  
player1.y = event.y  
end  
player1:addEventListener ("touch", dragplayer1)  
  
local object1 = display.newRect(0, 0, 50, 50) --red  
object1.x = \_W / 2  
object1.y = \_H / \_H - 50  
object1:setFillColor(255, 0, 0)  
physics.addBody(object1, {density = 1.0, friction = 1, bounce = 0.2})  
object1.myName = "object1"  
  
local function dragObject1 (event)  
object1.x = event.x  
object1.y = event.y  
end  
object1:addEventListener ("touch", dragObject1)  
  
local object2 = display.newRect(0, 0, 50, 50) --green  
object2.x = \_W / 2  
object2.y = \_H / 3  
object2:setFillColor(0, 255, 0)  
physics.addBody(object2, "kinematic", {density = 1.0, friction = 1, bounce = 0.2})  
object2.myName = "object2"  
  
local function dragObject2 (event)  
object2.x = event.x  
object2.y = event.y  
end  
object2:addEventListener ("touch", dragObject2)  
  
local ground = display.newRect(0, 0, \_W, 5)  
ground.x = \_W / 2  
ground.y = \_H - 5  
ground:setFillColor(0, 255, 0)  
physics.addBody(ground, "static", {density = 1.0, friction = 1, bounce = 0.2})  
ground.myName = "ground"  
local function onLocalCollision( self, event )  
 if ( event.phase == "began" ) then  
   
 print( self.myName .. ": collision began with " .. event.other.myName )   
  
 elseif ( event.phase == "ended" ) then  
   
 print( self.myName .. ": collision ended with " .. event.other.myName )  
   
 end  
end  
  
object2.collision = onLocalCollision  
object2:addEventListener( "collision", object2 )  
  
player1.collision = onLocalCollision  
player1:addEventListener( "collision", player1 )  

[import]uid: 67933 topic_id: 13471 reply_id: 49909[/import]

Hi Spider
Thanks for your support.
Yesterday I try to rethink and found for workaround.
I’ll set gravity to zero and object2 I’ll use transition solution for move as manual and use your code for apply my scenario.

Thanks
Mate [import]uid: 58733 topic_id: 13471 reply_id: 50231[/import]