Compare commits

...

2 Commits

View File

@ -74,7 +74,7 @@ public class CollisionSystem extends ECSSystem {
double totalYVel = Math.abs(ridgidBody.yVel) + Math.abs(otherRidgidBody.yVel); double totalYVel = Math.abs(ridgidBody.yVel) + Math.abs(otherRidgidBody.yVel);
double xMult = 0; double xMult = 0;
double yMult = 0; double yMult = 0;
double multTot = (1 + ridgidBody.bounciness); double multTot = (1 + ridgidBody.bounciness); // reflect a force back 100%, (srtabilises) then add any extra in bounce
double totalMass = ridgidBody.mass + otherRidgidBody.mass; double totalMass = ridgidBody.mass + otherRidgidBody.mass;
boolean fullEdge = false; boolean fullEdge = false;
// left-hand collision // left-hand collision
@ -103,11 +103,112 @@ public class CollisionSystem extends ECSSystem {
pos.y = otherPos.y + otherCollider.y; pos.y = otherPos.y + otherCollider.y;
yMult = multTot; yMult = multTot;
} }
if(!fullEdge){
// Not a full edge collision, must evaluate which edge it is
double deltaXVel = Math.abs(ridgidBody.xVel - otherRidgidBody.xVel);
double deltaYVel = Math.abs(ridgidBody.yVel - otherRidgidBody.yVel);
double deltaMin = 50.;
double incidentThreshold = 0; // 10% of the sum of the velocity
if (trCollide){
fullEdge = true;
// use the ratio i.e. the angle
// There are 3 cases, the incident angle is less than 45
// The incident angle is more than 45
// The incident angle is near 0 or 90
if(deltaXVel < incidentThreshold || deltaYVel < incidentThreshold){
// must use cover calc, as it is near stationary
if (otherPos.y - (pos.y + collider.y) >= otherPos.x - (pos.x + collider.x)){
// vertical cover wins
yMult = multTot;
}
else{
xMult = -multTot;
}
}
else if (deltaYVel > deltaXVel){
// Top edge
if (deltaYVel > deltaMin) pos.y = otherPos.y + otherCollider.y;
yMult = multTot;
}
else{
if (deltaXVel > deltaMin) pos.x = otherPos.x + collider.x;
xMult = -multTot;
}
}
else if (brCollide){
fullEdge = true;
if(deltaXVel < incidentThreshold || deltaYVel < incidentThreshold){
// must use cover calc, as it is near stationary
if (pos.y - (otherPos.y + otherCollider.y) >= otherPos.x - (pos.x + collider.x)){
// vertical cover wins
yMult = -multTot;
}
else{
xMult = -multTot;
}
}
else if (deltaYVel < deltaXVel){
//pos.y = otherPos.y + otherCollider.y;
if (deltaYVel > deltaMin) pos.y = otherPos.y - collider.y;
yMult = -multTot;
}
else{
if (deltaXVel > deltaMin) pos.x = otherPos.x - collider.x;
xMult = -multTot;
}
}
else if (blCollide){
fullEdge = true;
if(deltaXVel < incidentThreshold || deltaYVel < incidentThreshold){
// must use cover calc, as it is near stationary
if (pos.y - (otherPos.y + otherCollider.y) >= pos.x - (otherPos.x + otherCollider.x)){
// vertical cover wins
yMult = -multTot;
}
else{
xMult = multTot;
}
}
else if (deltaYVel > deltaXVel){
if (deltaYVel > deltaMin) pos.y = otherPos.y - collider.y;
yMult = -multTot;
}
else{
if (deltaXVel > deltaMin) pos.x = otherPos.x + otherCollider.x;
xMult = multTot;
}
}
else{ // top left
fullEdge = true;
if(deltaXVel < incidentThreshold || deltaYVel < incidentThreshold){
// must use cover calc, as it is near stationary
if (otherPos.y - (pos.y + collider.y) >= pos.x - (otherPos.x + otherCollider.x)){
// vertical cover wins
yMult = multTot;
}
else{
xMult = multTot;
}
}
else if (deltaYVel > deltaXVel){
if (deltaYVel > deltaMin) pos.y = otherPos.y +otherCollider.y;
yMult = multTot;
}
else {
if (deltaXVel > deltaMin) pos.x = otherPos.x + otherCollider.x;
xMult = multTot;
}
}
}
if (fullEdge) { if (fullEdge) {
// Finally, actuate the calculation // Finally, actuate the calculation
// entity 2 is immovable; full acceleration applied to the current entity // entity 2 is immovable; full acceleration applied to the current entity
if (otherRidgidBody.mass > 0){ if (otherRidgidBody.mass >= 0){
yMult *= otherRidgidBody.bounciness;
xMult *= otherRidgidBody.bounciness;
ridgidBody.xVel += totalXVel * xMult * ((totalMass - ridgidBody.mass) / totalMass); ridgidBody.xVel += totalXVel * xMult * ((totalMass - ridgidBody.mass) / totalMass);
ridgidBody.yVel += totalYVel * yMult * ((totalMass - ridgidBody.mass) / totalMass); ridgidBody.yVel += totalYVel * yMult * ((totalMass - ridgidBody.mass) / totalMass);
@ -119,7 +220,7 @@ public class CollisionSystem extends ECSSystem {
ridgidBody.yVel += totalYVel * yMult; ridgidBody.yVel += totalYVel * yMult;
} }
Object grav = gameEngine.getComponentData(entity, Gravity.class); Object grav = gameEngine.getComponentData(entity, Gravity.class);
if (grav != null) { if (grav != null && otherRidgidBody.mass < 0) {
// negate gravity while collided // negate gravity while collided
Gravity gravity = (Gravity) grav; Gravity gravity = (Gravity) grav;
ridgidBody.xAcc += gravity.x * -1.; ridgidBody.xAcc += gravity.x * -1.;