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); }