public Bullet(World world, Car _car, Texture2D _dummyTexture) { car = _car; dummyTexture = _dummyTexture; bulletPhysicsObject= BodyFactory.CreateRectangle(world, 0.1f, 0.1f, 1f, Vector2.Zero); bulletPhysicsObject.BodyType = BodyType.Dynamic; bulletPhysicsObject.IsSensor = true; bulletPhysicsObject.IsBullet = true; isGoing = false; bulletPhysicsObject.IgnoreCollisionWith(car._compound); bulletPhysicsObject.CollisionCategories = Category.Cat20; bulletPhysicsObject.OnCollision += bullet_OnCollision; graphics = GameServices.GetService<GraphicsDeviceManager>(); pencilTexture = GameServices.GetService<ContentManager>().Load<Texture2D>("Images/pencilTextire"); laserVertices = new VertexPositionColorTexture[6]; laserVertices[0].Color = car.mColor; laserVertices[1].Color = car.mColor; laserVertices[2].Color = car.mColor; laserVertices[3].Color = car.mColor; laserVertices[4].Color = car.mColor; laserVertices[5].Color = car.mColor; laserSound = GameServices.GetService<ContentManager>().Load<SoundEffect>("Sounds/laserorribiledimmerda"); laserSoundInstance=laserSound.CreateInstance(); }
/// <summary> /// Player constructor /// </summary> /// <param name="a_world">World reference</param> /// <param name="a_texture">Texture reference</param> /// <param name="a_startPosition">Starting Position</param> /// <param name="a_size">Sizing</param> /// <param name="a_bodyType">Type of body? Dynamic, Static or Kenetic?</param> /// <param name="mass">Weight of mass</param> public Player(World a_world, Texture2D a_texture, Vector2 a_startPosition, Vector2 a_size, float mass) { Vector2 bodySize = new Vector2(a_size.X, a_size.Y); //Assigning arguments m_texture = a_texture; m_size = bodySize; m_colour = Color.White; m_speed = 10.0f; m_canJump = false; // Initalising camera Sprite m_cameraFocus = new Sprite(a_world, a_texture, bodySize, mass / 2.0f, a_startPosition); m_cameraFocus.Position = a_startPosition + new Vector2(0, 20); m_cameraFocus.m_body.BodyType = BodyType.Static; // Initalising body m_body = BodyFactory.CreateCapsule ( a_world, ConvertUnits.ToSimUnits(a_size.Y), ConvertUnits.ToSimUnits(a_size.X / 2), 5.0f, ConvertUnits.ToSimUnits(a_startPosition), 0.0f, BodyType.Dynamic ); Vector2 collisionBodyPosition = new Vector2( a_startPosition.X, a_startPosition.Y + m_texture.Height); // Collision body m_collisionBody = BodyFactory.CreateRectangle ( a_world, ConvertUnits.ToSimUnits(a_size.X), ConvertUnits.ToSimUnits(5), 5.0f, ConvertUnits.ToSimUnits(collisionBodyPosition), 0.0f, BodyType.Dynamic ); m_collisionBody.Friction = 5.0f; //Collisions m_collisionBody.CollidesWith = Category.All; m_collisionBody.IgnoreCollisionWith(m_body); m_cameraFocus.m_body.CollidesWith = Category.None; // Create a joint to keep the torso upright JointFactory.CreateAngleJoint(a_world, m_body, m_cameraFocus.m_body); JointFactory.CreateAngleJoint(a_world, m_body, m_collisionBody); //If collided, jumping possible m_collisionBody.OnCollision += Body_OnCollision; m_collisionBody.OnSeparation += Body_OnRelease; }
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; }
/// <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; }
public void Initialize(Texture2D sprite, ProjectileType type, Player plr, Body b, Texture2D shadow) { B = b; alreadyCollidedOnceOrMore = false; shade = shadow; foreach (var item in Game1.projectiles) { B.IgnoreCollisionWith(item.B); } B.OnCollision += new OnCollisionEventHandler(B_OnCollision); uint[] d = new uint[shade.Width * shade.Height]; shade.GetData(d); Vertices v = PolygonTools.CreatePolygon(d, shade.Width, true); Vector2[] a = v.ToArray(); h = ShadowHull.CreateConvex(ref a); if (plr.direction == 1) dir = 1; lock (Game1.projectileLock) Game1.lightingEngine.Hulls.Add(h); // B.ApplyLinearImpulse(new Vector2(50, -10)); if (plr.direction == 1) { B.Position = new Vector2(-plr.Position.X, plr.Position.Y + 1); B.ApplyTorque(-250f); B.LinearVelocity = new Vector2(-65, 7); } else { B.Position = new Vector2(-plr.Position.X, plr.Position.Y + 1); B.ApplyTorque(250f); B.LinearVelocity = new Vector2(65, 7); } Active = true; position = ConvertUnits.ToDisplayUnits(-plr.Position); texture = sprite; Type = type; Pelaaja = plr; hitBox.Width = 17; hitBox.Height = 17; hitBox.X = (int)position.X; hitBox.Y = (int)position.Y; }
//Torso private void CreateBody(World world, Vector2 position) { Color ragdollColor = Color.Turquoise; DebugMaterial matHead = new DebugMaterial(MaterialType.Face) { Color = ragdollColor, Scale = 2f }; DebugMaterial matShirt = new DebugMaterial(MaterialType.Tiles) { Color = ragdollColor, Scale = 2f }; DebugMaterial matShorts = new DebugMaterial(MaterialType.Tiles) { Color = ragdollColor, Scale = 2f }; DebugMaterial matSkin = new DebugMaterial(MaterialType.Blank) { Color = ragdollColor, Scale = 2f }; //Head _head = new Body(world); FixtureFactory.AttachCircle(.9f, LegDensity, _head, matHead); _head.BodyType = BodyType.Dynamic; _head.AngularDamping = LimbAngularDamping; //_head.Body.Mass = 2; _head.Position = position; //Body _body = new Body(world); FixtureFactory.AttachRectangle(2, 4, LegDensity, Vector2.Zero, _body, matShirt); _body.BodyType = BodyType.Dynamic; //_body.Mass = 2; _body.Position = position + new Vector2(0, -3); //Left Arm _lowerLeftArm = new Body(world); FixtureFactory.AttachRectangle(.7f, elbowDistance, ArmDensity, Vector2.Zero, _lowerLeftArm, matSkin); _lowerLeftArm.BodyType = BodyType.Dynamic; _lowerLeftArm.AngularDamping = LimbAngularDamping; //_lowerLeftArm.Mass = 2; _lowerLeftArm.Friction = .2f; _lowerLeftArm.Rotation = -1.4f; _lowerLeftArm.Position = position + new Vector2(-4, -2.2f); _upperLeftArm = new Body(world); FixtureFactory.AttachRectangle(.7f, elbowDistance, ArmDensity, Vector2.Zero, _upperLeftArm, matShirt); _upperLeftArm.BodyType = BodyType.Dynamic; _upperLeftArm.AngularDamping = LimbAngularDamping; // _upperLeftArm.Body.Mass = 2; _upperLeftArm.Rotation = -1.4f; _upperLeftArm.Position = position + new Vector2(-2, -1.8f); //Right Arm _lowerRightArm = new Body(world); FixtureFactory.AttachRectangle(.7f, elbowDistance, ArmDensity, Vector2.Zero, _lowerRightArm, matSkin); _lowerRightArm.BodyType = BodyType.Dynamic; _lowerRightArm.AngularDamping = LimbAngularDamping; //_lowerRightArm.Mass = 2; _lowerRightArm.Friction = .3f; _lowerRightArm.Rotation = 1.4f; _lowerRightArm.Position = position + new Vector2(4, -2.2f); _upperRightArm = new Body(world); FixtureFactory.AttachRectangle(.7f, elbowDistance, ArmDensity, Vector2.Zero, _upperRightArm, matShirt); _upperRightArm.BodyType = BodyType.Dynamic; _upperRightArm.AngularDamping = LimbAngularDamping; //_upperRightArm.Body.Mass = 2; _upperRightArm.Rotation = 1.4f; _upperRightArm.Position = position + new Vector2(2, -1.8f); //Left Leg _lowerLeftLeg = new Body(world); FixtureFactory.AttachRectangle(.7f, 2f, LegDensity, Vector2.Zero, _lowerLeftLeg, matSkin); _lowerLeftLeg.BodyType = BodyType.Dynamic; _lowerLeftLeg.AngularDamping = LimbAngularDamping; // _lowerLeftLeg.Mass = 2; _lowerLeftLeg.Friction = .5f; _lowerLeftLeg.Position = position + new Vector2(-0.6f, -8); _upperLeftLeg = new Body(world); FixtureFactory.AttachRectangle(.7f, 2f, LegDensity, Vector2.Zero, _upperLeftLeg, matShorts); _upperLeftLeg.BodyType = BodyType.Dynamic; _upperLeftLeg.AngularDamping = LimbAngularDamping; _upperLeftLeg.Mass = 2; _upperLeftLeg.Position = position + new Vector2(-0.6f, -6); //Right Leg _lowerRightLeg = new Body(world); FixtureFactory.AttachRectangle(.7f, 2f, LegDensity, Vector2.Zero, _lowerRightLeg, matSkin); _lowerRightLeg.BodyType = BodyType.Dynamic; _lowerRightLeg.AngularDamping = LimbAngularDamping; //_lowerRightLeg.Mass = 2; _lowerRightLeg.Friction = .5f; _lowerRightLeg.Position = position + new Vector2(0.6f, -8); _upperRightLeg = new Body(world); FixtureFactory.AttachRectangle(.7f, 2f, LegDensity, Vector2.Zero, _upperRightLeg, matShorts); _upperRightLeg.BodyType = BodyType.Dynamic; _upperRightLeg.AngularDamping = LimbAngularDamping; //_upperRightLeg.Mass = 2; _upperRightLeg.Position = position + new Vector2(0.6f, -6); _upperRightArm.IgnoreCollisionWith(_head); _upperLeftArm.IgnoreCollisionWith(_head); AllBodies = new List<Body>(); AllBodies.Add(_body); AllBodies.Add(_head); AllBodies.Add(_lowerLeftArm); AllBodies.Add(_lowerLeftLeg); AllBodies.Add(_lowerRightArm); AllBodies.Add(_lowerRightLeg); AllBodies.Add(_upperLeftArm); AllBodies.Add(_upperLeftLeg); AllBodies.Add(_upperRightArm); AllBodies.Add(_upperRightLeg); }