Hi guys,
I found an algorithm that gets the time value of a collision from this site
Here is a quick rewrite
bool DynamicCollision::sweptAABBvsAABB(const AABB& a, const AABB& b, float &t, Vec2 &normal){
const float x_inner_dist=abs(a.pos.x-b.pos.x)-(a.getExtents().x+b.getExtents().x);
const float x_outer_dist=abs(a.pos.x-b.pos.x)+(a.getExtents().x+b.getExtents().x);
const float y_inner_dist=abs(a.pos.y-b.pos.y)-(a.getExtents().y+b.getExtents().y);
const float y_outer_dist=abs(a.pos.y-b.pos.y)+(a.getExtents().y+b.getExtents().y);
const Vec2 rel_vel=b.vel-a.vel;
const float x_inner_time=x_inner_dist/rel_vel.x;
const float x_outer_time=x_outer_dist/rel_vel.x;
const float y_inner_time=y_inner_dist/rel_vel.y;
const float y_outer_time=y_outer_dist/rel_vel.y;
const float entry_time=std::max(x_inner_time,y_inner_time);
const float exit_time=std::min(x_outer_time,y_outer_time);
if( entry_time>exit_time || x_inner_time<0.0f && y_inner_time<0.0f || x_inner_time>1.0f || y_inner_time>1.0f){
t=1.0f;
normal.set(0.0f,0.0f);
return false;
}
else{
if(x_inner_time>y_inner_time){
if(x_inner_dist<0.0f){
normal.set(1.0f,0.0f);
}
else{
normal.set(-1.0f,0.0f);
}
}
else{
if(y_inner_dist<0.0f){
normal.set(0.0f,1.0f);
}
else{
normal.set(0.0f,-1.0f);
}
}
}
t=entry_time;
return true;
}
I don't quite understand how to step forward the update with the "time" value of collision.
For example, the following does not work. The aabb accelerates for a little while, then gets positionally corrected from half way accros the screen.
for(auto& aabb: aabbs)
aabb->integrateVel(dt);
for(auto& aabb: aabbs){
float t=0.0f;
Vec2 normal;
if(DynamicCollision::sweptAABBvsAABB(*obstacle,*aabb,t,normal)){
aabb->pos+=t*aabb->vel;
}
else
aabb->integratePos(dt);
}
where
void integrateVel(dt){
vel+=gravity*dt;
}
void integratePos(dt){
pos+=vel*dt;
}
Could someone please explain to me how to step forward a swept test simulation while accounting for the time value?
Thanks,
Mike