Dumb-ways-to-memorize
2D game
Enumerations | Functions
dumb_physics.h File Reference
#include "entity.h"
+ Include dependency graph for dumb_physics.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  physics_laws_t {
  PHYSICS_NULL = 0,
  PHYSICS_BASE_FRICTION = 1,
  PHYSICS_BASE_SPEED_X = 5,
  PHYSICS_BASE_SPEED_Y = 10,
  PHYSICS_MAX_SPEED = 16,
  PHYSICS_MAX
}
 

Functions

void RunPhysics ()
 
int CheckCollision (entity_t *self, entity_t *other)
 
void DoCollision (entity_t *self, entity_t *other)
 
void ApplySpeedLimit (vec2_t *a)
 
void ApplyBounds (entity_t *ent)
 
void ApplyFriction (vec2_t *a)
 

Enumeration Type Documentation

Defines the Preset Physics values

Enumerator
PHYSICS_NULL 
PHYSICS_BASE_FRICTION 

An enum constant representing the physics base friction option

PHYSICS_BASE_SPEED_X 

An enum constant representing the physics base speed in x direction

PHYSICS_BASE_SPEED_Y 

An enum constant representing the physics base speed in y direction

PHYSICS_MAX_SPEED 

An enum constant representing the physics maximum speed

PHYSICS_MAX 

Definition at line 7 of file dumb_physics.h.

8 {
9  PHYSICS_NULL = 0,
10  PHYSICS_BASE_FRICTION = 1, /**< An enum constant representing the physics base friction option */
11  PHYSICS_BASE_SPEED_X = 5, /**< An enum constant representing the physics base speed in x direction */
12  PHYSICS_BASE_SPEED_Y = 10, /**< An enum constant representing the physics base speed in y direction */
13  PHYSICS_MAX_SPEED = 16, /**< An enum constant representing the physics maximum speed */
physics_laws_t
Definition: dumb_physics.h:7

Function Documentation

void ApplyBounds ( entity_t *  ent)

Applies the bounds to specified ent, defined by the screen dimensions.

Parameters
[in,out]entIf non-null, the ent.
Author
Anthony Rios
Date
3/29/2016

Definition at line 168 of file dumb_physics.c.

References gScreenHeight, gScreenWidth, and PHYSICS_MAX_SPEED.

Referenced by RunPhysics().

169 {
170  if(ent->mPosition.x < 0)
171  {
172  ent->mPosition.x = 0;
173  ent->mVelocity.x = 0;
174  ent->mAccel.x = 0;
175  } else if (ent->mPosition.x > gScreenWidth)
176  {
177  ent->mPosition.x = gScreenWidth - PHYSICS_MAX_SPEED;
178  ent->mVelocity.x = 0;
179  ent->mAccel.x = 0;
180  }
181  if(ent->mPosition.y < 0)
182  {
183  ent->mPosition.y = 0;
184  ent->mVelocity.y = 0;
185  ent->mAccel.y = 0;
186  } else if (ent->mPosition.y > gScreenHeight)
187  {
188  ent->mPosition.y = gScreenHeight - PHYSICS_MAX_SPEED;
189  ent->mVelocity.y = 0;
190  ent->mAccel.y = 0;
191  }
192 }
int gScreenWidth
Definition: graphics.c:16
int gScreenHeight
Definition: graphics.c:17
void ApplyFriction ( vec2_t a)

Applies the friction to vector a, for the nonzero values. Uses Physics_base_friction for friction coefficient.

Parameters
[in,out]aIf non-null, the vec2_t to process.
Author
Anthony Rios
Date
3/29/2016

Definition at line 194 of file dumb_physics.c.

References PHYSICS_BASE_FRICTION, vec2_t::x, and vec2_t::y.

Referenced by RunPhysics().

195 {
196  if(abs(a->x))
197  {
199  }
200  if(abs(a->y))
201  {
203  }
204 }
int y
Definition: globals.h:22
int x
Definition: globals.h:21
void ApplySpeedLimit ( vec2_t a)

Applies the speed limit to vector a, defined by physics max speed.

Parameters
[in,out]aIf non-null, the vec2_t to process.
Author
Anthony Rios
Date
3/29/2016

Definition at line 162 of file dumb_physics.c.

References PHYSICS_MAX_SPEED, vec2_t::x, and vec2_t::y.

Referenced by RunPhysics().

163 {
164  a->x = abs(a->x) > PHYSICS_MAX_SPEED ? (a->x < 0 ? -PHYSICS_MAX_SPEED : PHYSICS_MAX_SPEED) : a->x;
165  a->y = abs(a->y) > PHYSICS_MAX_SPEED ? (a->y < 0 ? -PHYSICS_MAX_SPEED : PHYSICS_MAX_SPEED) : a->y;
166 }
int y
Definition: globals.h:22
int x
Definition: globals.h:21
int CheckCollision ( entity_t *  self,
entity_t *  other 
)

Check collision between entities. (AABB)

Parameters
[in,out]selfIf non-null, the class instance that this method operates on.
[in,out]otherIf non-null, the other.
Returns
An int 1 if colliding, 0 if not.
Author
Anthony Rios
Date
3/20/2016

Definition at line 57 of file dumb_physics.c.

References COLLISION_TYPE_CLIP.

Referenced by RunPhysics().

58 {
59  if(self->mCollisionType == COLLISION_TYPE_CLIP || other->mCollisionType == COLLISION_TYPE_CLIP)
60  {
61  return 0;
62  }
63  if(!self->mSprites || !self->mSprites)
64  {
65  return 0;
66  }
67  if(self->mPosition.x + self->mSprites[0]->mSize.x < other->mPosition.x || self->mPosition.y + self->mSprites[0]->mSize.y < other->mPosition.y
68  || self->mPosition.x > other->mPosition.x + other->mSprites[0]->mSize.x || self->mPosition.y > other->mPosition.y + other->mSprites[0]->mSize.y)
69  {
70  return 0;
71  }
72  return 1;
73 }
void DoCollision ( entity_t *  self,
entity_t *  other 
)

Executes the collision operation. Place self farthest x or y away from other, vice versa. And negative the velocity to half of orginal collision vel, for bounce back.

Parameters
[in,out]selfIf non-null, the class instance that this method operates on.
[in,out]otherIf non-null, the other.
Author
Anthony Rios
Date
3/20/2016

Definition at line 75 of file dumb_physics.c.

References COLLISION_TYPE_RAGDOLL, Vec2Add(), vec2_t::x, and vec2_t::y.

Referenced by RunPhysics().

76 {
77  vec2_t self_min, self_max, self_res, other_min, other_max, other_res;
78  int left, right, bottom, top;
79  if(self->Touch)
80  {
81  self->Touch(self, other, other->mCollisionType);
82  }
83  if(other->Touch)
84  {
85  other->Touch(other, self, self->mCollisionType);
86  }
87  if(strcmp(self->mName, other->mName))
88  {
89  printf("%s collided with %s \n", self->mName, other->mName);
90  }
91  self_min = self->mPosition;
92  Vec2Add(&self->mPosition, &self->mSprites[0]->mSize, &self_max);
93  other_min = other->mPosition;
94  Vec2Add(&other->mPosition, &other->mSprites[0]->mSize, &other_max);
95 
96  left = other_min.x - self_max.x;
97  right = other_max.x - self_min.x;
98  top = other_min.y - self_max.y;
99  bottom = other_max.y - self_min.y;
100  if(abs(left) < right)
101  {
102  self_res.x = left;
103  other_res.x = right;
104  } else
105  {
106  self_res.x = right;
107  other_res.x = left;
108  }
109 
110  if(abs(top) < bottom)
111  {
112  self_res.y = top;
113  other_res.y = bottom;
114  } else
115  {
116  self_res.y = bottom;
117  other_res.y = top;
118  }
119 
120  if(abs(self_res.x) < abs(self_res.y))
121  {
122  self_res.y = 0;
123  } else
124  {
125  self_res.x = 0;
126  }
127 
128  if(abs(other_res.x) < abs(other_res.y))
129  {
130  other_res.y = 0;
131  } else
132  {
133  other_res.x = 0;
134  }
135 
136  if(self->mCollisionType == COLLISION_TYPE_RAGDOLL)
137  {
138  Vec2Add(&self->mPosition, &self_res, &self->mPosition);
139  if(self_res.x)
140  {
141  self->mVelocity.x = -self->mVelocity.x/2;
142  } else
143  {
144  self->mVelocity.y = -self->mVelocity.y/2;
145  }
146  }
147  if(other->mCollisionType == COLLISION_TYPE_RAGDOLL)
148  {
149  Vec2Add(&other->mPosition, &other_res, &other->mPosition);
150  if(other_res.x)
151  {
152  other->mVelocity.x = -self->mVelocity.x/2;
153  } else
154  {
155  other->mVelocity.y = -self->mVelocity.y/2;
156  }
157 
158  }
159 
160 }
int y
Definition: globals.h:22
int x
Definition: globals.h:21
void Vec2Add(vec2_t *A, vec2_t *B, vec2_t *C)
Definition: mymath.c:20
Definition: globals.h:19
void RunPhysics ( )

Executes physics. RunTime O( n(n+1)/2 ) Applies velocity, acceleration, gravity, and friction. Limits velocity, acceleration, and sets bounds.

Author
Anthony Rios
Date
3/20/2016

Definition at line 9 of file dumb_physics.c.

References ApplyBounds(), ApplyFriction(), ApplySpeedLimit(), CheckCollision(), COLLISION_TYPE_STATIC, DoCollision(), gEntities, MAX_ENTITIES, PHYSICS_LIMITER, Vec2Add(), and vec2_t::y.

Referenced by UpdatePlaying().

10 {
11  int i, j;
12  for(i = 0; i < MAX_ENTITIES; i++)
13  {
14  if(!gEntities[i].mName)
15  {
16  continue;
17  }
18  if(!gEntities[i].mCollisionType == COLLISION_TYPE_STATIC)
19  {
20  ApplyFriction(&gEntities[i].mVelocity);
21  gEntities[i].mPosition.x += gEntities[i].mVelocity.x/PHYSICS_LIMITER;
22  gEntities[i].mPosition.y += gEntities[i].mVelocity.y/PHYSICS_LIMITER;
23  Vec2Add(&gEntities[i].mAccel,&gEntities[i].mVelocity,&gEntities[i].mVelocity);
24  if(gEntities[i].mWeight)
25  {
26  gEntities[i].mAccel.y = gGravity.y;
27  }
28  ApplySpeedLimit(&gEntities[i].mVelocity);
29  ApplySpeedLimit(&gEntities[i].mAccel);
31  }
32 
33  //Vec2Add(&friction,&gEntities[i].mVelocity,&gEntities[i].mVelocity);
34 
35  }
36  //Collision Check
37  for(i = 0; i < MAX_ENTITIES; i++)
38  {
39  if(!gEntities[i].mName)
40  {
41  continue;
42  }
43  for(j = i; j < MAX_ENTITIES; j++)
44  {
45  if(i == j || !gEntities[j].mName)
46  {
47  continue;
48  }
49  if(CheckCollision(&gEntities[i], &gEntities[j]))
50  {
52  }
53  }
54  }
55 }
#define PHYSICS_LIMITER
Definition: globals.h:136
int y
Definition: globals.h:22
void ApplyFriction(vec2_t *a)
Definition: dumb_physics.c:194
int CheckCollision(entity_t *self, entity_t *other)
Definition: dumb_physics.c:57
vec2_t gGravity
Definition: dumb_physics.c:7
void ApplyBounds(entity_t *ent)
Definition: dumb_physics.c:168
void DoCollision(entity_t *self, entity_t *other)
Definition: dumb_physics.c:75
#define MAX_ENTITIES
Definition: entity.h:8
entity_t * gEntities
Definition: entity.c:11
void Vec2Add(vec2_t *A, vec2_t *B, vec2_t *C)
Definition: mymath.c:20
void ApplySpeedLimit(vec2_t *a)
Definition: dumb_physics.c:162