-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathPhysicsWorld.c
91 lines (73 loc) · 2.65 KB
/
PhysicsWorld.c
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
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2014-5-30
* Update : 2019-1-18
* Author : scott.cgi
*/
#include "Engine/Physics/PhysicsWorld.h"
#include "Engine/Toolkit/Utils/ArrayIntSet.h"
#include "Engine/Physics/PhysicsCollision.h"
static ArrayIntSet(PhysicsBody*) bodyInWorldSet[1] = AArrayIntSet_Init(PhysicsBody*, 20);
static void Update(float deltaSeconds)
{
for (int i = 0; i < bodyInWorldSet->elementList->size; ++i)
{
PhysicsBody* body = AArrayList_Get(bodyInWorldSet->elementList, i, PhysicsBody*);
if (body->state != PhysicsBodyState_Freeze)
{
// test collision
for (int fromIndex = i + 1; fromIndex < bodyInWorldSet->elementList->size; ++fromIndex)
{
PhysicsBody* otherBody = AArrayList_Get(bodyInWorldSet->elementList, fromIndex, PhysicsBody*);
if
(
otherBody->state != PhysicsBodyState_Freeze &&
// check collisionGroup whether has same bit
(body->collisionGroup & otherBody->collisionGroup) == 0 &&
APhysicsCollision->TestCollision(body, otherBody)
)
{
if (body->OnCollision != NULL)
{
body->OnCollision(body, otherBody, deltaSeconds);
}
if (otherBody->OnCollision != NULL)
{
otherBody->OnCollision(otherBody, body, deltaSeconds);
}
}
}
}
if (body->state != PhysicsBodyState_Freeze && body->state != PhysicsBodyState_Fixed)
{
// after test collision can update motion
APhysicsBody->Update(body, deltaSeconds);
}
}
}
static PhysicsBody* AddBody(PhysicsShape shape, Array(float)* vertexArr)
{
PhysicsBody* body = APhysicsBody->Create(shape, vertexArr);
AArrayIntSet->TryAdd(bodyInWorldSet, (intptr_t) body);
return body;
}
static void DestroyBody(PhysicsBody* body)
{
AArrayIntSet->TryRemove(bodyInWorldSet, (intptr_t) body);
free(body);
}
struct APhysicsWorld APhysicsWorld[1] =
{{
{0.0f, 0.0f},
AddBody,
DestroyBody,
Update,
}};