Skip to content

Commit

Permalink
Physics WIP + Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiale.dev authored and Hiale.dev committed Aug 6, 2013
1 parent 72df2d1 commit 58a863a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 27 additions & 5 deletions GTA2.NET/GTA2.NET/GTA2.NET Core/Logic/Car.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,30 @@
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics.Dynamics.Joints;
using FarseerPhysics.Factories;
using Hiale.GTA2NET.Core.Helper;
using Microsoft.Xna.Framework;

namespace Hiale.GTA2NET.Core.Logic
{
/// <summary>
/// Represents a car.
/// See Car physics.png:
/// Steering mechanism joints - The front wheels rotate on these points. Angle is locked from -35 degrees to +35 degrees (subject to change).
/// Front wheels - Are needed to determine in which angle the car should drive; not displayed to the player, only exists as physics engine object
/// Sprite area (whole gray area) - Any sprite data is drawn on this layer. Usually bigger than the collsions box, because it can also contain door information for example.
/// Collision box - determines the area for collision detection. Usually smaller than the sprite area.
/// Rear wheels - Fixed angle wheels. Invisible. Force is applied to these wheels which moves the whole car as a consequence,
/// </summary>
public class Car : Vehicle
{
public CarInfo CarInfo { get; private set; }

private const float angleLock = 35f;
private const float lockToLockTime = 500; //from lock to lock in 0.5 seconds

private Body _body;
private PolygonShape _shape;
private PolygonShape _spriteShape;
Expand All @@ -65,6 +78,8 @@ public override float CollisionHeight
get { return _collisionHeight; }
}

public override event EventHandler Collided;

public override Vector2 CollisionTopLeft
{
get { return _shape.Vertices[0].ToBlockUnits(); }
Expand Down Expand Up @@ -162,6 +177,7 @@ public override void CreateBody(World world, float width, float height)
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
Expand Down Expand Up @@ -213,6 +229,13 @@ public override void CreateBody(World world, float width, float height)
_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);
Expand All @@ -232,14 +255,13 @@ public override void Update(ParticipantInput input, float elapsedTime)
Position3 = new Vector3(position.X, position.Y, Position3.Z);
foreach (var wheel in _wheels)
{
wheel.UpdateFriction();
wheel.UpdateDrive(input.Forward);
wheel.Update(input.Forward, elapsedTime);
}

//control steering
var lockAngle = MathHelper.ToRadians(35);
var turnSpeedPerSec = MathHelper.ToRadians(320); //from lock to lock in 0.25s
var turnPerTimeSpep = turnSpeedPerSec / 60f;
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;
Expand Down
2 changes: 2 additions & 0 deletions GTA2.NET/GTA2.NET/GTA2.NET Core/Logic/Vehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ protected Vehicle(Vector3 startUpPosition, float startUpRotation) : base(startUp
set { throw new NotSupportedException("Set Position3 instead."); }
}

public abstract event EventHandler Collided; //ToDo: change EventHandler to something more specific...

public abstract Vector2 CollisionTopLeft { get; }
public abstract Vector2 CollisionTopRight { get; }
public abstract Vector2 CollisionBottomRight { get; }
Expand Down
17 changes: 6 additions & 11 deletions GTA2.NET/GTA2.NET/GTA2.NET Core/Logic/Wheel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,18 @@ public Vector2 GetForwardVelocity()
return Vector2.Dot(currentForwardNormal, Body.LinearVelocity)*currentForwardNormal;
}

public void UpdateFriction()
public void Update(float acceleration, float elapsedTime)
{
//angulat velocity
Body.ApplyAngularImpulse(_mCurrentTraction * 0.1f * Body.Inertia * -Body.AngularVelocity);

//forward linear velocity
var currentForwardNormal = GetForwardVelocity();
Geometry.SafeNormalize(ref currentForwardNormal);
float currentForwardSpeed = Geometry.SafeNormalize(ref currentForwardNormal);
float dragForceMagnitude = -0.25f*currentForwardSpeed;
float dragForceMagnitude = -0.25f * currentForwardSpeed;
dragForceMagnitude *= _mCurrentDrag;
Body.ApplyForce(_mCurrentTraction * dragForceMagnitude * currentForwardNormal, Body.WorldCenter);

}

public void UpdateDrive(float acceleration)
{
//find desired speed
float desiredSpeed = 0;
if (acceleration > 0)
Expand All @@ -136,8 +131,8 @@ public void UpdateDrive(float acceleration)
desiredSpeed = _mMaxBackwardSpeed;

//find current speed in forward direction
var currentForwardNormal = Body.GetWorldVector(Vector2.UnitY);
float currentSpeed = Vector2.Dot(GetForwardVelocity(), currentForwardNormal);
var worldVector = Body.GetWorldVector(Vector2.UnitY);
float currentSpeed = Vector2.Dot(GetForwardVelocity(), worldVector);

//apply necessary force
float force = 0;
Expand All @@ -151,7 +146,7 @@ public void UpdateDrive(float acceleration)

float speedFactor = currentSpeed/120;

var driveImpulse = (force/60f)*currentForwardNormal;
var driveImpulse = (force*elapsedTime)*worldVector;
if (driveImpulse.Length() > _mMaxLateralImpulse)
driveImpulse *= _mMaxLateralImpulse/driveImpulse.Length();

Expand Down
12 changes: 6 additions & 6 deletions GTA2.NET/GTA2.NET/GTA2.NET/MainGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ protected override void Update(GameTime gameTime)
var elapsedGameTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

var input = new PlayerInput();
HandleInput(ref input);
HandleInput(ref input, controller);
ChasingObject.Update(input, elapsedGameTime);


Expand Down Expand Up @@ -286,25 +286,25 @@ private void UpdateObjects(float elapsedGameTime)



private void HandleInput(ref PlayerInput playerInput)
private void HandleInput(ref PlayerInput playerInput, XboxController controller)
{
//Vector3 cameraPos = BaseGame.CameraPos;
if (Input.KeyboardUpPressed)
if (Input.KeyboardUpPressed || controller.RightTrigger > 0)
{
//_playerForwardDelta = ForwardScalar * _elapsedGameTime;
playerInput.Forward = 1;
}
if (Input.KeyboardDownPressed)
else if (Input.KeyboardDownPressed || controller.LeftTrigger > 0)
{
//_playerForwardDelta = -ForwardScalar * _elapsedGameTime;
playerInput.Forward = -1;
}
if (Input.KeyboardLeftPressed)
if (Input.KeyboardLeftPressed || controller.IsDPadLeftPressed)
{
//_playerRotationDelta = -RotationScalar * _elapsedGameTime;
playerInput.Rotation = 1;
}
if (Input.KeyboardRightPressed)
else if (Input.KeyboardRightPressed || controller.IsDPadRightPressed)
{
//_playerRotationDelta = RotationScalar * _elapsedGameTime;
playerInput.Rotation = -1;
Expand Down

0 comments on commit 58a863a

Please sign in to comment.