-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysics.cpp
More file actions
81 lines (69 loc) · 1.89 KB
/
Copy pathPhysics.cpp
File metadata and controls
81 lines (69 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <cmath>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include "Physics.hpp"
// Compare objects
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;
}
return false;
}
return false;
}
return false;
};
// Function to convert Vec2 to sf::Vector2f
sf::Vector2f toSF(const Vec2& v) {
return sf::Vector2f(v.x, v.y);
}
// Function to get dot product
float Dot(const Vec2& a, const Vec2& b) {
return a.x * b.x + a.y * b.y;
}