2 physics bodies following each other

Hello, I would like my monster to have a body and a head physics separate bodies. I been looking into all the physics joints guides trying stuff like:

physics.addBody( game.mboss\_1, "static",{ density=0, friction=0, bounce=0, filter = { categoryBits = 1, maskBits = 1 }, shape = game.waterBossShape} ) physics.addBody( game.mbossHead\_1, "dynamic",{ density=0, friction=0, bounce=0, filter = { categoryBits = 1, maskBits = 1 }, shape = game.waterBossHeadShape} )

and after doing a joint:

local weldJoint = physics.newJoint( “weld”, game.mboss_1, game.mbossHead_1, 0, 0 )

When I move the boss, the joint never drags the other physics body. I want to do a transistion on the body image and the head will follow with it, but every time I try the joints it just shows the joint line getting bigger and not bringing the physics body.

So I been using PhysicsEditor and that helped me get the 2 seperate physics bodies and when I move the body they both move and that works perfectly, except I can’t seem to figure out how to identify only the “head” collision for one of them and a “body” for the other. Yes I have also looked at the Physics Editor guides, and tried to figure it out.

PhysicsEditor part instead:

physics.addBody( game.mboss_1, “static”, physicsData:get(“turtle”) ) --gives 2 seperate physics bodies, but I can’t figure out how to identify them being seperate or their own unique identity in the code.

It doesn’t matter to me which method to use joints, or Physics Editor I just want one of them to work :slight_smile: Thanks for your time, any tips are greatly appreciated.

For a multibody… You can use:

event.element1 & event.element2 (iirc) to differentiate between the body parts in a collison listener.

They return an integer index of the body parts collided with between the two objects

looks like your boss is a static body, so I assume when you “move it” you’re directly altering it’s xy’s, right?  if so, there may never be any velocity to convey through the joint.  (also, density=0 is suspicious - once you DO get your weld working, you’ll likely need mass to keep it stable)  if that’s the case, perhaps you could just directly move the head’s xy’s too, by same amount, and eliminate the weld?

@Azmar,

Beware.   You may run into problems trying to move physics bodies with complex joints using transitions. 

If body  A attached to body B goes to sleep, it may not move when body B is moved by a transition.

I’m not saying this will happen, but I seem to remember having issues combining transitions and joints some time in the past.

If this happens, you may need to force the bodies to be awake all the time with the isSleepingAllowed attribute.

Hello, thanks for all the responses!

I decided to drop the physics editor, the multi-element body guide is helping me more, but still no results.

The physics bodies moving together seem to work perfectly with the physics code:

physics.addBody( game.mboss\_1, "static",{shape = game.waterBossShape}, {shape = game.waterBossHeadShape}) game.mboss\_1.element = {1,2} game.mboss\_1.type = "waterBoss"

I still have the same problem where I can’t figure out how to determine if he only hits the head or the body part, I tried to give the element part for 1 being head and 2 being the body…

my runtime collision handler (handles all my weapons so not specific to 1 object)

local weaponCollision = function(event)     if event.phase == "began" then         local agro = event.object1         local hit = event.object2               if (agro.type == 1 and event.element == 1) or (event.element == 1 and hit.type == 1) then                 print("`HIT Boss HEAD :)")         end         if (agro.type == 1 and event.element == 2) or (event.element == 2 and hit.type == 1) then               print("`HIT Boss HEAD :)")         end             --- I have also tried below part and several variations of it to get the element part.        if (agro.type == 1 and hit.element[1] == 1) or (agro.element[1] == 1 and hit.type == 1) then                 print("`HIT Boss HEAD :)")        end ....other code for other stuff end

I know 100% the .type == 1 part is my first weapon and works for everything else, so I know its only the element part that is not working correctly with picking up the multi element body.

Thanks for your time

[quote=“Azmar,post:5,topic:327779”]

Hello, thanks for all the responses!

I decided to drop the physics editor, the multi-element body guide is helping me more, but still no results.

The physics bodies moving together seem to work perfectly with the physics code:

physics.addBody( game.mboss\_1, "static",{shape = game.waterBossShape}, {shape = game.waterBossHeadShape}) game.mboss\_1.element = {1,2} game.mboss\_1.type = "waterBoss"

I still have the same problem where I can’t figure out how to determine if he only hits the head or the body part, I tried to give the element part for 1 being head and 2 being the body…

my runtime collision handler (handles all my weapons so not specific to 1 object)

local weaponCollision = function(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 if (agro.type == 1 and event.element == 1) or (event.element == 1 and hit.type == 1) then print("`HIT Boss HEAD :)") end if (agro.type == 1 and event.element == 2) or (event.element == 2 and hit.type == 1) then print("`HIT Boss HEAD :)") end --- I have also tried below part and several variations of it to get the element part. if (agro.type == 1 and hit.element[1] == 1) or (agro.element[1] == 1 and hit.type == 1) then print("`HIT Boss HEAD :)") end ....other code for other stuff end

I know 100% the .type == 1 part is my first weapon and works for everything else, so I know its only the element part that is not working correctly with picking up the multi element body.

Thanks for your time [/quote]
See my response up top. it’s event.element1 etc, not event.element :wink:

http://bit.ly/1BhsasM

@Gremlin the

if (agro.type == 1 and event.element1 == 1) or (event.element1 == 1 and hit.type == 1) then      print("`HIT Boss HEAD :)") end 

Picks up all the physics bodies, not just 1 of them.

I just want it only being able to register on one of the physics bodies

Check element2 also. Element1 is for one physics object, element2 for the other.

It’s helpful to print the elements too, so you can see what’s going on

I’ve tried all that and been using prints,

if (agro.type == 1 and event.element1 == 1) or (event.element1 == 1 and hit.type == 1) then    print("HIT Boss HEAD :)")    print(event.element1)    print(event.element2)    print(event.object1)    print(event.object2) end if (agro.type == 1 and event.element2 == 2) or (event.element2 == 2 and hit.type == 1) then    print("HIT Boss")    print(event.element1)    print(event.element2)    print(event.object1)    print(event.object2)  end   

I’ve even tried to print out the variables outside of collision just at the start and its all nil

print(game.mboss\_1.element1) print(game.mboss\_1.element2)

Still all registers on one body…for the element1

I’m using a normal collision handler for weapons being:

Runtime:addEventListener(“collision”, weaponCollision)

[quote=“Azmar,post:10,topic:327779”]

I’ve tried all that and been using prints,

if (agro.type == 1 and event.element1 == 1) or (event.element1 == 1 and hit.type == 1) then print("HIT Boss HEAD :)") print(event.element1) print(event.element2) print(event.object1) print(event.object2) end if (agro.type == 1 and event.element2 == 2) or (event.element2 == 2 and hit.type == 1) then print("HIT Boss") print(event.element1) print(event.element2) print(event.object1) print(event.object2) end

I’ve even tried to print out the variables outside of collision just at the start and its all nil

print(game.mboss\_1.element1) print(game.mboss\_1.element2)

Still all registers on one body…for the element1

I’m using a normal collision handler for weapons being:

Runtime:addEventListener(“collision”, weaponCollision) [/quote]

Print the elements outside of those “if” statements, for several reasons.

For a 2 part body, you should see 1 and 2 respectively, not necessarily in that order

Hi @Azmar,

You may be confused by what the “elements” are. “element1” should be the boss, and “element2” the other object… not the individual elements within the boss. So, the boss, as a whole, is always “element1”, and in a global collision handler, the value it returns will indicate which element of the boss was collided with.

Brent

For a multibody… You can use:

event.element1 & event.element2 (iirc) to differentiate between the body parts in a collison listener.

They return an integer index of the body parts collided with between the two objects

looks like your boss is a static body, so I assume when you “move it” you’re directly altering it’s xy’s, right?  if so, there may never be any velocity to convey through the joint.  (also, density=0 is suspicious - once you DO get your weld working, you’ll likely need mass to keep it stable)  if that’s the case, perhaps you could just directly move the head’s xy’s too, by same amount, and eliminate the weld?

@Azmar,

Beware.   You may run into problems trying to move physics bodies with complex joints using transitions. 

If body  A attached to body B goes to sleep, it may not move when body B is moved by a transition.

I’m not saying this will happen, but I seem to remember having issues combining transitions and joints some time in the past.

If this happens, you may need to force the bodies to be awake all the time with the isSleepingAllowed attribute.

Hello, thanks for all the responses!

I decided to drop the physics editor, the multi-element body guide is helping me more, but still no results.

The physics bodies moving together seem to work perfectly with the physics code:

physics.addBody( game.mboss\_1, "static",{shape = game.waterBossShape}, {shape = game.waterBossHeadShape}) game.mboss\_1.element = {1,2} game.mboss\_1.type = "waterBoss"

I still have the same problem where I can’t figure out how to determine if he only hits the head or the body part, I tried to give the element part for 1 being head and 2 being the body…

my runtime collision handler (handles all my weapons so not specific to 1 object)

local weaponCollision = function(event)     if event.phase == "began" then         local agro = event.object1         local hit = event.object2               if (agro.type == 1 and event.element == 1) or (event.element == 1 and hit.type == 1) then                 print("`HIT Boss HEAD :)")         end         if (agro.type == 1 and event.element == 2) or (event.element == 2 and hit.type == 1) then               print("`HIT Boss HEAD :)")         end             --- I have also tried below part and several variations of it to get the element part.        if (agro.type == 1 and hit.element[1] == 1) or (agro.element[1] == 1 and hit.type == 1) then                 print("`HIT Boss HEAD :)")        end ....other code for other stuff end

I know 100% the .type == 1 part is my first weapon and works for everything else, so I know its only the element part that is not working correctly with picking up the multi element body.

Thanks for your time

[quote=“Azmar,post:16,topic:327779”]

Hello, thanks for all the responses!

I decided to drop the physics editor, the multi-element body guide is helping me more, but still no results.

The physics bodies moving together seem to work perfectly with the physics code:

physics.addBody( game.mboss\_1, "static",{shape = game.waterBossShape}, {shape = game.waterBossHeadShape}) game.mboss\_1.element = {1,2} game.mboss\_1.type = "waterBoss"

I still have the same problem where I can’t figure out how to determine if he only hits the head or the body part, I tried to give the element part for 1 being head and 2 being the body…

my runtime collision handler (handles all my weapons so not specific to 1 object)

local weaponCollision = function(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 if (agro.type == 1 and event.element == 1) or (event.element == 1 and hit.type == 1) then print("`HIT Boss HEAD :)") end if (agro.type == 1 and event.element == 2) or (event.element == 2 and hit.type == 1) then print("`HIT Boss HEAD :)") end --- I have also tried below part and several variations of it to get the element part. if (agro.type == 1 and hit.element[1] == 1) or (agro.element[1] == 1 and hit.type == 1) then print("`HIT Boss HEAD :)") end ....other code for other stuff end

I know 100% the .type == 1 part is my first weapon and works for everything else, so I know its only the element part that is not working correctly with picking up the multi element body.

Thanks for your time [/quote]
See my response up top. it’s event.element1 etc, not event.element :wink:

http://bit.ly/1BhsasM

@Gremlin the

if (agro.type == 1 and event.element1 == 1) or (event.element1 == 1 and hit.type == 1) then      print("`HIT Boss HEAD :)") end 

Picks up all the physics bodies, not just 1 of them.

I just want it only being able to register on one of the physics bodies

Check element2 also. Element1 is for one physics object, element2 for the other.

It’s helpful to print the elements too, so you can see what’s going on