private void CreatePlayerPhysicsObjects(Vector2 gameWorldPosition) { MainFixture = FixtureFactory.CreateRectangle(Engine.Physics.World, 0.5f, 0.5f, 1); MainFixture.CollisionFilter.CollisionCategories = (Category)(Global.CollisionCategories.Player); Bodies.Add(MainFixture.Body); MainFixture.Body.Position = Engine.Physics.PositionToPhysicsWorld(gameWorldPosition); MainFixture.Body.BodyType = BodyType.Dynamic; MainFixture.Body.SleepingAllowed = false; WheelFixture = FixtureFactory.CreateCircle(Engine.Physics.World, 0.3f, 1.0f); WheelFixture.CollisionFilter.CollisionCategories = (Category)(Global.CollisionCategories.Player); Bodies.Add(WheelFixture.Body); WheelFixture.Body.Position = MainFixture.Body.Position + new Vector2(0.0f, 0.6f); WheelFixture.Body.BodyType = BodyType.Dynamic; WheelFixture.Body.SleepingAllowed = false; WheelFixture.Friction = 0.5f; playerFAJ = JointFactory.CreateFixedAngleJoint(Engine.Physics.World, MainFixture.Body); playerFAJ.BodyB = WheelFixture.Body; wheelMotorRevJoint = JointFactory.CreateRevoluteJoint(MainFixture.Body, WheelFixture.Body, Vector2.Zero); wheelMotorRevJoint.MaxMotorTorque = 10.0f; wheelMotorRevJoint.MotorEnabled = true; Engine.Physics.World.AddJoint(wheelMotorRevJoint); }
/// <summary> /// Creates a fixed angle joint. /// </summary> /// <param name="world">The world.</param> /// <param name="body">The body.</param> /// <returns></returns> public static FixedAngleJoint CreateFixedAngleJoint(World world, Body body) { FixedAngleJoint angleJoint = new FixedAngleJoint(body); world.AddJoint(angleJoint); return angleJoint; }
private AngleJointTest() { BodyFactory.CreateEdge(World, new Vector2(-40, 0), new Vector2(40, 0)); Body fA = BodyFactory.CreateRectangle(World, 4, 4, 1, new Vector2(-5, 4)); fA.BodyType = BodyType.Dynamic; Body fB = BodyFactory.CreateRectangle(World, 4, 4, 1, new Vector2(5, 4)); fB.BodyType = BodyType.Dynamic; AngleJoint joint = new AngleJoint(fA, fB); joint.TargetAngle = (float)Math.PI / 2; World.AddJoint(joint); Body fC = BodyFactory.CreateRectangle(World, 4, 4, 1, new Vector2(10, 4)); fC.BodyType = BodyType.Dynamic; FixedAngleJoint fixedJoint = new FixedAngleJoint(fC); fixedJoint.TargetAngle = (float)Math.PI / 3; World.AddJoint(fixedJoint); }
public override void Initialize() { const float width = 1f; const float height = 0.5f; physics.bodys = new Body[1]{new Body(physics.world)}; mainBody.BodyType = BodyType.Dynamic; FixtureFactory.AttachRectangle(width, height, 1f, Vector2.Zero, mainBody); FixtureFactory.AttachCircle(0.25f, 0f, mainBody, new Vector2(-0.40f, -0.1f)).Friction = 0f; FixtureFactory.AttachCircle(0.25f, 0f, mainBody, new Vector2(0.40f, -0.1f)).Friction = 0f; WheelL = FixtureFactory.AttachCircle(0.2f, 0f, mainBody, new Vector2(-0.4f, 0.25f)); WheelL.Friction = wheelsFriction; WheelL.AfterCollision += WheelAfterContactHandler; WheelL.OnCollision += WheelContactHandler; WheelL.OnSeparation += WheelSeparateHandler; WheelR = FixtureFactory.AttachCircle(0.2f, 0f, mainBody, new Vector2(0.4f, 0.25f)); WheelR.Friction = wheelsFriction; WheelR.AfterCollision += WheelAfterContactHandler; WheelR.OnCollision += WheelContactHandler; WheelR.OnSeparation += WheelSeparateHandler; StabilizeJoint = JointFactory.CreateFixedAngleJoint(physics.world, mainBody); mainBody.Restitution = 0f; mainBody.CollisionCategories = Category.Cat2; mainBody.CollidesWith = Category.Cat1 | Category.Cat3; base.Initialize(); }
private void Spawn(Utility.Timer timer) { if (nextPiece == null) { nextPiece = getRandomTetrisPiece(); } currentPiece = nextPiece; currentPieceMaxLen = Math.Max(currentPiece.shape.GetLength(0), currentPiece.shape.GetLength(1)); currentPiece.body.Position = getSpawnPosition(); currentPieceCollide = new OnCollisionEventHandler(currentPieceCollision); currentPieceSeparate = new OnSeparationEventHandler(currentPieceSeparation); currentPiece.body.OnCollision += currentPieceCollide; currentPiece.body.OnSeparation += currentPieceSeparate; currentPieceRotation = JointFactory.CreateFixedAngleJoint(_world, currentPiece.body); pieces.Add(currentPiece); activePieces.Add(currentPiece); currentCheat = null; --countdownToCheat; if (countdownToCheat < 0) { currentCheat = new TetrisPiece(_world, tetrisTextures[2], tetrisShapes[2], currentPiece.body.WorldCenter + new Vector2(1, -2)); currentCheat.body.FixedRotation = true; currentCheat.body.Rotation = (float)Math.PI / 2; currentCheat.body.OnCollision += currentPieceCollide; currentCheat.body.OnSeparation += currentPieceSeparate; JointFactory.CreateRevoluteJoint(_world, currentCheat.body, currentPiece.body, currentPiece.body.LocalCenter); pieces.Add(currentCheat); activePieces.Add(currentCheat); countdownToCheat = 5; } currentPiece.body.Enabled = true; if(currentCheat!=null) currentCheat.body.Enabled = true; nextPiece = getRandomTetrisPiece(); //Debug.Print("Spawn new tetris piece at: {0}, {1}", currentPiece.body.Position.X, currentPiece.body.Position.Y); }
private void dropCurrentPiece() { if (currentPiece != null) { if (isCurrentPieceBlocked()) { currentPiece.body.Position = getSpawnPosition(); } else { currentPiece.body.LinearVelocity = Vector2.Zero; currentPiece.body.ResetDynamics(); currentPiece.body.OnCollision -= currentPieceCollide; currentPiece.body.OnSeparation -= currentPieceSeparate; if (currentCheat != null) { currentCheat.body.OnCollision -= currentPieceCollide; currentCheat.body.OnSeparation -= currentPieceSeparate; } if (currentPieceRotation != null) { _world.RemoveJoint(currentPieceRotation); currentPieceRotation = null; } currentPiece = null; currentPieceCollide = null; currentCheat = null; Game1.Timers.Create(SPAWN_TIME, false, Spawn); } } }
protected override void SetUpPhysics(World world, Vector2 position, float width, float height, float mass) { //Create a fixture with a body almost the size of the entire object //but with the bottom part cut off. float upperBodyHeight = height - (width / 2); //fixture = FixtureFactory.AttachRectangle((float)ConvertUnits.ToSimUnits(width), (float)ConvertUnits.ToSimUnits(upperBodyHeight), mass / 2, Vector2.Zero, body); //CreateRectangle(world, (float)ConvertUnits.ToSimUnits(width), (float)ConvertUnits.ToSimUnits(upperBodyHeight), mass / 2); body = BodyFactory.CreateRectangle(world, (float)ConvertUnits.ToSimUnits(width), (float)ConvertUnits.ToSimUnits(upperBodyHeight), mass / 2); //fixture.Body; body.BodyType = BodyType.Dynamic; body.Restitution = 0.0f; body.Friction = 0.5f; //also shift it up a tiny bit to keey the new object's center correct body.Position = ConvertUnits.ToSimUnits(position - (Vector2.UnitY * (width / 4))); centerOffset = position.Y - (float)ConvertUnits.ToDisplayUnits(body.Position.Y); //remember the offset from the center for drawing //fixture = FixtureFactory.AttachRectangle((float)ConvertUnits.ToSimUnits(width), (float)ConvertUnits.ToSimUnits(upperBodyHeight), mass / 2, Vector2.Zero, body); //Now let's make sure our upperbody is always facing up. fixedAngleJoint = JointFactory.CreateFixedAngleJoint(world, body); //Create a wheel as wide as the whole object //wheel = FixtureFactory.AttachCircle((float)ConvertUnits.ToSimUnits(width / 2), mass / 2, body); //CreateCircle(world, (float)ConvertUnits.ToSimUnits(width / 2), mass / 2); wheel = BodyFactory.CreateCircle(world, (float)ConvertUnits.ToSimUnits(width / 2), mass / 2); //And position its center at the bottom of the upper body wheel.Position = body.Position + ConvertUnits.ToSimUnits(Vector2.UnitY * (upperBodyHeight / 2)); wheel.BodyType = BodyType.Dynamic; wheel.Restitution = 0.0f; wheel.Friction = 0.5f; //These two bodies together are width wide and height high :) //So lets connect them together motor = JointFactory.CreateRevoluteJoint(world, body, wheel, Vector2.Zero); motor.MotorEnabled = true; motor.MaxMotorTorque = 4f; //set this higher for some more juice motor.MotorSpeed = 0; //Make sure the two fixtures don't collide with each other wheel.IgnoreCollisionWith(body); body.IgnoreCollisionWith(wheel); //Set the friction of the wheel to float.MaxValue for fast stopping/starting //or set it higher to make the character slip. //wheel.Friction = float.MaxValue; wheel.Friction = 9f; }
public override void OnInit() { //Najpierw komponenty fizyczne, by dało się pobrać prawidłowy atrybut prędkości. var pObj = new PhysicalObject(true); var bBox = new BoundingBox(new OpenTK.Vector2(this.Description.Width, this.Description.Height), 10f); this.Components.Add(pObj); this.Components.Add(bBox); this.Health_ = this.Attributes.GetOrCreate<int>("Health"); this.Position_ = this.Attributes.GetOrCreate<Vector2>("Position"); this.Health = (int)this.Description.Health; //Ustawiamy właściwości ciała tak, by poruszało się po naszej myśli pObj.Body.Mass = 10; pObj.Body.LinearDamping = 0.2f; //pObj.Body.FixedRotation = true; pObj.Body.UserData = this; //Ustawiamy maskę kolizji tak by kolidowało tylko z innymi graczami pObj.Body.SetCollisionCategories((Category)((int)Category.Cat1 << (int)this.Owner.Type)); //Koliduje ze wszystkim z wyłączeniem jednostek tego samego gracza i zasobami. pObj.Body.SetCollidesWith(Category.All & ~pObj.Body.GetCollisionCategories() & ~Category.Cat10 & ~((Category)((int)Category.Cat11 << (int)this.Owner.Type))); //Ustawianie stałego konta jednostek this.FixedAngle = new FixedAngleJoint(pObj.Body); this.FixedAngle.BiasFactor = 0.08f; ClashEngine.NET.PhysicsManager.Instance.World.AddJoint(this.FixedAngle); var velMult = this.Attributes.GetOrCreate<float>("VelocityMultiplier"); //I zdarzenia kolizji pomiędzy jednostkami pObj.Body.SetCollisionEvent((fixtureA, fixtureB, contact) => { if (fixtureB.Body.UserData is IUnit && this.CollisionWithUnit != null) { this.CollisionWithUnit(this, fixtureB.Body.UserData as IUnit); return false; } else if (fixtureB.Body.UserData is IPlayerEntity && this.CollisionWithPlayer != null) { this.CollisionWithPlayer(this, (fixtureB.Body.UserData as IPlayerEntity).Player); } else if (fixtureB.Body.UserData is IResourceOnMap && this.CollisionWithResource != null) { //Taka iteracja zapobiega zbieraniu już zebranych zasobów var delegates = this.CollisionWithResource.GetInvocationList(); foreach (CollisionWithResourceEventHandler d in delegates) { if (d(this, fixtureB.Body.UserData as IResourceOnMap)) { (fixtureB.Body.UserData as IResourceOnMap).Gather(); break; } } } else if (fixtureB.Body.UserData is IMap) //Ustawiamy kąt nachylenia dla postaci { if (fixtureA.UserData == null || (velMult.Value >= 0.0 && (int)fixtureA.UserData < (int)fixtureB.UserData) || (velMult.Value < 0.0 && (int)fixtureA.UserData > (int)fixtureB.UserData)) { FarseerPhysics.Common.FixedArray2<Microsoft.Xna.Framework.Vector2> points; Microsoft.Xna.Framework.Vector2 normal; contact.GetWorldManifold(out normal, out points); var dot = Microsoft.Xna.Framework.Vector2.Dot(new Microsoft.Xna.Framework.Vector2(0, -1), normal); var angle = (float)Math.Acos(dot) * Math.Sign(normal.X); this.FixedAngle.TargetAngle = (float)angle; fixtureA.UserData = fixtureB.UserData; } } return true; }); foreach (var component in this.Description.Components) { this.Components.Add(component.Create()); } }
public void InitializePhysics(World world) { Body body = new Body(world); Vertices verts = new Vertices(); verts.Add(new Vector2(ConvertUnits.ToSimUnits(Bounds.X), ConvertUnits.ToSimUnits(Bounds.Y + Bounds.Height / 4f))); verts.Add(new Vector2(ConvertUnits.ToSimUnits(Bounds.X + Bounds.Width/2f), ConvertUnits.ToSimUnits(Bounds.Y+Bounds.Height/4f))); verts.Add(new Vector2(ConvertUnits.ToSimUnits(Bounds.X + Bounds.Width/2f), ConvertUnits.ToSimUnits(Bounds.Y + Bounds.Height- Bounds.Height / 4f))); verts.Add(new Vector2(ConvertUnits.ToSimUnits(Bounds.X), ConvertUnits.ToSimUnits(Bounds.Y + Bounds.Height- Bounds.Height/4f))); body.CreateFixture(new PolygonShape(verts, 1)); Bodies = new List<Body>(); body.BodyType = BodyType.Dynamic; body.Mass = Settings.PLAYERMASS; body.Friction = 0.3f; Bodies = new List<Body>(); Bodies.Add(body); AngJoint = JointFactory.CreateFixedAngleJoint(world, body); AngJoint.CollideConnected = false; AngJoint.Softness = 0f; AngJoint.TargetAngle = 0f; }
public JointComponent(FixedAngleJoint joint) { m_Joint = joint; }
/// <summary> /// Attached body and wheel together to make a character /// </summary> /// <param name="world">The world that the character is being added to</param> /// <param name="position">The position in the world that the character is being added to</param> /// <param name="mass">The mass of the character</param> protected override void SetUpPhysics(World world, Vector2 position, float mass) { float upperBodyHeight = size.Y - (size.X / 2); // Create upper body body = BodyFactory.CreateRectangle(world, (float)size.X, (float)upperBodyHeight, mass / 2); body.BodyType = BodyType.Dynamic; body.Restitution = 0.1f; body.Friction = 0.5f; body.Position = ConvertUnits.ToSimUnits(position) - (Vector2.UnitY * (size.X / 4)); centerOffset = position.Y - (float)ConvertUnits.ToDisplayUnits(body.Position.Y); fixedAngleJoint = JointFactory.CreateFixedAngleJoint(world, body); // Create lower body wheel = BodyFactory.CreateCircle(world, (float)size.X / 2, mass / 2); wheel.Position = body.Position + (Vector2.UnitY * (upperBodyHeight / 2)); wheel.BodyType = BodyType.Dynamic; wheel.Restitution = 0.1f; // Connecting bodies motor = JointFactory.CreateRevoluteJoint(world, body, wheel, Vector2.Zero); motor.MotorEnabled = true; motor.MaxMotorTorque = 1000f; motor.MotorSpeed = 0; wheel.IgnoreCollisionWith(body); body.IgnoreCollisionWith(wheel); wheel.Friction = float.MaxValue; }
private void initializeJoints() { fRevoluteJoint = JointFactory.CreateFixedRevoluteJoint(scene.World, Body, Body.LocalCenter, Body.Position); fRevoluteJoint.MaxMotorTorque = 20f; fRevoluteJoint.MotorSpeed = 0f; fRevoluteJoint.MotorTorque = 10f; fRevoluteJoint.MotorEnabled = false; fAngleJoint = JointFactory.CreateFixedAngleJoint(scene.World, Body); fAngleJoint.TargetAngle = 0f; fAngleJoint.MaxImpulse = 5f; fAngleJoint.BiasFactor = 2f; fAngleJoint.Softness = .72f; }
public static Joint CopyJoint(Joint joint, Body bodyA, Body bodyB, World world) { Joint newJoint = null; switch (joint.JointType) { case JointType.Angle: newJoint = JointFactory.CreateAngleJoint(world, bodyA, bodyB); break; case JointType.Distance: newJoint = new DistanceJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter); break; case JointType.FixedAngle: newJoint = new FixedAngleJoint(bodyA); break; case JointType.FixedDistance: newJoint = new FixedDistanceJoint(bodyA, bodyA.WorldCenter, Vector2.Zero); break; case JointType.FixedFriction: newJoint = new FixedFrictionJoint(bodyA, bodyA.WorldCenter); break; case JointType.FixedPrismatic: var fpJoint = joint as FixedPrismaticJoint; var fpAxis = fpJoint.LocalXAxis1; newJoint = new FixedPrismaticJoint(bodyA, bodyA.WorldCenter, fpAxis); break; case JointType.FixedRevolute: newJoint = new FixedRevoluteJoint(bodyA, bodyA.WorldCenter, Vector2.Zero); break; case JointType.Friction: newJoint = new FrictionJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter); break; case JointType.Line: var lineJoint = joint as LineJoint; var axis = lineJoint.LocalXAxis; newJoint = new LineJoint(bodyA, bodyB, bodyA.WorldCenter, axis); break; case JointType.Prismatic: var pJoint = joint as PrismaticJoint; var pAxis = pJoint.LocalXAxis1; newJoint = new PrismaticJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter, pAxis); ((PrismaticJoint)newJoint).LimitEnabled = pJoint.LimitEnabled; ((PrismaticJoint)newJoint).MotorEnabled = pJoint.MotorEnabled; ((PrismaticJoint)newJoint).MaxMotorForce = pJoint.MaxMotorForce; ((PrismaticJoint)newJoint).MotorSpeed = pJoint.MotorSpeed; ((PrismaticJoint)newJoint).LowerLimit = pJoint.LowerLimit; ((PrismaticJoint)newJoint).UpperLimit = pJoint.UpperLimit; ((PrismaticJoint)newJoint).ReferenceAngle = pJoint.ReferenceAngle; ((PrismaticJoint)newJoint).LocalXAxis1 = pJoint.LocalXAxis1; break; case JointType.Pulley: var pulleyJoint = joint as PulleyJoint; var ratio = pulleyJoint.Ratio; newJoint = new PulleyJoint(bodyA, bodyB, Vector2.Zero, Vector2.Zero, bodyA.WorldCenter, bodyB.WorldCenter, ratio); break; case JointType.Revolute: newJoint = new RevoluteJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter); break; case JointType.Slider: var sliderJoint = joint as SliderJoint; var minLength = sliderJoint.MinLength; var maxLength = sliderJoint.MaxLength; newJoint = new SliderJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter, minLength, maxLength); break; case JointType.Weld: newJoint = new WeldJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter); break; } var data = new FarseerJointUserData(); data.BodyAName = ((FarseerJointUserData) joint.UserData).BodyAName; data.BodyBName = ((FarseerJointUserData) joint.UserData).BodyBName; joint.UserData = data; return newJoint; }