/// <summary> /// Constructs a new constraint which restricts the linear and angular motion between two entities. /// This constructs the internal constraints, but does not configure them. Before using a constraint constructed in this manner, /// ensure that its active constituent constraints are properly configured. The entire group as well as all internal constraints are initially inactive (IsActive = false). /// </summary> public WeldJoint() { IsActive = false; BallSocketJoint = new BallSocketJoint(); NoRotationJoint = new NoRotationJoint(); Add(BallSocketJoint); Add(NoRotationJoint); }
/// <summary> /// Constructs a new constraint which restricts the linear and angular motion between two entities. /// </summary> /// <param name="connectionA">First entity of the constraint pair.</param> /// <param name="connectionB">Second entity of the constraint pair.</param> /// <param name="anchor">The location of the weld.</param> public WeldJoint(Entity connectionA, Entity connectionB, Vector3 anchor) { if (connectionA == null) connectionA = TwoEntityConstraint.WorldEntity; if (connectionB == null) connectionB = TwoEntityConstraint.WorldEntity; BallSocketJoint = new BallSocketJoint(connectionA, connectionB, anchor); NoRotationJoint = new NoRotationJoint(connectionA, connectionB); Add(BallSocketJoint); Add(NoRotationJoint); }
/// <summary> /// Constructs a new constraint which restricts the linear and angular motion between two entities. /// </summary> /// <param name="connectionA">First entity of the constraint pair.</param> /// <param name="connectionB">Second entity of the constraint pair.</param> public WeldJoint(Entity connectionA, Entity connectionB) { if (connectionA == null) connectionA = TwoEntityConstraint.WorldEntity; if (connectionB == null) connectionB = TwoEntityConstraint.WorldEntity; BallSocketJoint = new BallSocketJoint(connectionA, connectionB, (connectionA.position + connectionB.position) * .5f); NoRotationJoint = new NoRotationJoint(connectionA, connectionB); Add(BallSocketJoint); Add(NoRotationJoint); }
/// <summary> /// Constructs a new constraint which restricts the linear and angular motion between two entities. /// Uses the average of the two entity positions for the anchor. /// </summary> /// <param name="connectionA">First entity of the constraint pair.</param> /// <param name="connectionB">Second entity of the constraint pair.</param> public WeldJoint(Entity connectionA, Entity connectionB) { if( connectionA == null ) connectionA = TwoEntityConstraint.WorldEntity; if( connectionB == null ) connectionB = TwoEntityConstraint.WorldEntity; Vector3 anchor; GetAnchorGuess( connectionA, connectionB, out anchor ); BallSocketJoint = new BallSocketJoint( connectionA, connectionB, ref anchor ); NoRotationJoint = new NoRotationJoint( connectionA, connectionB ); Add( BallSocketJoint ); Add( NoRotationJoint ); }
/// <summary> /// Constructs a new constraint which restricts two degrees of linear freedom and all three degrees of angular freedom. /// This constructs the internal constraints, but does not configure them. Before using a constraint constructed in this manner, /// ensure that its active constituent constraints are properly configured. The entire group as well as all internal constraints are initially inactive (IsActive = false). /// </summary> public PrismaticJoint() { IsActive = false; PointOnLineJoint = new PointOnLineJoint(); AngularJoint = new NoRotationJoint(); Limit = new LinearAxisLimit(); Motor = new LinearAxisMotor(); Add(PointOnLineJoint); Add(AngularJoint); Add(Limit); Add(Motor); }
/// <summary> /// Constructs a new constraint which restricts two degrees of linear freedom and all three degrees of angular freedom. /// </summary> /// <param name="connectionA">First entity of the constraint pair.</param> /// <param name="connectionB">Second entity of the constraint pair.</param> /// <param name="lineAnchor">Location of the anchor for the line to be attached to connectionA in world space.</param> /// <param name="lineDirection">Axis in world space to be attached to connectionA along which connectionB can move.</param> /// <param name="pointAnchor">Location of the anchor for the point to be attached to connectionB in world space.</param> public PrismaticJoint(Entity connectionA, Entity connectionB, Vector3 lineAnchor, Vector3 lineDirection, Vector3 pointAnchor) { if (connectionA == null) connectionA = TwoEntityConstraint.WorldEntity; if (connectionB == null) connectionB = TwoEntityConstraint.WorldEntity; PointOnLineJoint = new PointOnLineJoint(connectionA, connectionB, lineAnchor, lineDirection, pointAnchor); AngularJoint = new NoRotationJoint(connectionA, connectionB); Limit = new LinearAxisLimit(connectionA, connectionB, lineAnchor, pointAnchor, lineDirection, 0, 0); Motor = new LinearAxisMotor(connectionA, connectionB, lineAnchor, pointAnchor, lineDirection); Limit.IsActive = false; Motor.IsActive = false; Add(PointOnLineJoint); Add(AngularJoint); Add(Limit); Add(Motor); }
public void GrabEntity() { if (_grabbing) return; if (!grabbedEntity.IsDynamic) return; EntityTag eT = lastTouched.Tag as EntityTag; bsj = new BallSocketJoint(grabbedEntity, grabPairCollector, grabPairCollector.Position); bsj.SpringSettings.StiffnessConstant = 2000000.0f; nrj = new NoRotationJoint(grabbedEntity, grabPairCollector); Space.Add(bsj); Space.Add(nrj); _grabbing = true; eT.ParentComponent.Mesh.Highlight = true; }
void BuildStick(Vector3 position) { //Set up a bone chain. float fullLength = 20; int linkCount = 20; float linkLength = fullLength / linkCount; float linkRadius = linkLength * 0.2f; var previousBoneEntity = new Cylinder(position, linkLength, linkRadius, 100); var previousBone = new Bone(previousBoneEntity.Position, previousBoneEntity.Orientation, previousBoneEntity.Radius, previousBoneEntity.Height); bones.Add(new BoneRelationship(previousBone, previousBoneEntity)); Space.Add(previousBoneEntity); for (int i = 1; i < linkCount; i++) { var boneEntity = new Cylinder(previousBone.Position + new Vector3(0, linkLength, 0), linkLength, linkRadius, 100); var bone = new Bone(boneEntity.Position, boneEntity.Orientation, boneEntity.Radius, boneEntity.Height); bones.Add(new BoneRelationship(bone, boneEntity)); Space.Add(boneEntity); //Make a relationship between the two bones and entities. CollisionRules.AddRule(previousBoneEntity, boneEntity, CollisionRule.NoBroadPhase); Vector3 anchor = (previousBoneEntity.Position + boneEntity.Position) / 2; var dynamicsBallSocketJoint = new BallSocketJoint(previousBoneEntity, boneEntity, anchor); var dynamicsAngularFriction = new NoRotationJoint(previousBoneEntity, boneEntity); Space.Add(dynamicsBallSocketJoint); Space.Add(dynamicsAngularFriction); var ikBallSocketJoint = new IKBallSocketJoint(previousBone, bone, anchor); //(the joint is auto-added to the bones; no solver-add is needed) var ikAngularJoint = new IKAngularJoint(previousBone, bone); joints.Add(ikBallSocketJoint); joints.Add(ikAngularJoint); previousBone = bone; previousBoneEntity = boneEntity; } }
public override void Bind(Entity result, Main main, bool creating = false) { Property<float> maxForce = result.GetOrMakeProperty<float>("MaxForce", true, 150.0f); Property<float> damping = result.GetOrMakeProperty<float>("Damping", true, 1.5f); Property<float> stiffness = result.GetOrMakeProperty<float>("Stiffness", true, 15.0f); NoRotationJoint joint = null; EntityMover mover = null; Action setMaxForce = delegate() { if (mover != null) { if (maxForce > 0.001f) mover.LinearMotor.Settings.MaximumForce = maxForce * result.Get<DynamicMap>().PhysicsEntity.Mass; else mover.LinearMotor.Settings.MaximumForce = float.MaxValue; } }; result.Add(new NotifyBinding(setMaxForce, maxForce)); result.Add(new CommandBinding(result.Get<DynamicMap>().PhysicsUpdated, setMaxForce)); Action setDamping = delegate() { if (mover != null && damping != 0) mover.LinearMotor.Settings.Servo.SpringSettings.DampingConstant = damping; }; result.Add(new NotifyBinding(setDamping, damping)); Action setStiffness = delegate() { if (mover != null && stiffness != 0) mover.LinearMotor.Settings.Servo.SpringSettings.StiffnessConstant = stiffness * result.Get<DynamicMap>().PhysicsEntity.Mass; }; result.Add(new NotifyBinding(setStiffness, stiffness)); result.Add(new CommandBinding(result.Get<DynamicMap>().PhysicsUpdated, setStiffness)); Func<BEPUphysics.Entities.Entity, BEPUphysics.Entities.Entity, Vector3, Vector3, Vector3, ISpaceObject> createJoint = delegate(BEPUphysics.Entities.Entity entity1, BEPUphysics.Entities.Entity entity2, Vector3 pos, Vector3 direction, Vector3 anchor) { joint = new NoRotationJoint(entity1, entity2); if (mover != null && mover.Space != null) main.Space.Remove(mover); mover = new EntityMover(entity1); main.Space.Add(mover); setMaxForce(); setDamping(); setStiffness(); return joint; }; JointFactory.Bind(result, main, createJoint, true, creating); result.Add(new CommandBinding(result.Get<DynamicMap>().OnSuspended, delegate() { if (mover != null && mover.Space != null) main.Space.Remove(mover); })); result.Add(new CommandBinding(result.Get<DynamicMap>().OnResumed, delegate() { if (mover != null && mover.Space == null) main.Space.Add(mover); })); result.Add(new CommandBinding(result.Delete, delegate() { if (mover != null && mover.Space != null) { main.Space.Remove(mover); mover = null; } })); Property<Entity.Handle> parent = result.GetOrMakeProperty<Entity.Handle>("Parent"); Property<Map.Coordinate> coord = result.GetOrMakeProperty<Map.Coordinate>("Coord"); Updater updater = null; updater = new Updater { delegate(float dt) { Entity parentEntity = parent.Value.Target; if (parentEntity != null && parentEntity.Active) mover.TargetPosition = parentEntity.Get<Map>().GetAbsolutePosition(coord) + new Vector3(0, -0.01f, 0); else { updater.Delete.Execute(); parent.Value = null; if (mover != null && mover.Space != null) { main.Space.Remove(mover); mover = null; } } } }; result.Add(updater); }