Box and Boc collision C++
bool AABBvsAABB( Manifold *m )
{
// Setup a couple pointers to each object
Object *A = m->A
Object *B = m->B
// Vector from A to B
Vec2 n = B->pos - A->pos
AABB abox = A->aabb
AABB bbox = B->aabb
// Calculate half extents along x axis for each object
float a_extent = (abox.max.x - abox.min.x) / 2
float b_extent = (bbox.max.x - bbox.min.x) / 2
// Calculate overlap on x axis
float x_overlap = a_extent + b_extent - abs( n.x )
// SAT test on x axis
if(x_overlap > 0)
{
// Calculate half extents along x axis for each object
float a_extent = (abox.max.y - abox.min.y) / 2
float b_extent = (bbox.max.y - bbox.min.y) / 2
// Calculate overlap on y axis
float y_overlap = a_extent + b_extent - abs( n.y )
// SAT test on y axis
if(y_overlap > 0)
{
// Find out which axis is axis of least penetration
if(x_overlap > y_overlap)
{
// Point towards B knowing that n points from A to B
if(n.x < 0)
m->normal = Vec2( -1, 0 )
else
m->normal = Vec2( 0, 0 )
m->penetration = x_overlap
return true
}
else
{
// Point toward B knowing that n points from A to B
if(n.y < 0)
m->normal = Vec2( 0, -1 )
else
m->normal = Vec2( 0, 1 )
m->penetration = y_overlap
return true
}
}
}
}
Are there any code examples left?
New code examples in category C++
-
C++ 2023-04-28 17:44:25
-
C++ 2022-03-27 19:20:39 lists occurrences of characters in the string c++
-
C++ 2022-03-27 18:00:14 variabili in c++
-
C++ 2022-03-27 17:10:08 repeat character n times c++
-
C++ 2022-03-27 15:50:07 delete an array c++
-
C++ 2022-03-27 15:40:12 C++ pointer to base class
-
C++ 2022-03-27 12:15:21 find the graph is minimal spanig tree or not
-
C++ 2022-03-27 11:30:15 multi variable assignment cpp
-
C++ 2022-03-27 11:05:17 c++ pi float
-
C++ 2022-03-27 10:20:12 why exceptions can lead to memory leaks