 A Flash Developer Resource Site

# Thread: Verlet Angle Constraint Troubles

1. ## Verlet Angle Constraint Troubles

Okay.... I've hit a snag with my Verlet angle constraint code. I've managed to get a decent minimum angle constraint writen up and working; however, it seems that a small problem comparing a theta using atan2 might prevent me from continuing for a while until I manage to solve the problem.

It seems that my code has a flaw and leaves off a quarter of the unit circle. In this quarter of the circle, Flash believes that it is part of the minimum constraint and solves it as such. Therefore, I believe that it may be a problem of bad mathematics that is at hand. The range is from 1-270 degrees, with another quarter circle 1-89.99999999.... I do use an absolute value, and sadly, that is a part of the problem.

If anything, I would like to stay away from the seemingly simple add a 90 degrees (that would modify the way the unit circle would look and would make my code seem confusing), and, while it seems a simple fix, it doesn't work. Here's what I have so far (Please be nice to my code ) :
code:
```       var apL = this.apList;
var angL = this.angList;
var l0 = apL;
var l1 = apL;
var l2 = apL;
var MINA = angL;
var MAXA = angL;
var dx1 = l0.x - l1.x;
var dy1 = l0.y - l1.y;
var dx2 = l1.x - l2.x;
var dy2 = l1.y - l2.y;
var ang1 = Math.atan2(-dy1, dx1)*(180/Math.PI);
var ang2 = Math.atan2(dy2, dx2)*(180/Math.PI);
var ANG = Math.abs(ang1-ang2);
trace (ANG);
if (ANG < MINA){
ang2 += (MINA-ang2)-ang1;
ang2 /= 180/Math.PI;
l2.x = (l2.rList*Math.cos(ang2)) + l1.x;
l2.y = (l2.rList*Math.sin(ang2)) + l1.y;
} else if (ANG > MAXA) {
ang2 += (ang2-MAXA)-ang1;
ang2 /= 180/Math.PI;
l2.x = (l2.rList*Math.cos(ang2)) + l1.x;
l2.y = (l2.rList*Math.sin(ang2)) + l1.y;
}```

This is run once every 9 milliseconds inside of a function. I did try appending Flade's Angular Constraint code into my own, but seemed to fail miserably.... Any help would be GREATLY appreciated.

EDIT:// Added the .fla I've been using to test this darned code. Hope this helps you more visual people.  Reply With Quote

2. YAY!!!!!! RESOLVED!!!! WEEEEEE!!! You have no idea how elated I am to present... the working angle constraint code!
code:
```       var apL = this.apList;
var angL = this.angList;
var l0 = apL;
var l1 = apL;
var l2 = apL;
var MINA = angL;
var MAXA = angL;
var dx1 = l0.x-l1.x;
var dy1 = l0.y-l1.y;
var dx2 = l2.x-l1.x;
var dy2 = l2.y-l1.y;
var ang1 = Math.atan2(dy1, dx1);
var ang2 = Math.atan2(dy2, dx2);
var ANG = (ang1-ang2)*(180/Math.PI);
if (ANG < 0) {
ANG += 360;
}
//trace(ANG);
if (ANG<MINA) {
var dAng = (MINA-ANG)/180/Math.PI;
l2.x = l2.rList*Math.cos(ang2-dAng)+l1.x;
l2.y = l2.rList*Math.sin(ang2-dAng)+l1.y;
}else if (ANG>MAXA) {
var dAng = (MAXA-ANG)/180/Math.PI;
l2.x = l2.rList*Math.cos(ang2-dAng)+l1.x;
l2.y = l2.rList*Math.sin(ang2-dAng)+l1.y;
}```

*head explodes with ecstacy, releasing thousands of delicious swedish fish into the air.*  Reply With Quote

3. I tried the code and it works, exept for the fact that sometimes the constrained limb spin wildly. Why does it do this?  Reply With Quote

4. First off, let me apologize about not getting back to you on your other post.

The constrained limb glitch is caused because of two different things (if you're using this code). One: the center of the angle hasn't been corrected for the new placement of the points. There's code at the bottom for you. It's simply the average of the coordinates of the resolved angle minus the average of the old coordinates. Two: sometimes in collision with an object, the angle of the limb over-extends and causes a flip-flop. Let's say that a constricted arm collides with the ground and causes the hand to go up past the shoulder. This tells the angle code to go from 0-90 degrees to 270-360 degrees instantaneously and then the maximum angle 'if' statement is run instead of the minimum, causing massive jitter. I never found a workaround for that.

This is the code I finally came up with for my angular constraints. The variable OANG I left in just in case you wanted to attempt a workaround. I tried fixing it by doing something like: OANG set to 350 -> New angle is 10 -> if (OANG > 270 && ANG < 90) -> add 360 to new angle. That didn't work. So here it is:
code:
```var apL = this.apList;
var l0 = apL;
var l1 = apL;
var l2 = apL;
var MINA = apL;
var MAXA = apL;
var dx1 = l0.x - l1.x;
var dy1 = l0.y - l1.y;
var dx2 = l2.x - l1.x;
var dy2 = l2.y - l1.y;
var octx = (l0.x + l1.x + l2.x) / 3;
var octy = (l0.y + l1.y + l2.y) / 3;
var dist1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
var dist2 = Math.sqrt(dx2 * dx2 + dy2 * dy2);
var ang1 = Math.atan2(dy1, dx1);
var ang2 = Math.atan2(dy2, dx2);
var ANG = (ang1 - ang2) * (180 / Math.PI);
if (ANG < 0) {
ANG += 360;
}
if (ANG < MINA) {
for (var j = 0; j < NUMIT; j++) {
var dAng = (MINA - ANG) / 180 / Math.PI * 2;
l0.x = (dist1 * Math.cos(ang1 + dAng) + l1.x);
l0.y = (dist1 * Math.sin(ang1 + dAng) + l1.y);
l2.x = (dist2 * Math.cos(ang2 - dAng) + l1.x);
l2.y = (dist2 * Math.sin(ang2 - dAng) + l1.y);
var nctx = (l0.x + l1.x + l2.x) / 3;
var ncty = (l0.y + l1.y + l2.y) / 3;
var ctx = nctx - octx;
var cty = ncty - octy;
l0.x -= ctx;
l0.y -= cty;
l1.x -= ctx;
l1.y -= cty;
l2.x -= ctx;
l2.y -= cty;
this.OANG = ANG;
}
} else if (ANG > MAXA) {
for (var j = 0; j < NUMIT; j++) {
var dAng = (MAXA - ANG) / 180 / Math.PI * 2;
l0.x = (dist1 * Math.cos(ang1 + dAng) + l1.x);
l0.y = (dist1 * Math.sin(ang1 + dAng) + l1.y);
l2.x = (dist2 * Math.cos(ang2 - dAng) + l1.x);
l2.y = (dist2 * Math.sin(ang2 - dAng) + l1.y);
var nctx = (l0.x + l1.x + l2.x) / 3;
var ncty = (l0.y + l1.y + l2.y) / 3;
var ctx = nctx - octx;
var cty = ncty - octy;
l0.x -= ctx;
l0.y -= cty;
l1.x -= ctx;
l1.y -= cty;
l2.x -= ctx;
l2.y -= cty;
this.OANG = ANG;
}
};
```  Reply With Quote

5. hmmm... now it isn't doing anything...  Reply With Quote

6. wow........ I got the code to run but my ragdoll goes crazy and jumps everywhere, everytime it runs the code.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 