public void Load(Demo demo, PhysicsSimulator physicsSimulator) { _circleBody = new Body[_count]; _circleGeom = new Geom[_count]; _circleBody[0] = BodyFactory.Instance.CreateCircleBody(physicsSimulator, _radius, .1f); _circleBody[0].Position = _startPosition; demo.AddCircleToCanvas(_circleBody[0], _color, _radius); for (int i = 1; i < _count; i++) { _circleBody[i] = BodyFactory.Instance.CreateBody(physicsSimulator, _circleBody[0]); _circleBody[i].Position = Vector2.Lerp(_startPosition, _endPosition, i / (float)(_count - 1)); demo.AddCircleToCanvas(_circleBody[i], _color, _radius); } _circleGeom[0] = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, _circleBody[0], _radius, 10); _circleGeom[0].RestitutionCoefficient = .7f; _circleGeom[0].FrictionCoefficient = .2f; _circleGeom[0].CollisionCategories = _collisionCategories; _circleGeom[0].CollidesWith = _collidesWith; for (int j = 1; j < _count; j++) { _circleGeom[j] = GeomFactory.Instance.CreateGeom(physicsSimulator, _circleBody[j], _circleGeom[0]); } }
public void Load(Demo demo, PhysicsSimulator physicsSimulator) { //Load bodies _spiderBody = BodyFactory.Instance.CreateCircleBody(physicsSimulator, _spiderBodyRadius, 1); _spiderBody.Position = _position; _spiderBody.IsStatic = false; demo.AddCircleToCanvas(_spiderBody, _spiderBodyRadius); _leftUpperLegBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, _upperLegSize.X, _upperLegSize.Y, 1); _leftUpperLegBody.Position = _spiderBody.Position - new Vector2(_spiderBodyRadius, 0) - new Vector2(_upperLegSize.X / 2, 0); demo.AddRectangleToCanvas(_leftUpperLegBody, Colors.White, new Vector2(_upperLegSize.X, _upperLegSize.Y)); _leftLowerLegBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, _lowerLegSize.X, _lowerLegSize.Y, 1); _leftLowerLegBody.Position = _spiderBody.Position - new Vector2(_spiderBodyRadius, 0) - new Vector2(_upperLegSize.X, 0) - new Vector2(_lowerLegSize.X / 2, 0); demo.AddRectangleToCanvas(_leftLowerLegBody, Colors.Red, new Vector2(_lowerLegSize.X, _lowerLegSize.Y)); _rightUpperLegBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, _upperLegSize.X, _upperLegSize.Y, 1); _rightUpperLegBody.Position = _spiderBody.Position + new Vector2(_spiderBodyRadius, 0) + new Vector2(_upperLegSize.X / 2, 0); demo.AddRectangleToCanvas(_rightUpperLegBody, Colors.White, new Vector2(_upperLegSize.X, _upperLegSize.Y)); _rightLowerLegBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, _lowerLegSize.X, _lowerLegSize.Y, 1); _rightLowerLegBody.Position = _spiderBody.Position + new Vector2(_spiderBodyRadius, 0) + new Vector2(_upperLegSize.X, 0) + new Vector2(_lowerLegSize.X / 2, 0); demo.AddRectangleToCanvas(_rightLowerLegBody, Colors.Red, new Vector2(_lowerLegSize.X, _lowerLegSize.Y)); //load geometries _spiderGeom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, _spiderBody, _spiderBodyRadius, 14); _leftUpperLegGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, _leftUpperLegBody, _upperLegSize.X, _upperLegSize.Y); _leftLowerLegGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, _leftLowerLegBody, _lowerLegSize.X, _lowerLegSize.Y); _rightUpperLegGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, _rightUpperLegBody, _upperLegSize.X, _upperLegSize.Y); _rightLowerLegGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, _rightLowerLegBody, _lowerLegSize.X, _lowerLegSize.Y); _spiderGeom.CollisionGroup = _collisionGroup; _leftUpperLegGeom.CollisionGroup = _collisionGroup; _leftLowerLegGeom.CollisionGroup = _collisionGroup; _rightUpperLegGeom.CollisionGroup = _collisionGroup; _rightLowerLegGeom.CollisionGroup = _collisionGroup; //load joints JointFactory.Instance.CreateRevoluteJoint(physicsSimulator, _spiderBody, _leftUpperLegBody, _spiderBody.Position - new Vector2(_spiderBodyRadius, 0)); _leftShoulderAngleJoint = JointFactory.Instance.CreateAngleJoint(physicsSimulator, _spiderBody, _leftUpperLegBody); _leftShoulderAngleJoint.TargetAngle = -.4f; _leftShoulderAngleJoint.MaxImpulse = 300; JointFactory.Instance.CreateRevoluteJoint(physicsSimulator, _spiderBody, _rightUpperLegBody, _spiderBody.Position + new Vector2(_spiderBodyRadius, 0)); _rightShoulderAngleJoint = JointFactory.Instance.CreateAngleJoint(physicsSimulator, _spiderBody, _rightUpperLegBody); _rightShoulderAngleJoint.TargetAngle = .4f; _leftShoulderAngleJoint.MaxImpulse = 300; JointFactory.Instance.CreateRevoluteJoint(physicsSimulator, _leftUpperLegBody, _leftLowerLegBody, _spiderBody.Position - new Vector2(_spiderBodyRadius, 0) - new Vector2(_upperLegSize.X, 0)); _leftKneeAngleJoint = JointFactory.Instance.CreateAngleJoint(physicsSimulator, _leftUpperLegBody, _leftLowerLegBody); _leftKneeAngleJoint.TargetAngle = -_kneeTargetAngle; _leftKneeAngleJoint.MaxImpulse = 300; JointFactory.Instance.CreateRevoluteJoint(physicsSimulator, _rightUpperLegBody, _rightLowerLegBody, _spiderBody.Position + new Vector2(_spiderBodyRadius, 0) + new Vector2(_upperLegSize.X, 0)); _rightKneeAngleJoint = JointFactory.Instance.CreateAngleJoint(physicsSimulator, _rightUpperLegBody, _rightLowerLegBody); _rightKneeAngleJoint.TargetAngle = _kneeTargetAngle; _rightKneeAngleJoint.MaxImpulse = 300; }
public void Load(Demo demo, PhysicsSimulator physicsSimulator) { int radius; if (_attachPoint == 0 | _attachPoint == 2) { radius = _rectangleHeight; } else { radius = _rectangleWidth; } //body is created as rectangle so that it has the moment of inertia closer to the final shape of the object. _angleSpringleverBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, _rectangleWidth, _rectangleHeight, 1f); demo.AddRectangleToCanvas(_angleSpringleverBody, Colors.White, new Vector2(_rectangleWidth, _rectangleHeight)); _rectangleGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, _angleSpringleverBody, _rectangleWidth, _rectangleHeight); _rectangleGeom.FrictionCoefficient = .5f; _rectangleGeom.CollisionGroup = _collisionGroup; Vector2 offset = Vector2.Zero; switch (_attachPoint) { case 0: { offset = new Vector2(-_rectangleWidth / 2f, 0); //offset to rectangle to left break; } case 1: { offset = new Vector2(0, -_rectangleHeight / 2f); //offset to rectangle to top break; } case 2: { offset = new Vector2(_rectangleWidth / 2f, 0); //offset to rectangle to right break; } case 3: { offset = new Vector2(0, _rectangleHeight / 2f); //offset to rectangle to bottom break; } } _angleSpringleverBody.Position = _position - offset; Ellipse circle = demo.AddCircleToCanvas(null, Colors.White, 20); Demo.CenterAround(circle, _position); _circleGeom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, _angleSpringleverBody, radius, 20, offset, 0); _circleGeom.FrictionCoefficient = .5f; _circleGeom.CollisionGroup = _collisionGroup; JointFactory.Instance.CreateFixedRevoluteJoint(physicsSimulator, _angleSpringleverBody, _position); SpringFactory.Instance.CreateFixedAngleSpring(physicsSimulator, _angleSpringleverBody, _springConstant, _dampningConstant); }