/* * public override void CreateBody(World world, float width, float height) * { * _collisionWidth = width; * _collisionHeight = height; * var carPosition = Position2.ToMeters(); * _body = new Body(world) {BodyType = BodyType.Dynamic, Position = carPosition, AngularDamping = 5}; * * var halfWidth = width/2; * var halfHeight = height/2; * * var frontWheelOffset = (float) CarInfo.FrontWheelOffset/64; * var rearWheelOffset = (float) CarInfo.RearWheelOffset/64; * * //collision detection Fixture * var vertices = new Vertices(4); * vertices.Add(new Vector2(-halfWidth, -halfHeight)); //Top-Left * vertices.Add(new Vector2(halfWidth, -halfHeight)); //Top-Right * vertices.Add(new Vector2(halfWidth, halfHeight)); //Bottom-Right * vertices.Add(new Vector2(-halfWidth, halfHeight)); //Bottom-Left * _shape = new PolygonShape(vertices.ToMeters(), 0.1f); * var fixture = _body.CreateFixture(_shape); //shape, density * fixture.OnCollision += OnCollision; * * //SpriteId Fixture * //var spriteHalfWidth = (float) CarInfo.Sprite.Rectangle.CollisionWidth/2; //ToDo * //var spriteHalfHeight = (float) CarInfo.Sprite.Rectangle.CollisionHeight/2; * var spriteHalfWidth = (float) 52/64/2; * var spriteHalfHeight = (float) 128/64/2; * var spriteVertices = new Vertices(4); * spriteVertices.Add(new Vector2(-spriteHalfWidth, -spriteHalfHeight)); //Top-Left * spriteVertices.Add(new Vector2(spriteHalfWidth, -spriteHalfHeight)); //Top-Right * spriteVertices.Add(new Vector2(spriteHalfWidth, spriteHalfHeight)); //Bottom-Right * spriteVertices.Add(new Vector2(-spriteHalfWidth, spriteHalfHeight)); //Bottom-Left * _spriteShape = new PolygonShape(spriteVertices.ToMeters(), 0.1f); * var spriteFixture = _body.CreateFixture(_spriteShape); * spriteFixture.IsSensor = true; * * float maxForwardSpeed = 300; * float maxBackwardSpeed = -40; * float backWheelMaxDriveForce = 950; * float frontWheelMaxDriveForce = 400; * float backWheelMaxLateralImpulse = 9; * float fronWheelMaxLateralImpulse = 9; * * //back left wheel * var wheelOffsetPosition = new Vector2(halfWidth, rearWheelOffset).ToMeters(); * var wheel = new Wheel(world); * wheel.SetCharacteristics(maxForwardSpeed, maxBackwardSpeed, backWheelMaxDriveForce, backWheelMaxLateralImpulse); * _wheels[0] = wheel; * _backLeftJoint = CreateJoint(_body, wheel.Body, wheelOffsetPosition, world); * * //back right wheel * wheelOffsetPosition = new Vector2(-halfWidth, rearWheelOffset).ToMeters(); * wheel = new Wheel(world); * wheel.SetCharacteristics(maxForwardSpeed, maxBackwardSpeed, backWheelMaxDriveForce, backWheelMaxLateralImpulse); * _wheels[1] = wheel; * _backRightJoint = CreateJoint(_body, wheel.Body, wheelOffsetPosition, world); * * //front left wheel * wheelOffsetPosition = new Vector2(halfWidth, frontWheelOffset).ToMeters(); * wheel = new Wheel(world); * wheel.SetCharacteristics(maxForwardSpeed, maxBackwardSpeed, frontWheelMaxDriveForce, fronWheelMaxLateralImpulse); * _wheels[2] = wheel; * _frontLeftJoint = CreateJoint(_body, wheel.Body, wheelOffsetPosition, world); * * //front right wheel * wheelOffsetPosition = new Vector2(-halfWidth, frontWheelOffset).ToMeters(); * wheel = new Wheel(world); * wheel.SetCharacteristics(maxForwardSpeed, maxBackwardSpeed, frontWheelMaxDriveForce, fronWheelMaxLateralImpulse); * _wheels[3] = wheel; * _frontRightJoint = CreateJoint(_body, wheel.Body, wheelOffsetPosition, world); * }*/ /* * private bool OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact) * { * if (Collided != null) * Collided(this, EventArgs.Empty); * return true; * } * * private static RevoluteJoint CreateJoint(Body carBody, Body wheelBody, Vector2 anchor, World world) * { * var joint = JointFactory.CreateRevoluteJoint(carBody, wheelBody, Vector2.Zero); * joint.LocalAnchorA = anchor; * joint.LimitEnabled = true; * joint.LowerLimit = 0; * joint.UpperLimit = 0; * world.AddJoint(joint); * wheelBody.Position = carBody.Position + anchor; * return joint; * } */ public override void Update(ParticipantInput input, float elapsedTime) { /* * //System.Diagnostics.Debug.WriteLine(_body.Position); * var position = _body.Position.ToBlockUnits(); * Position3 = new Vector3(position.X, position.Y, Position3.Z); * foreach (var wheel in _wheels) * { * wheel.Update(input.Forward, elapsedTime); * } * * //control steering * var lockAngle = MathHelper.ToRadians(angleLock); * var turnSpeedPerSec = MathHelper.ToRadians((angleLock*2)/(lockToLockTime/1000)); * var turnPerTimeSpep = turnSpeedPerSec*elapsedTime; * var desiredAngle = 0f; * if (input.Rotation < 0) * desiredAngle = lockAngle; * else if (input.Rotation > 0) * desiredAngle = -lockAngle; * var angleNow = _frontLeftJoint.JointAngle; * var angleToTurn = desiredAngle - angleNow; * angleToTurn = MathHelper.Clamp(angleToTurn, -turnPerTimeSpep, turnPerTimeSpep); * var newAngle = angleNow + angleToTurn; * _frontLeftJoint.LowerLimit = newAngle; * _frontLeftJoint.UpperLimit = newAngle; * _frontRightJoint.LowerLimit = newAngle; * _frontRightJoint.UpperLimit = newAngle; */ }
public override void Update(ParticipantInput input, float elapsedTime) { float x, y; RotationAngle += input.Rotation; x = (float)(Position3.X + input.Forward * Math.Cos(RotationAngle)); y = (float)(Position3.Y - input.Forward * Math.Sin(RotationAngle)); Position3 = new Vector3(x, y, Position3.Z); }
/// <summary> /// Updates the state of the Object. /// </summary> /// <param name="input">The Input to apply to the Object.</param> /// <param name="elapsedTime">The time occurred since the last Update.</param> public override void Update(ParticipantInput input, float elapsedTime) { if (input.Forward != 0) { if (input.Forward > 0) { Controller.Direction = Walk.Forward; } else { Controller.Direction = Walk.Backward; } } else { Controller.Direction = Walk.None; } if (input.Rotation != 0) { if (input.Rotation > 0) { Controller.Rotate = Rotate.Clockwise; } else { Controller.Rotate = Rotate.Anticlockwise; } } else { Controller.Rotate = Rotate.None; } Position3 = new Vector3(body.Position.X, body.Position.Y, body.Position.Z); // Calculate the angle on Z. // Seen on http://nghiaho.com/?page_id=846 float r21 = body.Orientation.M21; float r11 = body.Orientation.M11; float angle = (float)Math.Atan2(r21, r11); RotationAngle = angle; }
/// <summary> /// Updates the state of the Object /// </summary> /// <param name="input">The Input to apply to the Object</param> /// <param name="elapsedTime">The time occurred since the last Update</param> public abstract void Update(ParticipantInput input, float elapsedTime);
/// <summary> /// Updates the state of the Object. /// </summary> /// <param name="input">The Input to apply to the Object.</param> /// <param name="elapsedTime">The time occurred since the last Update.</param> public override void Update(ParticipantInput input, float elapsedTime) { if (input.Forward != 0) { if(input.Forward > 0) Controller.Direction = Walk.Forward; else Controller.Direction = Walk.Backward; } else { Controller.Direction = Walk.None; } if (input.Rotation != 0) { if (input.Rotation > 0) Controller.Rotate = Rotate.Clockwise; else Controller.Rotate = Rotate.Anticlockwise; } else { Controller.Rotate = Rotate.None; } Position3 = new Vector3(body.Position.X, body.Position.Y, body.Position.Z); // Calculate the angle on Z. // Seen on http://nghiaho.com/?page_id=846 float r21 = body.Orientation.M21; float r11 = body.Orientation.M11; float angle = (float)Math.Atan2(r21, r11); RotationAngle = angle; }
/// <summary> /// Updates the state of the Object. /// </summary> /// <param name="input">The Input to apply to the Object.</param> /// <param name="elapsedTime">The time occurred since the last Update.</param> public abstract void Update(ParticipantInput input, float elapsedTime);
public override void Update(ParticipantInput input, float elapsedTime) { //System.Diagnostics.Debug.WriteLine(_body.Position); var position = _body.Position.ToBlockUnits(); Position3 = new Vector3(position.X, position.Y, Position3.Z); foreach (var wheel in _wheels) { wheel.Update(input.Forward, elapsedTime); } //control steering var lockAngle = MathHelper.ToRadians(angleLock); var turnSpeedPerSec = MathHelper.ToRadians((angleLock*2)/(lockToLockTime/1000)); var turnPerTimeSpep = turnSpeedPerSec*elapsedTime; var desiredAngle = 0f; if (input.Rotation < 0) desiredAngle = lockAngle; else if (input.Rotation > 0) desiredAngle = -lockAngle; var angleNow = _frontLeftJoint.JointAngle; var angleToTurn = desiredAngle - angleNow; angleToTurn = MathHelper.Clamp(angleToTurn, -turnPerTimeSpep, turnPerTimeSpep); var newAngle = angleNow + angleToTurn; _frontLeftJoint.LowerLimit = newAngle; _frontLeftJoint.UpperLimit = newAngle; _frontRightJoint.LowerLimit = newAngle; _frontRightJoint.UpperLimit = newAngle; }