Jo Engine  2023.08.26
Jo Sega Saturn Engine
physics.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2020, Johannes Fetz (johannesfetz@gmail.com)
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are met:
8 ** * Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** * Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** * Neither the name of the Johannes Fetz nor the
14 ** names of its contributors may be used to endorse or promote products
15 ** derived from this software without specific prior written permission.
16 **
17 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 ** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
38 #ifndef __JO_PHYSICS_H__
39 # define __JO_PHYSICS_H__
40 
41 typedef struct
42 {
43  bool is_in_air;
46  float friction;
48  float max_speed;
49  float max_speed_y;
51  float gravity;
52  float jump_speed_y;
53  float speed;
54  float speed_y;
56 
57 /*
58 ** PHYSICS COMPUTATIONS
59 */
60 
65 {
66  physics->speed -= JO_MIN(JO_ABS(physics->speed), physics->friction) * (physics->speed > 0 ? 1 : -1);
67 }
68 
73 {
74  physics->speed_y = physics->jump_speed_y;
75 }
76 
81 {
82  physics->speed_y += physics->gravity;
83  if (physics->speed_y > physics->max_speed_y)
84  physics->speed_y = physics->max_speed_y;
85 }
86 
91 {
92  physics->speed += (physics->is_in_air ? physics->air_acceleration_strength : physics->acceleration_strength);
93  physics->speed = JO_MIN(physics->speed, physics->max_speed);
94 }
95 
100 {
101  physics->speed -= (physics->is_in_air ? physics->air_acceleration_strength : physics->acceleration_strength);
102  physics->speed = JO_MAX(physics->speed, JO_CHANGE_SIGN(physics->max_speed));
103 }
104 
109 {
110  physics->speed -= physics->deceleration_strength;
111  physics->speed = JO_MAX(0, physics->speed);
112 }
113 
118 {
119  physics->speed += physics->deceleration_strength;
120  physics->speed = JO_MIN(physics->speed, 0);
121 }
122 
123 /*
124 ** PHYSICS STATES TEST
125 */
126 
132 {
133  return (jo_is_float_equals_zero(physics->speed));
134 }
135 
142 {
143  return (JO_ABS(physics->speed) < tolerance);
144 }
145 
151 {
152  return (physics->speed > 0.0f);
153 }
154 
160 {
161  return (physics->speed < 0.0f);
162 }
163 
169 {
170  if (jo_physics_is_going_on_the_left(physics))
171  return (physics->speed >= physics->braking_animation_speed);
172  return (physics->speed <= JO_CHANGE_SIGN(physics->braking_animation_speed));
173 }
174 
175 /*
176 ** HELPER
177 */
178 
183 {
184  physics->air_acceleration_strength = 0.09375f;
185  physics->acceleration_strength = 0.046875f;
186  physics->friction = 0.046875f;
187  physics->deceleration_strength = 0.5f;
188  physics->max_speed = 6.0f;
189  physics->max_speed_y = 16.0f;
190  physics->braking_animation_speed = 4.5f;
191  physics->gravity = 0.21875f;
192  physics->jump_speed_y = -7.0f;
193  physics->speed = 0.0f;
194  physics->speed_y = 0.0f;
195  physics->is_in_air = false;
196 };
197 
198 #endif /* !__JO_PHYSICS_H__ */
199 
200 /*
201 ** END OF FILE
202 */
jo_sidescroller_physics_params::speed_y
float speed_y
Definition: physics.h:54
JO_MAX
#define JO_MAX(A, B)
Get the bigger value between A and B.
Definition: math.h:242
jo_physics_accelerate_left
static __jo_force_inline void jo_physics_accelerate_left(jo_sidescroller_physics_params *physics)
Acceleration computation when going to the left.
Definition: physics.h:99
jo_physics_is_standing
static __jo_force_inline bool jo_physics_is_standing(jo_sidescroller_physics_params *physics)
Check if we doesn't move.
Definition: physics.h:131
jo_sidescroller_physics_params::speed
float speed
Definition: physics.h:53
jo_sidescroller_physics_params::friction
float friction
Definition: physics.h:46
jo_sidescroller_physics_params::max_speed_y
float max_speed_y
Definition: physics.h:49
jo_sidescroller_physics_params
Definition: physics.h:42
jo_sidescroller_physics_params::jump_speed_y
float jump_speed_y
Definition: physics.h:52
jo_is_float_equals_zero
static __jo_force_inline bool jo_is_float_equals_zero(const float f)
Check if float almost equals 0.
Definition: math.h:1723
jo_physics_apply_gravity
static __jo_force_inline void jo_physics_apply_gravity(jo_sidescroller_physics_params *physics)
Gravity computation.
Definition: physics.h:80
jo_physics_accelerate_right
static __jo_force_inline void jo_physics_accelerate_right(jo_sidescroller_physics_params *physics)
Acceleration computation when going to the right.
Definition: physics.h:90
jo_sidescroller_physics_params::braking_animation_speed
float braking_animation_speed
Definition: physics.h:50
JO_ABS
#define JO_ABS(X)
Get the absolute value of X.
Definition: math.h:225
jo_physics_init_for_sonic
static __jo_force_inline void jo_physics_init_for_sonic(jo_sidescroller_physics_params *physics)
Init physics engine for a Sonic like game.
Definition: physics.h:182
jo_physics_should_brake
static __jo_force_inline bool jo_physics_should_brake(jo_sidescroller_physics_params *physics)
Check if we should brake depending on the direction.
Definition: physics.h:168
__jo_force_inline
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: types.h:39
jo_sidescroller_physics_params::is_in_air
bool is_in_air
Definition: physics.h:43
JO_CHANGE_SIGN
#define JO_CHANGE_SIGN(X)
Change the sign of X.
Definition: math.h:247
jo_physics_is_going_on_the_right
static __jo_force_inline bool jo_physics_is_going_on_the_right(jo_sidescroller_physics_params *physics)
Check if we going on the right.
Definition: physics.h:150
jo_physics_jump
static __jo_force_inline void jo_physics_jump(jo_sidescroller_physics_params *physics)
Jump computation.
Definition: physics.h:72
jo_sidescroller_physics_params::gravity
float gravity
Definition: physics.h:51
jo_sidescroller_physics_params::max_speed
float max_speed
Definition: physics.h:48
JO_MIN
#define JO_MIN(A, B)
Get the smaller value between A and B.
Definition: math.h:236
jo_physics_is_almost_standing
static __jo_force_inline bool jo_physics_is_almost_standing(jo_sidescroller_physics_params *physics, float tolerance)
Check if we almost doesn't move.
Definition: physics.h:141
jo_sidescroller_physics_params::acceleration_strength
float acceleration_strength
Definition: physics.h:45
jo_physics_decelerate_right
static __jo_force_inline void jo_physics_decelerate_right(jo_sidescroller_physics_params *physics)
Deceleration computation when going to the left.
Definition: physics.h:117
jo_sidescroller_physics_params::air_acceleration_strength
float air_acceleration_strength
Definition: physics.h:44
jo_physics_is_going_on_the_left
static __jo_force_inline bool jo_physics_is_going_on_the_left(jo_sidescroller_physics_params *physics)
Check if we going on the left.
Definition: physics.h:159
jo_physics_apply_friction
static __jo_force_inline void jo_physics_apply_friction(jo_sidescroller_physics_params *physics)
Friction computation.
Definition: physics.h:64
jo_physics_decelerate_left
static __jo_force_inline void jo_physics_decelerate_left(jo_sidescroller_physics_params *physics)
Deceleration computation when going to the right.
Definition: physics.h:108
jo_sidescroller_physics_params::deceleration_strength
float deceleration_strength
Definition: physics.h:47