Pivot Joints and apparent elasticity

126 replies [Last post]

Replies

ignis075
User offline. Last seen 2 years 22 weeks ago. Offline
Joined: 4 Oct 2010

(yawn)... hit a nerve? Believe whatever you want. :P

We all eagerly await your glorious return. We'll even roll out the red carpet for you, fanfare and all the rest.

walter
User offline. Last seen 28 weeks 4 days ago. Offline
Staff
Joined: 22 Jun 2009

Easy fellas! Let's take it down a notch...

@XenonBL, how about the following: I'll send you my ported ragdoll project. At the same time, can you send me some code that shows how you are applying forces on bodies (preferably with similar property values)?

From what I can tell from non-Corona forums on Box2D with folks facing this issue, there is a stability issue. And typically, one small tweak in the parameter can make all the difference.

I should clarify that it's not the force of the touch joint per se, but the amount of momentum that the touch joint gives the ragdoll *prior* to it colliding against another body (e.g. a wall). So if you are generating a tremendous impulse, then the collision could cause bad things to happen.

I'm playing with the Cocos2D ragdoll (https://github.com/YannickL/Box2D-Examples), and there's joint separation there too for this very reason. The culprit is the same as what I was alluding to in my ragdoll project. If the touch joint is allowed to impart a huge amount of momentum, then there will be instability in the joints during a collision.

Cranking down the maxForce, implicitly cranks down the amount of momentum the ragdoll can have prior to a collision --- and it's minimizing that momentum makes it go away in *both* the Cocos2D and Corona projects.

Hopefully it makes more sense to you (and the rest of the folks here) why I'm so emphatic about trying to get some sort of test code from you, so I can understand how you are imparting momentum on your ragdoll *prior* to collision.

In the mean time, if folks have other *working* ragdoll examples, e.g. Flash or Cocos2D, that they recommend, please let me know. Thanks!

ignis075
User offline. Last seen 2 years 22 weeks ago. Offline
Joined: 4 Oct 2010

I apologize to @iqsoup, @XenonBL, @walter, @fava, and everybody who has a legitimate reason for posting in this thread. Certain "tones" (mine included in this particular case) have no place in these typically mature, reasonable forums. Again, sorry about that. :)

Brent Sorrentino
Ignis Design

horacebury
User offline. Last seen 18 hours 32 min ago. Offline
Joined: 17 Aug 2010

Sorry @XenonBL, but in my honest opinion, it's pretty noob-sounding to start asking the co-founder and CEO of a software company not to require some form of working test case.

I'm not saying you are a noob or haven't been in the biz for many years, but your point is noob sounding. That coming from someone who, also IMHO, is usually quite well written and thought out is a bit jarring to see here.

This thread has gotten quite nuts, but as has been pointed out - well, you don't see Steve Ballmer making posts like these.

This next bit takes your post as a case in point but is general, because many similar things have been said in this and other threads...

You've said, "As far as I know I'm constructing my ragdoll..." - I think the point is that as far as anyone knows you are. Ansca doesn't know any different unless you send them code. I have personally made this mistake and found it frustrating, but if you can't reduce a single piece of code down to its core problem area then you can't demonstrate the problem sufficiently. The developer's argument breaks down if it appears that their entire program is broken. In proper modular coding the root problem code should be fairly simple to extract and believe me, I understand that might involve a lot of code. Enterprise web farms anyone?

iqSoup
User offline. Last seen 3 years 49 weeks ago. Offline
Joined: 28 Sep 2011

@Walter

Thank you for giving this issue some attention--its a big deal to a lot of us and its great to have some help.

Here's a spinets of code that demonstrate one of the problems I've had with joints. I have many more but maybe we should take it one at a time. Maybe the solution to the below code will help me solve some of the other joint problems I've been facing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
--borders
local borderLeft = display.newRect(_W*0.01, _H*(-1), 5, _H*4)
borderLeft.alpha = 0
physics.addBody(borderLeft, "static", {bounce=0.8})
local borderRight = display.newRect(_W*0.99, _H*(-1), 5, _H*4)
physics.addBody(borderRight, "static", {bounce=0.8})
borderRight.alpha = 0
 
--shape
local randomShape = display.newImage(temp.imgpath)              
shapegroup:insert (randomShape)
physics.addBody(randomShape)
randomShape.x = math.random(_W*0.05,_W*0.95)
randomShape.y = _H*(-0.5)
randomShape:applyLinearImpulse(math.random(0,1),math.random(0,0.5),randomShape.x, randomShape.y)
                
randomShape.shapeText = display.newImage("label.png")
physics.addBody(randomShape.shapeText, { isSensor = true } )
randomShape.shapeText.x = randomShape.x
randomShape.shapeText.y = randomShape.y
randomShape.myJoint1 = physics.newJoint( "weld", randomShape, randomShape.shapeText, randomShape.x, randomShape.y)
                
--movement
randomShape:rotate( math.random(1, 360) )

Hopefully that's enough to demonstrate what I'm doing. When the "shape" collides with a border there is a large bounce between the shape and its label even though the two objects are supposed to be welded together. I also have a rag-doll sample that has some major problems but it might take me a while to extract it from the rest of its code. I'm hoping a solution to the above problem might help me solve my ragdoll problems on my own. Again thank you for giving this issue your attention.

@IGNIS

Let me explain.

It is not Ansca's job to write code specifically for me, but it 100% their job to deliver the product they promised. If I promised my clients one thing and sent them something else I wouldn't get paid and you bet I'd lose the client. Any demands they made of me would be more than justified.

Don't get me wrong, I quite like Corona and I've found Ansca staff to usually be more than friendly and helpful. Corona does most things very well. It even does most physics stuff fairly well--things like gravity, impulses...stuff like that. But one thing Corona just can't do is joints.

One of my apps just used a simple weld joint and even that wouldn't work properly. The welded objects would bump against a wall and there would be a large, noticeable bounce between the two objects. I had another app that I put a lot of work into--spent about 2 weeks created all the graphics I would end up needing. Before starting the project I looked into all the different joints and assumed it would be a walk in the park. This game relied heavily on using ragdoll-like objects. I, like everyone else whose tried, ran into some MAJOR problems. I spent who knows how long searching for a solution. I looked online and found little help. Eventually I found this forum and saw there were many others facing the exact same problem. I was frustrated that Ansca seemed to be ignoring the issue. I eventually gave up and scrapped that app. I wasted countless hours, days, even weeks because I thought Corona could do what it is supposed to do.

I hope that helps explain why this is such a major issue to me and why I've been frustrated by Ansca's lack of attention. I'm very happy to see Walter get into this.

horacebury
User offline. Last seen 18 hours 32 min ago. Offline
Joined: 17 Aug 2010

Actually, I have in the past personally requested that I send my entire app to Ansca with as much security as they could provide to ensure that it didn't get misused when I had a big, all-application-encompassing problem bug. This was because of a bug report I submitted which turned out not to be a Corona bug. Ansca took my whole game code and, I'm confident, deleted it after sending me an email explaining what I was doing wrong in my code. This, after I submitted a reduced test case which, unknownst to me at the time, was nothing to do with the actual problem. They also solved my problem and suggested overall improvements.

I'm not suggesting we all start doing that, but I am saying that Ansca's team have been open to it and I think that says a lot.

horacebury
User offline. Last seen 18 hours 32 min ago. Offline
Joined: 17 Aug 2010

@iqsoup, can you send me (or post here) the code sample you used to demonstrate the breaking weld joint? I've not seen that problem and I use loads of them. Maybe I can help?

walter
User offline. Last seen 28 weeks 4 days ago. Offline
Staff
Joined: 22 Jun 2009

Okay, so another long post, but I have found a solution that's available in today's Corona!

(1) Separation-free Ragdolls on other Box2D implementations.

First off, if anyone has a solid working example in Cocos2D, please send it my way.

As I mentioned previously, I have seen problems in alternative Box2D implementations including Cocos2D *and* Flash. I saw some assertion that this was unique to Corona, but I have not found a solid sample. Again, if there is one, I'd love to see what's different about that setup. I have a feeling any working sample will do what I discuss in #3 below.

(2) Separation due to limitations of Box2D

Second, I found some relevant data about instability (i.e. separation) from 2 authoritative sources.

* From iforce2d (who's clearly an expert on Box2D, given the number of excellent discussions on the site): You may get instability problems if you try to pull a heavy body with a lighter body.
* From Erin Catto: Chains of bodies connected by joints may stretch if a lighter body is supporting a heavier body. For example, a wrecking ball connect to a chain of light weight bodies may not be stable. Stability degrades as the mass ratio passes 10:1.

These both describe the sources of instability that I'm seeing. When you drag the ragdoll by the bicep, I'm able to get some instability. And this makes sense b/c you've got over 10 times the mass attached to it, i.e. the rest of the body attached to the relatively ligher mass of the bicep.

(3) A Solution!

Finally, I found a solution. Erin Catto (creator of Box2d) suggests on this thread (http://www.box2d.org/forum/viewtopic.php?f=3&t=1499) to turn off continuous physics for some guy having problems with pivot (prismatic) joints. So on a lark I tried turning it on:

1
physics.setContinuous( false )

Voila! This seems to eliminate the instability and joint separation! If I crank up the maxForce on the touch joints (which seemed to exacerbate separation/instability under continuous physics), everything's still fine. I also confirmed this by modifying the Cocos2D sample project as well. Again, no separation.

Okay, so what's the catch? The catch is that continuous physics prevents objects from "tunnelling" where an object (e.g. a bullet) moves so quickly that it appears to move through a thin wall. Continuous physics prevents this, but there are apparently instability issues with joints. Turning it off makes the instability go away. However, you'll want to make your static bodies extra thick to prevent tunneling effects

In my case, when I turned off continuous physics, I'd move the ragdoll so quickly, that half the ragdoll would be on one side of the wall and half would be on the other side --- all because my walls were only 1 pixel thick! Making the walls 100 pixels thick prevented the tunneling.

So the trade-off seems to be: tunneling vs instability on joints.

Happy coding!

iqSoup
User offline. Last seen 3 years 49 weeks ago. Offline
Joined: 28 Sep 2011

Thank you VERY much Walter. This seems to be the answer I've been looking for. I will try this out tomorrow and hopefully this can breath new life into a previously abandoned project. Thanks again!

horacebury
User offline. Last seen 18 hours 32 min ago. Offline
Joined: 17 Aug 2010

Wow, Walter, that is some seriously hard core debugging going on! Thank you and go get sleep, please!

huntamh
User offline. Last seen 5 years 10 weeks ago. Offline
Joined: 8 Jul 2011

Walter,

Here's a brilliant working example of box2D joints (albeit not a ragdoll).

(The 2nd example): http://www.emanueleferonato.com/2009/04/06/two-ways-to-make-box2d-cars/

Hope that helps.

Adam

Pixin
User offline. Last seen 28 weeks 6 days ago. Offline
Joined: 21 Feb 2011

WOW!! BRAVO WALTER!!! That did the trick! There's no separation of the pivot joints! Unbelievable after all this time. I was going to sleep after I saw your post but just tossed and turned and finally got up and added the line. In 10 seconds...voila indeed! The movement is slow but I probably just have to tweak some of the other parameters.

THANK YOU SO MUCH!!

Pixin
User offline. Last seen 28 weeks 6 days ago. Offline
Joined: 21 Feb 2011

Ok, so a little testing, there's a little separation here and there, the higher the gravity the more it happens, and it's running slow, but I'll keep tweaking.

horacebury
User offline. Last seen 18 hours 32 min ago. Offline
Joined: 17 Aug 2010

Hi Walter,

Thanks again, but I do have a question: setContinuous does not appear to be in the API documentation - is it or am I more tired than I thought?

Thanks,

Matt.

walter
User offline. Last seen 28 weeks 4 days ago. Offline
Staff
Joined: 22 Jun 2009

D'oh! You're right, it's not there.

It will be in the new API docs. We hope to push that out next week. I did check, and the API has been available since last September, so should be available in the current public release.

@fava, my suggestions are to make sure:

(1) the two bodies overlap where you place your joint
(2) the densities "make sense" (you'll have to do some tweaking)

BTW, setting continuous to false turns off additional calculations, so it should run faster than if you have continuous turned on.

Pixin
User offline. Last seen 28 weeks 6 days ago. Offline
Joined: 21 Feb 2011

Thanks again Walter, yes it's working just fine! I'm so happy! ;)

guausoft
User offline. Last seen 4 years 17 weeks ago. Offline
Joined: 7 Apr 2012

Okay Walter:

Thank you very much for your solution. Ansca should give you a raise. You have been the only person at Ansca who have heard its customers.
I think that Ansca should have solved this problem at the beginning, not twenty months later than the first post.
As I published a video demonstrating the problem, this is a new video demonstrating the solution: http://www.youtube.com/watch?v=9HSJp8GJzCM

Congratulations, Walter.

optionniko
User offline. Last seen 3 years 23 weeks ago. Offline
Joined: 19 Jul 2011

Walter, I'm so glad you finally cracked this one!

There is one more thing and i know this thread was about pivot joints and elasticity but i've got
a really big problem with pivot joint and wheel joint. I spent almost half a year trying to get this
to work. Submited the bug about 3 times...no replay... I asked on the forum several times..no one
could provide me the solution.

I also want to add that setting limits for WheelJoints don't work at all.!

Can you just give it a quick look and see what could be wrong.

Here's the video I made, explaining the problem and the sample code below.
http://www.youtube.com/watch?v=y1Jb14pUrwU

Thank you very much for your time and help!!!

display.setStatusBar( display.HiddenStatusBar )
local _W=display.contentWidth;
local _H=display.contentHeight;

local physics = require ("physics")
physics.start ()
physics.setScale( 60 )
physics.setDrawMode ("hybrid")

----OBJECTS
local legs = display.newRect(0,0,80,140);
legs.x, legs.y = _W*0.5, 860;

local body = display.newRect(0,0,160,300)
body.x, body.y = _W*0.5, 695;

----------------------------------------------------------
------------------------- ANCHORES -----------------------
---------------------------------------------------------- 
local anchor = display.newCircle(510, 500, 4); --Anchor above body's body

----------------------------------------------------------
------------------- PHYSICS BODIES -----------------------
----------------------------------------------------------
physics.addBody( anchor, "static");
physics.addBody( body, "dinamic" );
body.isSleepingAllowed = false;
physics.addBody( legs, "static");

----------------------------------------------------------
------- PHYSICS JOINTS BETWEEN ANCHOR AND BODIES --------
----------------------------------------------------------
local bodyWheelJoint = physics.newJoint("wheel", body, anchor, anchor.x, anchor.y, 1, 1);
bodyWheelJoint.isLimitEnabled = true;
bodyWheelJoint:setLimits( 100, 100 )

local bodyPivotJoint = physics.newJoint("pivot", legs, body, body.x, body.y+102 );
bodyPivotJoint.isLimitEnabled = true;
bodyPivotJoint:setRotationLimits( -30, 30 );

----------------------------------------------------------
------------------- ROTATE UPPER BODY --------------------
----------------------------------------------------------
local function rotatebody(e)
	local body = e.target;
	local phase = e.phase;
	local stage = display.getCurrentStage();
	
	if (phase == "began") then
		stage:setFocus(body, e.id);
		body.tempJoint = physics.newJoint("touch", body, e.x, e.y)
		body.tempJoint.dampingRatio = 0.8
		body.tempJoint.frequency = 400
		body.tempJoint.maxForce = 400
	
	elseif (phase == "moved") then
		body.tempJoint:setTarget(e.x, e.y);
		
	elseif (phase == "ended" or phase == "cancelled") then	
		stage:setFocus(body, nil);
		body.tempJoint:removeSelf();
	end
	return true
end
body:addEventListener("touch", rotatebody);
walter
User offline. Last seen 28 weeks 4 days ago. Offline
Staff
Joined: 22 Jun 2009

@optionniko, please start a new thread. I'd like folks to focus on the pivot issues here. I did take a quick look and can provide some recommendations on the new thread that you start. Thanks!

Stephen Lewis
User offline. Last seen 1 year 18 weeks ago. Offline
Joined: 24 Sep 2010

Wow, I think turning continuous physics off might have done the trick! I'm still testing but so far I haven't seen any ragdoll dismemberment in my game. I did notice one instance where tunneling was an issue so I'll have to work around that, but I'm pretty sure I know what to do about it.

This thread reminds me of my wife's labor with our first child; Long and drawn out, no one got any sleep for days, lots of screaming near the end, but with a wonderful result!

Thanks again for looking at this Walter. Be sure to let us know if you find any other undocumented Corona features ;)

optionniko
User offline. Last seen 3 years 23 weeks ago. Offline
Joined: 19 Jul 2011

Hy Walter!

Here is the link to new thread...http://developer.anscamobile.com/forum/2012/06/08/wheel-joint-problems

I really appreciate your help!
Nick

Ernest
User offline. Last seen 30 weeks 2 days ago. Offline
Joined: 4 Sep 2011

Wow, I've just re-read this thread since it had become dormant last year and just want to say, my very first post on this forum was about the exact same topic, lol

Here was my silly little experiments with physics when I first started using Corona. Since that time, I'm on my way to making a game based on what was done here (a rag doll arm for hitting flies)

https://developer.anscamobile.com/forum/2011/10/18/controlremove-joint-elasticty

Anyways since then I've resolved the problem despite my rather unnatural application of corona physics. I still use touch joints and I tween them, but I'm always ensuring I never tween them past the point where the joints start separating.

It would be nice to alter the physics meter-pixel ratio as my physics objects are in ranges of 30-100 in pixel length. My current partial rag doll is a skyscraper. Having to scale things down will cause a lot of headaches when instantiating my world which I've pretty much completed. I look forward to being able to change the touch joint mass, but I'm happy with the results I'm currently getting despite this persistent problem. Sad to hear so many ppl out there are having problems, I too would like to see physics take a higher priority over something like a level editor.

A good house needs a solid foundation, software development is no different, I'm pleased with the priorities on bug fixing that Ansca has been taking. Hopefully the physics issue will be resolved before the Summer is over. fingers crossed

mike470
User offline. Last seen 2 years 24 weeks ago. Offline
Joined: 29 Jun 2012

Please please *please* put this physics.setContinuous trick in the docs - specifically the docs for pivots. It took me a month until I finally found this thread and now my stuff works as it should.

walter
User offline. Last seen 28 weeks 4 days ago. Offline
Staff
Joined: 22 Jun 2009
mike470
User offline. Last seen 2 years 24 weeks ago. Offline
Joined: 29 Jun 2012

walter, I mean in big, friendly letters in the physics-joints docs, at the top, put something that basically explains that if you're having problems with elasticity in the joints, use the physics.setContinuous(false). Would save some developers hours of fiddling with stuff, at least.

iqSoup
User offline. Last seen 3 years 49 weeks ago. Offline
Joined: 28 Sep 2011

Agreed. This needs to be very very clearly put into the joint docs in jumbo sized font.

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.