public void reset(VectorF pos, VectorF dir) { this.engine = 0.0f; this.steering = 0.0f; this.brakes = 0.0f; this.friction = 0.0f; if (pos == null) { this.position = VectorF.create(Room.Width / 2, Room.Height / 2); } else { this.position = pos.clone(); } if (dir == null) { this.direction = VectorF.create(0, 1); } else { this.direction = dir.clone(); this.direction.normalize(); } this.directionAngle = this.direction.angle(); this.thrust = new VectorF(); this.brakeForce = new VectorF(); this.driveVelocityValue = 0.0f; this.driveVelocity = new VectorF(); this.impactVelocity = new VectorF(); this.velocity = new VectorF(); this.thrustForceValue = 0.0f; this.brakeForceValue = 0.0f; this.syncCharacter(); }
public int TryHitNode(int x, int y, float threshold) { if (CheckptCount == 0) { return(-1); } VectorF hit = VectorF.create(x, y); float minDist = 9999.0f; int nearestNode = -1; int i = 0; for (i = 0; i < MAX_PATH_NODES; i += 1) { if (Checkpoints[i].pt == null) { continue; } float dist = VectorF.distance(hit, Checkpoints[i].pt); if (dist <= threshold && dist < minDist) { minDist = dist; nearestNode = i; } } return(nearestNode); }
public void setCharacter(Character c, int carSprite, CharacterDirection carSpriteDir, int view = 0, int loop = 0, int frame = 0) { int carl = 0; int carw = 0; if (carSpriteDir == eDirectionDown || carSpriteDir == eDirectionUp) { carl = Game.SpriteHeight[carSprite]; carw = Game.SpriteWidth[carSprite]; } else if (carSpriteDir == eDirectionLeft || carSpriteDir == eDirectionRight) { carl = Game.SpriteWidth[carSprite]; carw = Game.SpriteHeight[carSprite]; } else { AbortGame("Source car sprite direction cannot be diagonal, please provide sprite having one of the following directions: left, right, up or down."); return; } this.c = c; this.carSprite = carSprite; this.carSpriteAngle = RotatedView.AngleForLoop(carSpriteDir); this.viewFrame = Game.GetViewFrame(view, loop, frame); this.collPointOff[0] = VectorF.create(carl / 2, -carw / 2); this.collPointOff[1] = VectorF.create(carl / 2, carw / 2); this.collPointOff[2] = VectorF.create(-carl / 2, carw / 2); this.collPointOff[3] = VectorF.create(-carl / 2, -carw / 2); this.colpt[0] = new Point(); this.colpt[1] = new Point(); this.colpt[2] = new Point(); this.colpt[3] = new Point(); this.syncCharacter(); }
public void Reset() { this.vehicleIndex = -1; this.currentNode = -1; this.targetPos = null; this.targetDir = null; }
public static VectorF createF(float x, float y) { VectorF v = new VectorF(); v.x = x; v.y = y; return(v); }
public static float projection(VectorF a, VectorF b) { if (b.isZero()) { return(0.0f); } return((a.x * b.x + a.y * b.y) / b.length()); }
public static VectorF subtract(VectorF a, VectorF b) { VectorF v = new VectorF(); v.x = a.x - b.x; v.y = a.y - b.y; return(v); }
// Methods public VectorF clone() { VectorF v = new VectorF(); v.x = this.x; v.y = this.y; return(v); }
public static VectorF create(int x, int y) { VectorF v = new VectorF(); v.x = IntToFloat(x); v.y = IntToFloat(y); return(v); }
public VectorF PositionCarOnGrid(int car, int gridpos) { VectorF pos = StartingGrid[gridpos].clone(); pos.x += 4.0f + Cars[car].bodyLength / 2.0f; pos.y += 1.0f; Cars[car].Reset(pos, VectorF.create(-1, 0)); return(null); }
// Methods public void Reset() { this.pt = null; this.radius = 1.0f; this.threshold = 1.0f; this.speed = -1.0f; this.next = -1; this.prev = -1; }
public void RunVeh2VehCollision() { // Reset for (int i2 = 0; i2 < impactPairs.Length; i2 += 1) { impactPairs[i2] = false; } for (int i2 = 0; i2 < rect.Length; i2 += 1) { rect[i2] = null; } // Optimisation (previously generated a lot of garbage) //bool[] impactPairs = new bool[MAX_RACING_CARS_SQUARED]; //VectorF[] rect = new VectorF[4]; int i = 0; for (i = 0; i < MAX_RACING_CARS; i += 1) { if (!Cars[i].IsInit) { continue; } int j = 0; for (j = 0; j < MAX_RACING_CARS; j += 1) { if (j == i) { continue; } if (!Cars[j].IsInit) { continue; } if (i > j && impactPairs[i * MAX_RACING_CARS + j]) { continue; } rect[0] = Cars[j].collPoint[0]; rect[1] = Cars[j].collPoint[1]; rect[2] = Cars[j].collPoint[2]; rect[3] = Cars[j].collPoint[3]; VectorF impact = Cars[i].DetectCollision(rect, Cars[j].velocity, j); if (impact != null) { impactPairs[i * MAX_RACING_CARS + j] = true; Cars[i].velocity.add(impact); impact.negate(); Cars[j].velocity.add(impact); } } } }
public void UpdateBody() { int i = 0; for (i = 0; i < NUM_COLLISION_POINTS; i += 1) { VectorF colpt = this.collPoint[i]; colpt.set(this.collPointOff[i]); colpt.rotate(this.direction.angle()); colpt.add(this.position); } }
public void processInteraction(float deltaTime) { int i = 0; float[] friction = new float[NUM_COLLISION_POINTS]; float avg_friction = 0.0f; bool[] hit_area = new bool[NUM_COLLISION_POINTS]; for (i = 0; i < NUM_COLLISION_POINTS; i += 1) { VectorF colpoint = this.collPointOff[i].clone(); colpoint.rotate(this.direction.angle()); colpoint.add(this.position); this.collPoint[i] = colpoint; int room_x = FloatToInt(colpoint.x, eRoundNearest); int room_y = FloatToInt(colpoint.y, eRoundNearest); int screen_x = room_x - GetViewportX(); int screen_y = room_y - GetViewportY(); int area = GetWalkableAreaAt(screen_x, screen_y); if (area == 0) { hit_area[i] = true; } else { friction[i] = Track.TerraSlideFriction[area]; avg_friction += friction[i]; this.terrafriction[i] = friction[i]; } this.colpt[i].x = FloatToInt(colpoint.x, eRoundNearest); this.colpt[i].y = FloatToInt(colpoint.y, eRoundNearest); } avg_friction /= IntToFloat(NUM_COLLISION_POINTS); this.friction = avg_friction; for (i = 0; i < NUM_COLLISION_POINTS; i += 1) { if (!hit_area[i]) { continue; } VectorF impact = VectorF.subtract(this.position, this.collPoint[i]); impact.normalize(); impact.scale(Maths.AbsF(this.driveVelocityValue)); this.impactVelocity.add(impact); float projection = VectorF.projection(impact, this.driveVelocity); if (this.driveVelocityValue < 0.0f) { projection = -projection; } this.driveVelocityValue += projection; } }
public override void room_Load() { StartingGrid[0] = VectorF.create(1140, 326 + 12); StartingGrid[1] = VectorF.create(1172, 273 + 12); StartingGrid[2] = VectorF.create(1204, 326 + 12); StartingGrid[3] = VectorF.create(1236, 273 + 12); StartingGrid[4] = VectorF.create(1268, 326 + 12); StartingGrid[5] = VectorF.create(1300, 273 + 12); FadeOut(0); StopAllAudio(); SetupAIRace(); aWelcome_to_the_Show.Play(); }
public void UnInitBase() { this.DetachCharacter(); int i = 0; for (i = 0; i < NUM_COLLISION_POINTS; i += 1) { this.collPointOff[i] = null; this.collPoint[i] = null; } this.position = null; this.direction = null; this.velocity = null; }
public void DriveToTheTarget() { if (this.targetPos == null) { return; } this.targetDir = VectorF.subtract(this.targetPos, Cars[this.vehicleIndex].position); float angleThreshold = 0f; if (!this.targetDir.isZero()) { angleThreshold = Maths.ArcTan(this.targetThreshold / this.targetDir.length()); } float angleBetween = VectorF.angleBetween(Cars[this.vehicleIndex].direction, this.targetDir); if (angleBetween >= -angleThreshold && angleBetween <= angleThreshold) { Cars[this.vehicleIndex].steeringWheelAngle = 0.0f; } else { if (angleBetween > 0.0f) { Cars[this.vehicleIndex].steeringWheelAngle = UISteeringAngle; } else { Cars[this.vehicleIndex].steeringWheelAngle = -UISteeringAngle; } } Cars[this.vehicleIndex].Brakes = 0.0f; if (this.targetSpeedHint < 0.0f) { Cars[this.vehicleIndex].Accelerator = 1.0f; } else { float speed = Cars[this.vehicleIndex].velocity.length(); if (speed < this.targetSpeedHint) { Cars[this.vehicleIndex].Accelerator = 1.0f; } else if (speed > this.targetSpeedHint) { Cars[this.vehicleIndex].Accelerator = 0.0f; Cars[this.vehicleIndex].Brakes = 1.0f; } } }
public int PutNewNode(int x, int y) { if (FreePathSlot < 0) { return(0); } int newnode = FreePathSlot; Paths[newnode].Reset(); Paths[newnode].pt = VectorF.create(x, y); OnNewNode(newnode, LastPathNode, FirstPathNode); PathNodeCount += 1; FindFirstFreeNode(); return(newnode); }
public int PutNewNode(int x, int y) { if (FreeCheckptSlot < 0) { return(0); } int newnode = FreeCheckptSlot; Checkpoints[newnode].Reset(); Checkpoints[newnode].pt = VectorF.create(x, y); OnNewNode(newnode, LastCheckpt, FirstCheckpt); Checkpoints[newnode].order = CheckptCount; CheckptCount += 1; FindFirstFreeCheckpoint(); return(newnode); }
public void LoadAIPaths() { File f = File.Open("$APPDATADIR$/Data/aipaths.dat", eFileRead); if (f == null) { f = File.Open("$INSTALLDIR$/Data/aipaths.dat", eFileRead); if (f == null) { return; } } int n = 0; for (n = 0; n < MAX_PATH_NODES; n += 1) { Paths[n].Reset(); } PathNodeCount = 0; FirstPathNode = f.ReadInt(); LastPathNode = f.ReadInt(); n = FirstPathNode; int loads = 0; do { loads += 1; if (loads % 50 == 0) { Wait(1); } int x = f.ReadInt(); int y = f.ReadInt(); Paths[n].pt = VectorF.create(x, y); Paths[n].radius = IntToFloat(f.ReadInt()); Paths[n].threshold = IntToFloat(f.ReadInt()); Paths[n].speed = IntToFloat(f.ReadInt()); Paths[n].prev = f.ReadInt(); Paths[n].next = f.ReadInt(); PathNodeCount += 1; n = Paths[n].next; }while (n != FirstPathNode); FindFirstFreeNode(); }
public void LoadRaceCheckpoints() { File f = File.Open("$APPDATADIR$/Data/checkpoints.dat", eFileRead); if (f == null) { f = File.Open("$INSTALLDIR$/Data/checkpoints.dat", eFileRead); if (f == null) { return; } } int n = 0; for (n = 0; n < MAX_CHECKPOINTS; n += 1) { Checkpoints[n].Reset(); } CheckptCount = 0; FirstCheckpt = f.ReadInt(); LastCheckpt = f.ReadInt(); n = FirstCheckpt; int loads = 0; do { loads += 1; if (loads % 50 == 0) { Wait(1); } int x = f.ReadInt(); int y = f.ReadInt(); Checkpoints[n].pt = VectorF.create(x, y); Checkpoints[n].order = CheckptCount; Checkpoints[n].prev = f.ReadInt(); Checkpoints[n].next = f.ReadInt(); CheckptCount += 1; n = Checkpoints[n].next; }while (n != FirstCheckpt); f.Close(); FindFirstFreeCheckpoint(); }
// Methods public bool TestShouldChooseNewTarget() { if (this.targetPos == null) { return(true); } int curNode = this.currentNode; if (curNode >= 0) { int prevNode = Paths[curNode].prev; int nextNode = Paths[curNode].next; if (nextNode >= 0 && (prevNode < 0 || VectorF.distance(Cars[this.vehicleIndex].position, Paths[prevNode].pt) > VectorF.distance(Paths[this.currentNode].pt, Paths[prevNode].pt)) && VectorF.distance(Cars[this.vehicleIndex].position, Paths[nextNode].pt) < VectorF.distance(Paths[this.currentNode].pt, Paths[nextNode].pt)) { return(true); } } return(VectorF.distance(Cars[this.vehicleIndex].position, this.targetPos) <= this.targetCheckRadius); }
public void Reset(VectorF pos, VectorF dir) { this.ResetBase(pos, dir); this.engineMaxPower = 200.0f; this.engineAccelerator = 0.0f; this.enginePowerGoal = 0.0f; this.enginePower = 0.0f; this.brakePower = 0.0f; this.driveWheelTorque = 0.0f; this.driveWheelForce = 0.0f; this.distanceBetweenAxles = this.bodyLength / 2.0f; this.stillTurningVelocity = 4.0f; this.driftVelocityFactor = 240.0f; this.steeringWheelAngle = 0.0f; this.turningAccel = new VectorF(); this.bodyMass = 1.0f; this.bodyAerodynamics = 1.0f; this.hardImpactLossFactor = 0.5f; this.softImpactLossFactor = 0.8f; this.weightForce = 0.0f; this.envGrip = 0.0f; this.envSlideFriction = 0.0f; this.envRollFriction = 0.0f; this.envResistance = 0.0f; this.infoRollAntiforce = 0.0f; this.infoSlideAntiforce = 0.0f; this.infoImpact = new VectorF(); int i = 0; for (i = 0; i < NUM_COLLISION_POINTS; i += 1) { this.oldCollPt[i] = new VectorF(); this.collPtHit[i] = -1; this.oldCollPtHit[i] = -1; this.collPtCarHit[i] = -1; this.oldCollPtCarHit[i] = -1; } this.numHits = 0; this.numCarHits = 0; this.UpdateBody(); this.SyncCharacter(); }
public void run(float deltaTime) { this.position.addScaled(this.velocity, deltaTime); float rot_angle = 0f; if (this.driveVelocityValue >= 0.0f) { rot_angle = this.steering * deltaTime; } else { rot_angle = -this.steering * deltaTime; } this.directionAngle = Maths.Angle2Pi(this.directionAngle + rot_angle); this.direction.rotate(rot_angle); this.processInteraction(deltaTime); float absVelocity = Maths.AbsF(this.driveVelocityValue); float thrustResistance = this.friction * absVelocity + this.brakes; float thrustForce = this.engine * deltaTime; float brakeForce = Maths.MinF(thrustResistance * deltaTime, absVelocity); if (this.driveVelocityValue < 0.0f) { brakeForce = -brakeForce; } this.driveVelocityValue += thrustForce - brakeForce; this.driveVelocity.set(this.direction); this.driveVelocity.scale(this.driveVelocityValue); this.thrustForceValue = thrustForce; this.brakeForceValue = brakeForce; float impactValue = this.impactVelocity.length(); if (impactValue > 0.0f) { impactValue = Maths.MaxF(0.0f, impactValue - (this.friction * impactValue * 10.0f + this.brakes) * deltaTime); this.impactVelocity.truncate(impactValue); } this.velocity.set(this.driveVelocity); this.velocity.add(this.impactVelocity); this.syncCharacter(); }
public bool ChooseNewTarget() { if (PathNodeCount > 0) { if (this.currentNode >= 0 && Paths[this.currentNode].pt != null) { this.currentNode = Paths[this.currentNode].next; } else { this.currentNode = FirstPathNode; } this.targetPos = Paths[this.currentNode].pt.clone(); this.targetCheckRadius = Paths[this.currentNode].radius; this.targetThreshold = Paths[this.currentNode].threshold; this.targetSpeedHint = Paths[this.currentNode].speed; } else { } return(true); }
public void RunCollision(VectorF impactVelocity, float deltaTime) { if (this.numHits == 0) { return; } VectorF posImpact = VectorF.zero(); VectorF negImpact = VectorF.zero(); int i = 0; for (i = 0; i < NUM_COLLISION_POINTS; i += 1) { if (this.collPtHit[i] < 0) { continue; } if (this.oldCollPtHit[i] == this.collPtHit[i]) { continue; } VectorF impact = VectorF.subtract(this.collPoint[i], this.oldCollPt[i]); if (impact.isZero()) { continue; } impact.normalize(); float velProjection = VectorF.projection(this.velocity, impact); if (velProjection > 0.0f) { impact.scale(-velProjection * (2.0f - this.hardImpactLossFactor)); posImpact.max(impact); negImpact.min(impact); } } impactVelocity.add(posImpact); impactVelocity.add(negImpact); }
public void min(VectorF other) { this.x = Maths.MinF(this.x, other.x); this.y = Maths.MinF(this.y, other.y); }
public void max(VectorF other) { this.x = Maths.MaxF(this.x, other.x); this.y = Maths.MaxF(this.y, other.y); }
public void addScaled(VectorF v, float scale) { this.x += v.x * scale; this.y += v.y * scale; }
public void add(VectorF v) { this.x += v.x; this.y += v.y; }