-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathPhysicsBody.h
181 lines (141 loc) · 3.99 KB
/
PhysicsBody.h
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* 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-6-3
* Update : 2019-1-18
* Author : scott.cgi
*/
#ifndef PHYSICS_BODY_H
#define PHYSICS_BODY_H
#include "Engine/Toolkit/Math/Matrix.h"
#include "Engine/Toolkit/HeaderUtils/Bitwise.h"
#include "Engine/Toolkit/Utils/Array.h"
#include "Engine/Toolkit/HeaderUtils/UserData.h"
typedef enum
{
PhysicsShape_NULL = 0,
PhysicsShape_Polygon = 1,
PhysicsShape_Line = 1 << 2,
PhysicsShape_Point = 1 << 3,
}
PhysicsShape;
typedef enum
{
/**
* Not add in physics world yet.
*/
PhysicsBodyState_OutsideWorld,
/**
* Can motion can collision.
*/
PhysicsBodyState_Normal,
/**
* No motion can collision.
*/
PhysicsBodyState_Fixed,
/**
* No motion no collision.
*/
PhysicsBodyState_Freeze,
}
PhysicsBodyState;
typedef struct PhysicsBody PhysicsBody;
struct PhysicsBody
{
UserData userData[1];
/**
* Used to identify PhysicsBody, default -1.
*/
int userID;
float positionX;
float positionY;
float velocityX;
float velocityY;
float accelerationX;
float accelerationY;
float rotationZ;
PhysicsShape shape;
/**
* PhysicsBody current state.
*/
PhysicsBodyState state;
/**
* Pow of 2, default 0.
* body can collision between different collisionGroup (no same bit).
*/
int collisionGroup;
/**
* Store born vertices.
*/
Array(float) vertexArr[1];
/**
* The vertices after transformed.
*/
Array(float) transformedVertexArr[1];
/**
* When body collision callback.
*/
void (*OnCollision)(PhysicsBody* self, PhysicsBody* other, float deltaSeconds);
};
/**
* Control PhysicsBody.
*/
struct APhysicsBody
{
/**
* Create body with shape and vertices.
*
* the vertexArr will copy into body,
* and the body's vertexArr and transformedVertexArr are same when init,
* and all data create by one malloc.
*
* if shape not support will return NULL.
*/
PhysicsBody* (*Create) (PhysicsShape shape, Array(float)* vertexArr);
/**
* Reset body's transformedVertexArr to vertexArr.
*/
void (*ResetVertices)(PhysicsBody* body);
/**
* Simulate body motion and update transformedVertexArr by position, rotation.
*/
void (*Update) (PhysicsBody* body, float deltaSeconds);
};
extern struct APhysicsBody APhysicsBody[1];
//----------------------------------------------------------------------------------------------------------------------
/**
* Check physicsBody whether has same bit in collisionGroup.
*/
static inline bool APhysicsBody_CheckCollisionGroup(PhysicsBody* physicsBody, int collisionGroup)
{
return ABitwise_Check(physicsBody->collisionGroup, collisionGroup);
}
/**
* Add collisionGroup to physicsBody.
*/
static inline void APhysicsBody_AddCollisionGroup(PhysicsBody* physicsBody, int collisionGroup)
{
ABitwise_Add(physicsBody->collisionGroup, collisionGroup);
}
/**
* Set collisionGroup to physicsBody.
*/
static inline void APhysicsBody_SetCollisionGroup(PhysicsBody* physicsBody, int collisionGroup)
{
ABitwise_Set(physicsBody->collisionGroup, collisionGroup);
}
/**
* Clear collisionGroup in physicsBody.
*/
static inline void APhysicsBody_ClearCollisionGroup(PhysicsBody* physicsBody, int collisionGroup)
{
ABitwise_Clear(physicsBody->collisionGroup, collisionGroup);
}
#endif