kinematic bodies being affected by gravity

I have several sprites that I spawn to move across a conveyor belt on the top half of the screen. They are dynamic physics bodies so that they can interact with the conveyor belt and “fall off” when they get to the end of it. However, when I pick the objects up and move them to a certain portion of the screen, I no longer want them to be affected by gravity, so they don’t slide down the floor (which is at an isometric viewpoint). I’m able to change them to kinematic bodies just fine, but they are still affected by gravity. I’m sure that gravity is the culprit as, when I turn the y gravity to 0, they no longer move and vice versa.

Any idea what would cause my kinematic objects to be affected by gravity? [import]uid: 19999 topic_id: 26371 reply_id: 326371[/import]

I toggle bodyTypes a lot myself. Any time you changed a dynamic object to any other bodytype make sure to you turn off the body using obj.isBodyActive = false.

[lua]-- Body to kinematic –
obj.isBodyActive = false
obj.bodyType = ‘kinematic’
obj.isBodyActive = true

[import]uid: 7177 topic_id: 26371 reply_id: 107074[/import]

Darkmond,
Thanks for the quick reply. Just a question, why would I need to make the body inactive? Also, I am finding that I’m still going to have a problem b/c I need the body to be able to activate a sensor once it is turned into a kinematic body. [import]uid: 19999 topic_id: 26371 reply_id: 107089[/import]

Box2d stores your dynamic objects in its owned memory or array list. When you changed the bodyType to kinematic the original dynamic body is still in the Box2d list/memory. Setting it to inactive will remove from the list/memory if you don’t will you be getting dynamic + kinematic collision events on one object.

obj.isBodyActive = false " clears it from Box2d "
obj.isBodyActive = true " Re-activates after new bodyType"

[import]uid: 7177 topic_id: 26371 reply_id: 107100[/import]

I use this right now. I have objects that are static and turn dynamic after touching, or the other way around dynamic to static. Not sure if this will apply to you, if not it’s still fun to play with.

Etc etc.

This is basic stuff for the dragging, based on various code I’ve looked at and samples etc etc.

Darkmod will have the super advanced knowledge, I’m just sharing code that may or may not apply to your situation.

I am still noob, so yea.

[code]
local physics = require “physics”
physics.setPositionIterations(60) – prevents *some pass through
physics.setVelocityIterations(60) --another counter measure to “box2d tunneling”
physics.start(true)
physics.setGravity( 0,10)
physics.setDrawMode( “normal”)
physics.setScale(60)

–=================================
–DECLARE WIDTH AND HEIGHT SHORTCUT VARIABLES
–=================================
local _W = display.contentWidth
local _H = display.contentHeight
–=================================
–DECLARE GROUPS
–=================================
local dgroup = display.newGroup() – DRAG OBJECT GROUP
local ggroup = display.newGroup() – GOAL OBJECT GROUP
local d1 = display.newRect (_W/2-50,_H/2,130,130)
d1.myName = “dobject”
d1:setFillColor (0,255,0)
physics.addBody(d1, “dynamic”)
dgroup:insert(d1 )

–=================================
–DRAG CODE
–CREATES TEMPORARY TOUCH JOINTS BASED ON X AND Y AT TIME OF TOUCH
–=================================
local function dragBody( event )
–print (“drag started”)
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()

if “began” == phase then

stage:setFocus( body, event.id )
body.isFocus = true
–Change body to dynamic
event.target.bodyType = “dynamic”

– Create a temporary touch joint and store it in the object for later reference
body.tempJoint = physics.newJoint( “touch”, body, event.x, event.y )

–THIS PREVENTS ROTATION WHEN DRAGGING
–COMMENT OUT IF YOU DON’T WANT THIS
body.isFixedRotation = true
elseif body.isFocus then
if “moved” == phase then

– Update the joint to track the touch
body.tempJoint:setTarget( event.x, event.y )

elseif “ended” == phase or “cancelled” == phase then
stage:setFocus( body, nil )
body.isFocus = false

–I use this to pass information somewhere else
–event.target:setLinearVelocity( 0, 0 )
–event.target.angularVelocity = 0
–=======================================================================
–=======================================================================
–After the event ends, change body back to dynamic OR static.
– event.target.bodyType = “dynamic”
event.target.bodyType = “static”

–I haven’t gotten this one working. I’m sure there is a way, I haven’t spent enough time with it.
–event.target.bodyType = “kinematic”

–=======================================================================
–=======================================================================

– Remove the joint when the touch ends
body.tempJoint:removeSelf()
–print (“drag ended”)
end
end

– Stop further propagation of touch event
return true
end

d1:addEventListener( “touch”, dragBody )

[/code] [import]uid: 61600 topic_id: 26371 reply_id: 107105[/import]

did you have any luck staypuffinpc? [import]uid: 7177 topic_id: 26371 reply_id: 108165[/import]

Darkmod,
Thank you so much for your follow up. Right after posting the question, I had 4 dissertation/thesis defenses to attend to and then I took off to a conference in the mountains where I didn’t even have a cell reception most of the time. I’ve just returned and will try these solutions tomorrow (tonight I have to attend to year-end school performance for my children).

I really do appreciate the follow-up and hopefully, I’ll be able to get everything in order when I attack it. Should have an update by Friday.

-peter- [import]uid: 19999 topic_id: 26371 reply_id: 108389[/import]

OK, so I went back and played with it and I’m getting the exact opposite behavior of what I would expect. When I change the bodyType to kinematic, it no longer interacts with other physics objects, but it is still affected by gravity. My understanding of kinematic objects are that they are to be affected by interactions with other physics objects in the same display group but that they are unaffected by gravity. Any clue what might be going on? [import]uid: 19999 topic_id: 26371 reply_id: 108793[/import]

sorry for the long delay staypuffinpc, Its hard to tell whats going on without seeing your code. can you post a sand box example or you can email me your project can i can take a look at it. martiniallan at yhaoo dot com.

[import]uid: 7177 topic_id: 26371 reply_id: 110699[/import]

Darkmod,
Willing and giving attitudes like yours in the Corona community is one of the reasons I opted for Corona as a development platform. Thanks. The project itself is kind of large with lots of files, so I’ll set up a sandbox and give you access. I’ll also record a short vid to show what happens with the gravity situation. Is there a way I can pm you off board?

-Peter- [import]uid: 19999 topic_id: 26371 reply_id: 110958[/import]