-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPhysObject.cpp
More file actions
95 lines (79 loc) · 3.06 KB
/
Copy pathPhysObject.cpp
File metadata and controls
95 lines (79 loc) · 3.06 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "PhysObject.h"
#include "raylib-cpp.hpp"
const float TARGET_FIXED_TIME_STEP = 1.0f / 30.0f;
PhysObject::PhysObject() : Position({ 0,0 }), Velocity({ 0,0 }), PendingAcceleration({ 0,0 }), Collider({ShapeType::NONE})
{
}
void PhysObject::ContinuousTick(float Delta)
{
Velocity += PendingAcceleration * Delta;
PendingAcceleration = {};
Position += Velocity * Delta;
}
void PhysObject::InstantaneousTick(float Delta)
{
Position += Velocity * Delta;
}
void PhysObject::DrawPhysicsCircle() const
{
switch (Collider.Type)
{
case ShapeType::NONE:
DrawPixel(Position.x, Position.y, raylib::Color::Red());
break;
case ShapeType::CIRCLE:
DrawCircleLines(Position.x, Position.y, Collider.CircleData.Radius, raylib::Color::Red());
break;
case ShapeType::AABB:
DrawRectangleLines(Position.x - Collider.AABBData.HalfExtents.x, Position.y - Collider.AABBData.HalfExtents.y, Collider.AABBData.HalfExtents.x * 2.0f,
Collider.AABBData.HalfExtents.y * 2.0f, raylib::Color::Red());
break;
}
}
//Applies continuous forces on objects of any mass equally like gravity
void PhysObject::AddAcceleration(const glm::vec2& Acceleration)
{
PendingAcceleration += Acceleration;
}
//Applies instantaneous forces on objects of any mass equally to emulate platform movement
void PhysObject::AddVelocity(const glm::vec2& velocity)
{
Velocity += velocity;
}
//Applies continuous forces that consider objects' mass
void PhysObject::AddForce(const glm::vec2& Force)
{
PendingAcceleration += Force / Mass;
}
//Applies instantaneous forces that consider objects' mass
void PhysObject::AddImpulses(const glm::vec2& Impulse)
{
Velocity += Impulse / Mass;
}
float PhysObject::ResolveCollisions(const glm::vec2& PositionA, const glm::vec2& VelocityA, float MassA, const glm::vec2& PositionB,
const glm::vec2& VelocityB, float MassB, float Elasticity, const glm::vec2& Normal)
{
//Calculates the relative Velocity
glm::vec2 relativeVelocity = VelocityB - VelocityA;
//Calculates the impulse magnitude
float impulseMagnitude = glm::dot(-(1.0f + Elasticity) * relativeVelocity, Normal) /
glm::dot(Normal, Normal * (1 / MassA + 1 / MassB));
//Returns the impulse to apply to both objects
return impulseMagnitude;
}
void PhysObject::ResolvePhysObjects(PhysObject& LeftHandSide, PhysObject& RightHandSide, float Elasticity, const glm::vec2& Normal, float Penetration)
{
//Calculates resolution impulse
//The Normal and Penetration vectors are passed by reference and will get updated
float impulseMagnitude = ResolveCollisions(LeftHandSide.Position, LeftHandSide.Velocity, LeftHandSide.Mass,
RightHandSide.Position, RightHandSide.Velocity, RightHandSide.Mass, Elasticity, Normal);
//Depenetrates objects
glm::vec2 minimumTranslationVector = Normal * (Penetration / 2.0f);
LeftHandSide.Position -= minimumTranslationVector;
RightHandSide.Position += minimumTranslationVector;
//Applies impulses to to update velocities after a collision
//Applies an equal but opposite force to the other
glm::vec2 impulse = Normal * impulseMagnitude;
LeftHandSide.AddImpulses(-impulse);
RightHandSide.AddImpulses(impulse);
}