-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.cpp
More file actions
52 lines (46 loc) · 1.18 KB
/
Copy pathSphere.cpp
File metadata and controls
52 lines (46 loc) · 1.18 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
#include "template.h"
#include "scene.h"
#include "Sphere.h"
void Sphere::Move(float deltaTime, Tmpl8::Scene& scene)
{
m_direction = float3(0, -1, 0);
m_velocity -= float3(0, 1, 0) * m_acceleration;
m_center += GetDelta(deltaTime);
if (m_center.y < 0.f)
{
m_center.y = 1.1f;
m_velocity = 0;
}
ResolveCollisions(deltaTime, scene);
}
float3 Sphere::GetDelta(float deltaTime) const
{
return m_velocity * m_speed * deltaTime;
}
void Sphere::Reflect()
{
m_velocity.y = -m_velocity.y;
}
void Sphere::ResolveCollisions(float /*deltaTime*/, Tmpl8::Scene& scene)
{
float3 u, v;
GetPerpendiculars(m_direction, u, v);
u *= m_radius;
v *= m_radius;
constexpr size_t COUNT = 4;
float3 checkpoints[COUNT] = {
u, v, -u, -v
};
for (size_t i = 0; i < COUNT; ++i)
{
const float3 origin = m_center + m_direction * (m_radius + 0.01f);
Ray ray(origin + checkpoints[i], m_direction, length(m_velocity));
scene.FindNearest(ray);
if (ray.m_length < m_radius)
{
m_center -= ray.m_direction * (m_radius - ray.m_length);
Reflect();
return;
}
}
}