Jo Engine 2026.11.6
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 jo_fixed speed;
67
68 speed = jo_float2fixed(physics->speed);
69 speed -= JO_MIN(JO_ABS(speed), jo_float2fixed(physics->friction)) * (speed > 0 ? 1 : -1);
70 physics->speed = jo_fixed2float(speed);
71}
72
77{
78 physics->speed_y = physics->jump_speed_y;
79}
80
85{
86 physics->speed_y = jo_fixed2float(JO_MIN(jo_float2fixed(physics->speed_y) + jo_float2fixed(physics->gravity),
87 jo_float2fixed(physics->max_speed_y)));
88}
89
94{
95 jo_fixed speed;
96
97 speed = jo_float2fixed(physics->speed) + jo_float2fixed(physics->is_in_air ? physics->air_acceleration_strength :
98 physics->acceleration_strength);
99 physics->speed = jo_fixed2float(JO_MIN(speed, jo_float2fixed(physics->max_speed)));
100}
101
106{
107 jo_fixed speed;
108
109 speed = jo_float2fixed(physics->speed) - jo_float2fixed(physics->is_in_air ? physics->air_acceleration_strength :
110 physics->acceleration_strength);
111 physics->speed = jo_fixed2float(JO_MAX(speed, -jo_float2fixed(physics->max_speed)));
112}
113
118{
119 physics->speed = jo_fixed2float(JO_MAX(0, jo_float2fixed(physics->speed) - jo_float2fixed(physics->deceleration_strength)));
120}
121
126{
127 physics->speed = jo_fixed2float(JO_MIN(jo_float2fixed(physics->speed) + jo_float2fixed(physics->deceleration_strength), 0));
128}
129
130/*
131** PHYSICS STATES TEST
132*/
133
142
149{
150 // (|speed| < tolerance: the bits of 2 positive floats are in the same order as the floats)
151 if (__jo_float_bits(tolerance) & 0x80000000u)
152 return (false);
153 return ((__jo_float_bits(physics->speed) & 0x7FFFFFFFu) < __jo_float_bits(tolerance));
154}
155
164
173
179{
181 return (jo_float2fixed(physics->speed) >= jo_float2fixed(physics->braking_animation_speed));
182 return (jo_float2fixed(physics->speed) <= -jo_float2fixed(physics->braking_animation_speed));
183}
184
185/*
186** HELPER
187*/
188
193{
194 physics->air_acceleration_strength = 0.09375f;
195 physics->acceleration_strength = 0.046875f;
196 physics->friction = 0.046875f;
197 physics->deceleration_strength = 0.5f;
198 physics->max_speed = 6.0f;
199 physics->max_speed_y = 16.0f;
200 physics->braking_animation_speed = 4.5f;
201 physics->gravity = 0.21875f;
202 physics->jump_speed_y = -7.0f;
203 physics->speed = 0.0f;
204 physics->speed_y = 0.0f;
205 physics->is_in_air = false;
206};
207
208#endif /* !__JO_PHYSICS_H__ */
209
210/*
211** END OF FILE
212*/
static __jo_force_inline unsigned int __jo_float_bits(const float f)
The bits of a float (Internal usage).
Definition math.h:516
static __jo_force_inline int __jo_float_sign(const float x)
The sign of a float, in integers on its bits (Internal usage).
Definition math.h:591
#define JO_MAX(A, B)
Get the bigger value between A and B.
Definition math.h:274
static __jo_force_inline float jo_fixed2float(const jo_fixed x)
Convert jo engine fixed to float.
Definition math.h:573
#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:2208
#define JO_MIN(A, B)
Get the smaller value between A and B.
Definition math.h:268
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:105
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:148
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:117
static __jo_force_inline void jo_physics_apply_gravity(jo_sidescroller_physics_params *physics)
Gravity computation.
Definition physics.h:84
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:93
static __jo_force_inline void jo_physics_jump(jo_sidescroller_physics_params *physics)
Jump computation.
Definition physics.h:76
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:169
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:192
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:160
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:178
static __jo_force_inline void jo_physics_decelerate_right(jo_sidescroller_physics_params *physics)
Deceleration computation when going to the left.
Definition physics.h:125
static __jo_force_inline bool jo_physics_is_standing(jo_sidescroller_physics_params *physics)
Check if we doesn't move.
Definition physics.h:138
Definition physics.h:42
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39
int jo_fixed
Fixed point Q16.16 number.
Definition types.h:59