2020-05-01 14:36:33 -04:00
|
|
|
package mindustry.async;
|
|
|
|
|
|
|
|
import arc.math.geom.*;
|
|
|
|
import arc.struct.*;
|
|
|
|
import arc.util.*;
|
|
|
|
import mindustry.entities.*;
|
|
|
|
import mindustry.gen.*;
|
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
import static mindustry.Vars.*;
|
|
|
|
|
2020-05-01 16:35:18 -04:00
|
|
|
//TODO remove
|
2020-05-01 14:49:58 -04:00
|
|
|
public class CollisionProcess implements AsyncProcess{
|
|
|
|
//private Physics physics;
|
2020-05-01 16:35:18 -04:00
|
|
|
private QuadTree<Hitboxc> tree;
|
|
|
|
private Array<Hitboxc> insertEntities = new Array<>();
|
|
|
|
private Array<Hitboxc> checkEntities = new Array<>();
|
2020-05-01 14:36:33 -04:00
|
|
|
|
2020-05-01 16:35:18 -04:00
|
|
|
private Array<Hitboxc> arrOut = new Array<>();
|
2020-05-01 14:36:33 -04:00
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
private Vec2 l1 = new Vec2();
|
|
|
|
private Rect r1 = new Rect();
|
|
|
|
private Rect r2 = new Rect();
|
|
|
|
//private Array<CollisionRef> refs = new Array<>(false);
|
|
|
|
//private BodyDef def;
|
|
|
|
//private FixtureDef fdef;
|
2020-05-01 14:36:33 -04:00
|
|
|
|
2020-05-01 16:35:18 -04:00
|
|
|
private EntityGroup<? extends Hitboxc> inserted = Groups.unit;
|
|
|
|
private EntityGroup<? extends Hitboxc> checked = Groups.bullet;
|
|
|
|
private Array<Hitboxc> collisions = new Array<>(Hitboxc.class);
|
2020-05-01 14:36:33 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void begin(){
|
2020-05-01 14:49:58 -04:00
|
|
|
if(tree == null) return;
|
2020-05-01 14:36:33 -04:00
|
|
|
|
|
|
|
collisions.clear();
|
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
insertEntities.size = 0;
|
|
|
|
checkEntities.size = 0;
|
2020-05-01 14:36:33 -04:00
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
inserted.copy(insertEntities);
|
|
|
|
checked.copy(checkEntities);
|
2020-05-01 14:36:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void process(){
|
2020-05-01 14:49:58 -04:00
|
|
|
if(tree == null) return;
|
2020-05-01 14:36:33 -04:00
|
|
|
|
|
|
|
collisions.clear();
|
|
|
|
|
|
|
|
Time.mark();
|
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
tree.clear();
|
|
|
|
//insert targets
|
2020-05-01 16:35:18 -04:00
|
|
|
for(Hitboxc ins : insertEntities){
|
2020-05-01 14:49:58 -04:00
|
|
|
tree.insert(ins);
|
|
|
|
}
|
|
|
|
|
2020-05-01 16:35:18 -04:00
|
|
|
for(Hitboxc solid : checked){
|
2020-05-01 14:49:58 -04:00
|
|
|
solid.hitbox(r1);
|
|
|
|
r1.x += (solid.lastX() - solid.getX());
|
|
|
|
r1.y += (solid.lastY() - solid.getY());
|
|
|
|
|
|
|
|
solid.hitbox(r2);
|
|
|
|
r2.merge(r1);
|
2020-05-01 14:36:33 -04:00
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
arrOut.clear();
|
2020-05-01 16:35:18 -04:00
|
|
|
tree.intersect(r2, arrOut);
|
2020-05-01 14:49:58 -04:00
|
|
|
|
2020-05-01 16:35:18 -04:00
|
|
|
for(Hitboxc sc : arrOut){
|
2020-05-01 14:49:58 -04:00
|
|
|
sc.hitbox(r1);
|
|
|
|
if(r2.overlaps(r1)){
|
|
|
|
checkCollide(solid, sc);
|
|
|
|
//break out of loop when this object hits something
|
|
|
|
if(!solid.isAdded()) return;
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 14:36:33 -04:00
|
|
|
}
|
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
Log.info(Time.elapsed());
|
2020-05-01 14:36:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void end(){
|
2020-05-01 14:49:58 -04:00
|
|
|
if(tree == null) return;
|
2020-05-01 14:36:33 -04:00
|
|
|
|
|
|
|
//processes anything that happened
|
|
|
|
for(int i = 0; i < collisions.size; i += 2){
|
2020-05-01 16:35:18 -04:00
|
|
|
Hitboxc a = collisions.items[i];
|
|
|
|
Hitboxc b = collisions.items[i + 1];
|
2020-05-01 14:36:33 -04:00
|
|
|
|
|
|
|
//TODO incorrect
|
|
|
|
float cx = (a.x() + b.x())/2f, cy = (a.y() + b.y())/2f;
|
|
|
|
|
|
|
|
a.collision(b, cx, cy);
|
|
|
|
b.collision(a, cx, cy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void reset(){
|
2020-05-01 14:49:58 -04:00
|
|
|
tree = null;
|
|
|
|
insertEntities.clear();
|
|
|
|
checkEntities.clear();
|
2020-05-01 14:36:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void init(){
|
|
|
|
reset();
|
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
tree = new QuadTree<>(new Rect(-finalWorldBounds, -finalWorldBounds, world.width() * tilesize + finalWorldBounds * 2, world.height() * tilesize + finalWorldBounds * 2));
|
2020-05-01 14:36:33 -04:00
|
|
|
}
|
|
|
|
|
2020-05-01 16:35:18 -04:00
|
|
|
private void checkCollide(Hitboxc a, Hitboxc b){
|
2020-05-01 14:49:58 -04:00
|
|
|
|
|
|
|
a.hitbox(this.r1);
|
|
|
|
b.hitbox(this.r2);
|
|
|
|
|
|
|
|
r1.x += (a.lastX() - a.getX());
|
|
|
|
r1.y += (a.lastY() - a.getY());
|
|
|
|
r2.x += (b.lastX() - b.getX());
|
|
|
|
r2.y += (b.lastY() - b.getY());
|
|
|
|
|
|
|
|
float vax = a.getX() - a.lastX();
|
|
|
|
float vay = a.getY() - a.lastY();
|
|
|
|
float vbx = b.getX() - b.lastX();
|
|
|
|
float vby = b.getY() - b.lastY();
|
|
|
|
|
|
|
|
if(a != b && a.collides(b)){
|
|
|
|
l1.set(a.getX(), a.getY());
|
|
|
|
boolean collide = r1.overlaps(r2) || collide(r1.x, r1.y, r1.width, r1.height, vax, vay,
|
|
|
|
r2.x, r2.y, r2.width, r2.height, vbx, vby, l1);
|
|
|
|
if(collide){
|
|
|
|
collisions.add(a);
|
|
|
|
collisions.add(b);
|
|
|
|
//a.collision(b, l1.x, l1.y);
|
|
|
|
//b.collision(a, l1.x, l1.y);
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 14:36:33 -04:00
|
|
|
}
|
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
private boolean collide(float x1, float y1, float w1, float h1, float vx1, float vy1,
|
|
|
|
float x2, float y2, float w2, float h2, float vx2, float vy2, Vec2 out){
|
|
|
|
float px = vx1, py = vy1;
|
2020-05-01 14:36:33 -04:00
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
vx1 -= vx2;
|
|
|
|
vy1 -= vy2;
|
2020-05-01 14:36:33 -04:00
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
float xInvEntry, yInvEntry;
|
|
|
|
float xInvExit, yInvExit;
|
2020-05-01 14:36:33 -04:00
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
if(vx1 > 0.0f){
|
|
|
|
xInvEntry = x2 - (x1 + w1);
|
|
|
|
xInvExit = (x2 + w2) - x1;
|
|
|
|
}else{
|
|
|
|
xInvEntry = (x2 + w2) - x1;
|
|
|
|
xInvExit = x2 - (x1 + w1);
|
|
|
|
}
|
2020-05-01 14:36:33 -04:00
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
if(vy1 > 0.0f){
|
|
|
|
yInvEntry = y2 - (y1 + h1);
|
|
|
|
yInvExit = (y2 + h2) - y1;
|
|
|
|
}else{
|
|
|
|
yInvEntry = (y2 + h2) - y1;
|
|
|
|
yInvExit = y2 - (y1 + h1);
|
|
|
|
}
|
2020-05-01 14:36:33 -04:00
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
float xEntry, yEntry;
|
|
|
|
float xExit, yExit;
|
2020-05-01 14:36:33 -04:00
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
xEntry = xInvEntry / vx1;
|
|
|
|
xExit = xInvExit / vx1;
|
|
|
|
|
|
|
|
yEntry = yInvEntry / vy1;
|
|
|
|
yExit = yInvExit / vy1;
|
|
|
|
|
|
|
|
float entryTime = Math.max(xEntry, yEntry);
|
|
|
|
float exitTime = Math.min(xExit, yExit);
|
2020-05-01 14:36:33 -04:00
|
|
|
|
2020-05-01 14:49:58 -04:00
|
|
|
if(entryTime > exitTime || xExit < 0.0f || yExit < 0.0f || xEntry > 1.0f || yEntry > 1.0f){
|
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
float dx = x1 + w1 / 2f + px * entryTime;
|
|
|
|
float dy = y1 + h1 / 2f + py * entryTime;
|
|
|
|
|
|
|
|
out.set(dx, dy);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-01 14:36:33 -04:00
|
|
|
}
|
|
|
|
}
|