Jo Engine 2026.11.1
Jo Sega Saturn Engine
Loading...
Searching...
No Matches
physics.h
Go to the documentation of this file.
1/*
2** Jo Sega Saturn Engine
3** Copyright (c) 2012-2026, 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*/
37
38#ifndef __JO_PHYSICS_H__
39# define __JO_PHYSICS_H__
40
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
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{
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*/
#define JO_MAX(A, B)
Get the bigger value between A and B.
Definition math.h:274
#define JO_ABS(X)
Get the absolute value of X.
Definition math.h:257
static __jo_force_inline bool jo_is_float_equals_zero(const float f)
Check if float almost equals 0.
Definition math.h:2145
#define JO_MIN(A, B)
Get the smaller value between A and B.
Definition math.h:268
#define JO_CHANGE_SIGN(X)
Change the sign of X.
Definition math.h:279
float air_acceleration_strength
Definition physics.h:44
float deceleration_strength
Definition physics.h:47
float braking_animation_speed
Definition physics.h:50
float friction
Definition physics.h:46
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
static __jo_force_inline void jo_physics_apply_friction(jo_sidescroller_physics_params *physics)
Friction computation.
Definition physics.h:64
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
float max_speed_y
Definition physics.h:49
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
static __jo_force_inline void jo_physics_apply_gravity(jo_sidescroller_physics_params *physics)
Gravity computation.
Definition physics.h:80
float jump_speed_y
Definition physics.h:52
float speed_y
Definition physics.h:54
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
static __jo_force_inline void jo_physics_jump(jo_sidescroller_physics_params *physics)
Jump computation.
Definition physics.h:72
float gravity
Definition physics.h:51
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
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
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
bool is_in_air
Definition physics.h:43
float speed
Definition physics.h:53
float acceleration_strength
Definition physics.h:45
float max_speed
Definition physics.h:48
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
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
static __jo_force_inline bool jo_physics_is_standing(jo_sidescroller_physics_params *physics)
Check if we doesn't move.
Definition physics.h:131
Definition physics.h:42
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39