Physics box rotate around player

Hello, I have a physics box that I rotate around my player in a direction based on his direction.

My rotation works fine and physics is fine, just its all based on timers and sometimes it appears to stop/rotate at different times making it very inconsistent. Would like it to work the same every time will being optimized.

Pretty much I have my player swing around a giant hammer thing. It goes around the player and has a small physics box so I want it to go around in a certain time frame.

Here is the code:

game.actionPlayer\_4:setLinearVelocity(0, 0);          if m == "d" then -- Check direction this instance the player is facing down!    game.actionPlayer\_4.rotation = 180; player:setSequence("scD");    timer.performWithDelay( 5, function() game.actionPlayer\_4:setLinearVelocity(150, -150); end, 1 )    timer.performWithDelay( 150, function() game.actionPlayer\_4:setLinearVelocity(-150, -150); end, 1 )    timer.performWithDelay( 300, function() game.actionPlayer\_4:setLinearVelocity(-150, 150); end, 1 )    timer.performWithDelay( 500, function() game.actionPlayer\_4:setLinearVelocity(150, 150); end, 1 )

Timers appear to move physics box with different results every time! It gets the job done but I want it consistent.

Thanks for help!

Hi @Azmar,

Would a physics joint be another option for this? For example, join the “hammer” object to the player using a distance joint?

Alternatively, how about making the hammer object centrally-oriented around the player, but its actual physics body is defined only to one side (tip) of that object? For example, if “H” is the hammer tip (the physics body) and “P” is the player, but the dashes indicate the entire object size (except that they are visually transparent):

[lua]

H-------P--------

[/lua]

Then, you just use one transition to rotate the entire object around the center point.

Take care,

Brent

That sounds great, except every time I tried joints they never worked :frowning: I use MTE and maybe that is why, but probably not? I’ve looked over those documents several times and tried to implement it and after 4 months I just gave up and moved to other stuff. I will look again at it now and see if I can get it working with distance joint.

Hi @Azmar,

Distance joints are relatively simple. This guide shows how to create one, along with every other joint type:

http://docs.coronalabs.com/guide/physics/physicsJoints/index.html

Brent

Yes but when I create it, I use my MTE coordinates and the distance joint looks wonky and does nothing with a random line moving around… after I try to do some type of transition with the distance joint and that always crashes or doesn’t work.

How do I go about making one transition around the player?

Just create two objects… the player and a big long rectangle image. Position both in the same X/Y location, then use a rotation transition on the big rectangle. The important thing is that you define a custom physics body for the rectangle, so that the only part which is sensitive to physics is at one end, where the “hammer head” would be. The other side would just be transparent pixels, and not sensitive to physics.

Brent

Ok I appear to have gotten the distance joint to work correctly and apply force helps it spin the way I want to easily.

Now my last problem is I do:

local distanceJoint = physics.newJoint( "distance", player, game.actionPlayer\_4, player.levelPosX, player.levelPosY, player.levelPosX, player.levelPosY ) distanceJoint.length = 25

Which when I initialize it they both are ontop of each other, when I use the length part it spreads out the 2nd object by 25 pixels to the south. Which works perfectly for my “down attack” when I rotate it, as the axis swings around player still.

My problem is I can only get it to start at the south cuz of the distanceJoint.length, I want the joint to start at the top, right, left, bottom based on the player swing. I’ve to mess around with the starting locations as normal with the hammer but seems to mess up the axis part every time.

For example if I move the starting location of hammer to top of my character, the axis is now spinning at the top in a small circle and not around my guy.

Hi @Azmar,

I would suggest that you manually position the “hammer” part in the location you need it, instead of trying to force the joint to spread it out. That is unpredictable. So if you need it to start above the player, put it at Y -25 of the player’s location, and then create the joint.

Brent

Nice, that helped me solve it properly thanks again for the help! Distance joint did the trick.

Hi @Azmar,

Would a physics joint be another option for this? For example, join the “hammer” object to the player using a distance joint?

Alternatively, how about making the hammer object centrally-oriented around the player, but its actual physics body is defined only to one side (tip) of that object? For example, if “H” is the hammer tip (the physics body) and “P” is the player, but the dashes indicate the entire object size (except that they are visually transparent):

[lua]

H-------P--------

[/lua]

Then, you just use one transition to rotate the entire object around the center point.

Take care,

Brent

That sounds great, except every time I tried joints they never worked :frowning: I use MTE and maybe that is why, but probably not? I’ve looked over those documents several times and tried to implement it and after 4 months I just gave up and moved to other stuff. I will look again at it now and see if I can get it working with distance joint.

Hi @Azmar,

Distance joints are relatively simple. This guide shows how to create one, along with every other joint type:

http://docs.coronalabs.com/guide/physics/physicsJoints/index.html

Brent

Yes but when I create it, I use my MTE coordinates and the distance joint looks wonky and does nothing with a random line moving around… after I try to do some type of transition with the distance joint and that always crashes or doesn’t work.

How do I go about making one transition around the player?

Just create two objects… the player and a big long rectangle image. Position both in the same X/Y location, then use a rotation transition on the big rectangle. The important thing is that you define a custom physics body for the rectangle, so that the only part which is sensitive to physics is at one end, where the “hammer head” would be. The other side would just be transparent pixels, and not sensitive to physics.

Brent

Ok I appear to have gotten the distance joint to work correctly and apply force helps it spin the way I want to easily.

Now my last problem is I do:

local distanceJoint = physics.newJoint( "distance", player, game.actionPlayer\_4, player.levelPosX, player.levelPosY, player.levelPosX, player.levelPosY ) distanceJoint.length = 25

Which when I initialize it they both are ontop of each other, when I use the length part it spreads out the 2nd object by 25 pixels to the south. Which works perfectly for my “down attack” when I rotate it, as the axis swings around player still.

My problem is I can only get it to start at the south cuz of the distanceJoint.length, I want the joint to start at the top, right, left, bottom based on the player swing. I’ve to mess around with the starting locations as normal with the hammer but seems to mess up the axis part every time.

For example if I move the starting location of hammer to top of my character, the axis is now spinning at the top in a small circle and not around my guy.

Hi @Azmar,

I would suggest that you manually position the “hammer” part in the location you need it, instead of trying to force the joint to spread it out. That is unpredictable. So if you need it to start above the player, put it at Y -25 of the player’s location, and then create the joint.

Brent

Nice, that helped me solve it properly thanks again for the help! Distance joint did the trick.