[solved] How to make a dynamic object not react to gravity while still being able to detect collisions?

I have 2 objects, one stationary and one moving. The stationary object is called player and the moving object is called wall. I need both objects to not react to gravity while still detecting collisions. So I tried using the code below on player.

isBodyActive = false isAwake = true

the object doesn’t fall but it also doesn’t detect collisions. I can’t make both objects kinematic because that prevents them from detecting collisions, I can’t make them both dynamic because they’ll fall, and applying the code above doesn’t work. So I can’t think of anything! Seems like there should be an easy solution, but I’ve been trying to fix this for more than 3 hours straight, searching the internet for answers but nothing works!! Can someone with more experience pleeeaaase help! I’m seriously exhausted from searching and thinking. :frowning:

Set objects gravity scale to 0

ex:

local tmp = display.newCircle( 10, 10, 10 ) physics.addBody( tmp, "dynamic" ) tmp.gravityScale = 0

Also, if you want an object to not respond do collisions (i.e. no bounce or pushback), set isSensor to true.

You may need this info later.

You can find out more about this kind of thing 

yeah that was actually the first thing I tried haha. gravityScale = 0 doesn’t work. :frowning: any other ideas?

Here, I isolated the code that I’m using:

local physics = require "physics" physics.start() local \_W, \_H, centerX, centerY = display.contentWidth, display.contentHeight, display.contentCenterX, display.contentCenterY function onLocalCollision( self, event ) if ( event.phase == "began" ) then print( "collided" ) local loseText = display.newText( "YOU LOSE", centerX, centerY, native.systemFont, 100 ) end end function move( self, event ) self.x = self.x - self.speed end player = display.newRect( centerX, centerY, 100, 100 ) wall = display.newRect( 0, 0, 100, 100 ) wall.x = \_W+wall.contentWidth/2 wall.y = \_H/2 wall.speed = 20 wall.enterFrame = move physics.addBody( player, "dynamic" ) physics.addBody( wall, "kinematic" ) player.isSensor = true player.gravityScale = 0 player.collision = onLocalCollision player:addEventListener( "collision" ) Runtime:addEventListener( "enterFrame", wall )

Update: HOORAY I just solved it! It was a bug elsewhere in the code xD it finally works! thanks for the help roaminggamer!

You’re welcome.

Thanks for taking the time to isolate the code for me, even though you then solved it.

I wonder, did isolating the code, lead you to think of / identify the actual culprit?  

Cheers,

Ed

yes it did  :smiley:

Set objects gravity scale to 0

ex:

local tmp = display.newCircle( 10, 10, 10 ) physics.addBody( tmp, "dynamic" ) tmp.gravityScale = 0

Also, if you want an object to not respond do collisions (i.e. no bounce or pushback), set isSensor to true.

You may need this info later.

You can find out more about this kind of thing 

yeah that was actually the first thing I tried haha. gravityScale = 0 doesn’t work. :frowning: any other ideas?

Here, I isolated the code that I’m using:

local physics = require "physics" physics.start() local \_W, \_H, centerX, centerY = display.contentWidth, display.contentHeight, display.contentCenterX, display.contentCenterY function onLocalCollision( self, event ) if ( event.phase == "began" ) then print( "collided" ) local loseText = display.newText( "YOU LOSE", centerX, centerY, native.systemFont, 100 ) end end function move( self, event ) self.x = self.x - self.speed end player = display.newRect( centerX, centerY, 100, 100 ) wall = display.newRect( 0, 0, 100, 100 ) wall.x = \_W+wall.contentWidth/2 wall.y = \_H/2 wall.speed = 20 wall.enterFrame = move physics.addBody( player, "dynamic" ) physics.addBody( wall, "kinematic" ) player.isSensor = true player.gravityScale = 0 player.collision = onLocalCollision player:addEventListener( "collision" ) Runtime:addEventListener( "enterFrame", wall )

Update: HOORAY I just solved it! It was a bug elsewhere in the code xD it finally works! thanks for the help roaminggamer!

You’re welcome.

Thanks for taking the time to isolate the code for me, even though you then solved it.

I wonder, did isolating the code, lead you to think of / identify the actual culprit?  

Cheers,

Ed

yes it did  :smiley: