A random place of a body on a ground (Circle on a segment)
Imagine you want to find a random place for bodies on a ground (I think at worms on the great game Worms …), like that :

There are some difficulties:
- All segments aren’t horizontal
- Bodies’ centres aren’t always at the bottom of the shape (Box2D body for example) …
But Maths could help us.
We’ll work on this schema :

Let’s see the code. It’s using only Matrix, Point and Trigonometry :
//Distance Between Point A and M (A and B with the ratio you want : _nPosition) var nMidNorme:Number = Point.distance(new Point(cur1.x, cur1.y),new Point(cur2.x, cur2.y))*_nPosition; //Position of Point B with Point A for origin var pCur2:Point = new Point(cur2.x, cur2.y).subtract(new Point(cur1.x, cur1.y)); //Distance between A and D with Pythagoras theorem var nHypo:Number = Math.sqrt(Math.pow(nMidNorme,2) + Math.pow(_nRadius,2)); //Angle between AM (or AB) with hypothenuse in triangle AMD. //MD is the opposite angle or the radius of the circle. var nAngle:Number = Math.atan2(_nRadius, nMidNorme); //Angle between AC and AB -> CÂB var nCurAngle:Number = (Math.atan2(pCur2.y, pCur2.x)); //New matrix to handle place of the body var pMatrix:Matrix = new Matrix(); //We translate the point with the distance AD (along the AC segment) pMatrix.translate(nHypo, 0); //Angle between AC and AD -> CÂD (CÂB + BÂD) var nTotalAngle:Number = -nAngle + nCurAngle; //Rotate point of CÂD angle pMatrix.rotate(nTotalAngle);
Now you can place your circle:
_mcCircle.x = cur1.x + pMatrix.tx, _mcCircle.y = cur1.y + pMatrix.ty;
An exemple in swf:
[...] Ce billet était mentionné sur Twitter par Germain LECOURTOIS, Germain LECOURTOIS. Germain LECOURTOIS a dit: @actionsnippet Congratulations @piXelero – After Bounce in Circle by @makc3d, Circle on a segment ? http://bit.ly/18rj6W :p [...]