// Methods public void ResetBase(VectorF pos, VectorF dir) { 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(); } int i = 0; for (i = 0; i < NUM_COLLISION_POINTS; i += 1) { this.collPoint[i] = new VectorF(); } this.velocity = VectorF.zero(); this.angularVelocity = 0.0f; }
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 VectorF DetectCollision(VectorF[] rect, VectorF otherVelocity, int otherIndex) { this.numCarHits = 0; VectorF p21 = VectorF.subtract(rect[1], rect[0]); VectorF p41 = VectorF.subtract(rect[3], rect[0]); float p21magnitude_squared = p21.x * p21.x + p21.y * p21.y; float p41magnitude_squared = p41.x * p41.x + p41.y * p41.y; int i = 0; for (i = 0; i < NUM_COLLISION_POINTS; i += 1) { this.collPtCarHit[i] = -1; VectorF p = VectorF.subtract(this.collPoint[i], rect[0]); float pp21 = p.x * p21.x + p.y * p21.y; if (pp21 >= 0.0 && pp21 <= p21magnitude_squared) { float pp41 = p.x * p41.x + p.y * p41.y; if (pp41 >= 0.0 && pp41 <= p41magnitude_squared) { this.collPtCarHit[i] = otherIndex; this.numCarHits += 1; } } } if (this.numCarHits == 0) { return(null); } VectorF impactVelocity = new VectorF(); VectorF posImpact = VectorF.zero(); VectorF negImpact = VectorF.zero(); for (i = 0; i < NUM_COLLISION_POINTS; i += 1) { if (this.collPtCarHit[i] < 0) { continue; } if (this.oldCollPtCarHit[i] == this.collPtCarHit[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 * (1.0f - this.softImpactLossFactor)); posImpact.max(impact); negImpact.min(impact); impact.negate(); impact.normalize(); } float otherProjection = VectorF.projection(otherVelocity, impact); if (otherProjection < 0.0f) { impact.scale(otherProjection * (1.0f - this.softImpactLossFactor)); posImpact.max(impact); negImpact.min(impact); } } impactVelocity.add(posImpact); impactVelocity.add(negImpact); return(impactVelocity); }
public void RunPhysics(float deltaTime) { int i = 0; if (this.strictCollisions && this.numHits > 0) { float x = 0f; float y = 0f; for (i = 0; i < NUM_COLLISION_POINTS; i += 1) { this.collPoint[i].set(this.oldCollPt[i]); this.collPtHit[i] = this.oldCollPtHit[i]; x += this.collPoint[i].x; y += this.collPoint[i].y; } this.position.x = x / IntToFloat(NUM_COLLISION_POINTS); this.position.y = y / IntToFloat(NUM_COLLISION_POINTS); } else { for (i = 0; i < NUM_COLLISION_POINTS; i += 1) { this.oldCollPt[i].set(this.collPoint[i]); } } for (i = 0; i < NUM_COLLISION_POINTS; i += 1) { this.oldCollPtHit[i] = this.collPtHit[i]; this.oldCollPtCarHit[i] = this.collPtCarHit[i]; } this.RunPhysicsBase(deltaTime); this.UpdateBody(); this.UpdateEnviroment(); this.enginePower = this.engineMaxPower * this.engineAccelerator; this.driveWheelTorque = this.enginePower; this.driveWheelTorque -= this.driveWheelTorque * this.brakePower; this.driveWheelForce = this.driveWheelTorque * this.driveWheelGrip; VectorF rollDirection = this.direction.clone(); VectorF slideDirection = rollDirection.clone(); slideDirection.rotate(Maths.Pi / 2.0f); float rollingVelocity = VectorF.projection(this.velocity, rollDirection); float slidingVelocity = VectorF.projection(this.velocity, slideDirection); this.turningAccel.makeZero(); if (this.velocity.isZero()) { this.angularVelocity = this.steeringWheelAngle * this.stillTurningVelocity; } else { if (this.steeringWheelAngle != 0.0f) { float steerAngle = this.steeringWheelAngle; VectorF driveWheelPos = this.position.clone(); VectorF steerWheelPos = this.position.clone(); driveWheelPos.addScaled(this.direction, -this.distanceBetweenAxles / 2.0f); steerWheelPos.addScaled(this.direction, this.distanceBetweenAxles / 2.0f); VectorF driveWheelMovement = rollDirection.clone(); VectorF steerWheelMovement = rollDirection.clone(); steerWheelMovement.rotate(steerAngle); driveWheelPos.addScaled(driveWheelMovement, rollingVelocity); steerWheelPos.addScaled(steerWheelMovement, rollingVelocity); VectorF newPosDir = VectorF.subtract(steerWheelPos, driveWheelPos); newPosDir.normalize(); this.angularVelocity = VectorF.angleBetween(this.direction, newPosDir); this.angularVelocity *= this.envGrip; float dumb_drift_factor = Maths.ArcTan(Maths.AbsF(rollingVelocity / this.driftVelocityFactor)) / (Maths.Pi / 2.0f); this.turningAccel = VectorF.subtract(newPosDir, rollDirection); this.turningAccel.scale(rollingVelocity * (1.0f - dumb_drift_factor) * this.envGrip); } else { this.angularVelocity = 0.0f; } } VectorF rollResDir = rollDirection.clone(); rollResDir.scale(-rollingVelocity); rollResDir.normalize(); VectorF slideResDir = slideDirection.clone(); slideResDir.scale(-slidingVelocity); slideResDir.normalize(); float driveForce = (this.driveWheelForce * deltaTime) / this.bodyMass; rollingVelocity = Maths.AbsF(rollingVelocity); slidingVelocity = Maths.AbsF(slidingVelocity); float slide_friction = this.envSlideFriction * this.weightForce; float roll_friction = this.envRollFriction * this.weightForce; float airres_force = 0.5f * Track.AirResistance * this.bodyAerodynamics; float env_res_force = this.envResistance; float rollAF = ((slide_friction * this.brakePower + roll_friction * (1.0f - this.brakePower) + airres_force * rollingVelocity * rollingVelocity + env_res_force * rollingVelocity) * deltaTime) / this.bodyMass; float slideAF = ((slide_friction + airres_force * slidingVelocity * slidingVelocity + env_res_force * slidingVelocity) * deltaTime) / this.bodyMass; rollAF = Maths.ClampF(rollAF, 0.0f, rollingVelocity); slideAF = Maths.ClampF(slideAF, 0.0f, slidingVelocity); this.infoRollAntiforce = rollAF; this.infoSlideAntiforce = slideAF; this.velocity.addScaled(rollDirection, driveForce); float x1 = this.velocity.x; float y1 = this.velocity.y; this.velocity.addScaled(slideResDir, slideAF); this.velocity.addScaled(rollResDir, rollAF); float x2 = this.velocity.x; float y2 = this.velocity.y; if (x1 >= 0.0 && x2 < 0.0 || x1 <= 0.0 && x2 > 0.0f) { this.velocity.x = 0.0f; } if (y1 >= 0.0 && y2 < 0.0 || y1 <= 0.0 && y2 > 0.0f) { this.velocity.y = 0.0f; } this.velocity.addScaled(this.turningAccel, deltaTime); VectorF impactVelocity = VectorF.zero(); this.RunCollision(impactVelocity, deltaTime); this.velocity.add(impactVelocity); this.infoImpact.set(impactVelocity); }