Quark Physics  1.0
2D Rigid and Soft Body Physics Engine
qplatformerbody.h
1 
2 /************************************************************************************
3  * MIT License
4  *
5  * Copyright (c) 2023 Eray Zesen
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * https://github.com/erayzesen/QuarkPhysics
25  *
26 **************************************************************************************/
27 
28 #ifndef QPLATFORMERBODY
29 #define QPLATFORMERBODY
30 
31 #include "../qrigidbody.h"
32 #include "../qcollision.h"
33 
40 class QPlatformerBody : public QRigidBody{
41 protected:
42 
43 
44  bool onFloor=false;
45  bool onCeiling=false;
46 
47 
48  //Platform Collider Bit Mask
49 
50  int platformLayersBit=0;
51 
52  //Floor
53  float maxFloorAngle=M_PI*0.25;
54 
55  //Movable Floors
56  float movingFloorSnapOffset=10.0f;
57  QRigidBody *lastMovableFloor=nullptr;
58 
59 
60 
61 
62  //Gravity
63 
64  QVector gravity=QVector(0,0.3);
65 
66  float gravityMultiplier=1.0f;
67 
68  QVector velocity=QVector::Zero();
69 
70  //Directions According to the Gravity
71  QVector upDirection=QVector::Up();
72  QVector rightDirection=QVector::Right();
73 
74  //Walk
75  float walkSpeed=3.0;
76 
77  //Controller Velocities
78  QVector horizontalVelocity=QVector::Zero();
79  QVector verticalVelocity=QVector::Zero();
80  bool isFalling=false;
81  bool isRising=false;
82 
83  int walkSide=0;
84 
85  float walkAccelerationRate=0.1f;
86  float walkDecelerationRate=0.1f;
87 
88  //Jump
89  bool prevJumpMode=false;
90  bool jumpMode=false;
91  float jumpForce=5.0f;
92  int maxJumpCount=2;
93  int currentJumpCount=0;
94  int jumpDurationFrameCount=30;
95  int jumpFrameCountDown=0;
96  float jumpGravityMultiplier=0.4f;
97  float jumpFallGravityMultiplier=1.0f;
98  bool jumpReleased=true;
99 
100 
101 
102  virtual void PostUpdate();
103 
104 
105 public:
106 
108  QBody* body;
109  QVector position;
110  float penetration;
111  QVector normal;
112  CollisionTestInfo(QBody *body=nullptr,QVector position=QVector::Zero(),float penetration=0.0f,QVector normal=QVector::Zero() ): body(body), position(position),penetration(penetration),normal(normal) {}
113  };
114 
115  QPlatformerBody();
116 
117 
118 
119  //Floor
120 
127 
132  float GetMovingFloorSnapOffset();
133 
139  QPlatformerBody * SetFloorMaxAngle(float value);
144  float GetFloorMaxAngle();
145 
152 
157  float GetFloorMaxAngleDegree();
158 
163  bool GetIsOnFloor();
168  bool GetIsOnCeiling();
169 
170 
171  //Gravity
178 
184 
190  QPlatformerBody * SetGravityMultiplier(float value);
195  float GetGravityMultiplier();
196 
197  //Walk
203  QPlatformerBody * SetWalkSpeed(float value);
204 
209  float GetWalkSpeed();
210 
217 
222  float GetWalkAcelerationRate();
223 
230 
235  float GetWalkDecelerationRate();
236 
242  QPlatformerBody * Walk(int side);
243 
244  //Controller Velocities
245 
252 
258 
265 
271 
276  bool GetIsFalling();
277 
282  bool GetIsRising();
283 
284  //Jump
291 
297 
304 
309  float GetJumpGravityMultiplier();
310 
317 
323 
329  QPlatformerBody * SetMaxJumpCount(int value);
330 
335  int GetMaxJumpCount();
336 
343  QPlatformerBody * Jump(float force,bool unconditional=false);
344 
350 
355  bool GetIsJumping();
360  bool GetIsJumpReleased();
361 
362  //Platforms
372  QPlatformerBody * SetSpecificPlatformLayers(int layersBit);
373 
383 
390  QPlatformerBody::CollisionTestInfo GetPlatformCollisions(QVector testPosition,QVector nearestOnAxis=QVector::Zero() );
391 
398 
405 
412 
419 
420 
421 
422 
423 
424 
425 
426 
427 
428 };
429 
430 #endif //QPLATFORMERBODY
QBody objects are the base class for all types of bodies. Any class derived from QBody shares these m...
Definition: qbody.h:43
QPlatformerBody provides a ready-to-use foundation for character physics in platformer games....
Definition: qplatformerbody.h:40
QVector GetControllerHorizontalVelocity()
Gets the horizontal velocity controlled by the character physics.
Definition: qplatformerbody.cpp:308
QPlatformerBody * SetMaxJumpCount(int value)
Sets the maximum number of jumps allowed.
Definition: qplatformerbody.cpp:147
float GetFloorMaxAngleDegree()
Gets the maximum angle for the floor in degrees.
Definition: qplatformerbody.cpp:186
float GetJumpGravityMultiplier()
Gets the gravity multiplier during a jump.
Definition: qplatformerbody.cpp:131
bool GetIsOnFloor()
Checks if the body is currently on the floor.
Definition: qplatformerbody.cpp:621
int GetSpecificPlatformLayers()
Gets specific platform layers bit. This determines which platform layers the platformer physics will ...
Definition: qplatformerbody.cpp:164
bool GetIsRising()
Checks if the body is rising.
Definition: qplatformerbody.cpp:375
float GetMovingFloorSnapOffset()
Gets the snap offset for moving floors.
Definition: qplatformerbody.cpp:56
float GetWalkAcelerationRate()
Gets the walking acceleration rate.
Definition: qplatformerbody.cpp:102
QPlatformerBody * SetWalkAcelerationRate(float value)
Sets the walking acceleration rate.
Definition: qplatformerbody.cpp:96
QPlatformerBody * SetJumpGravityMultiplier(float value)
Sets the gravity multiplier during a jump.
Definition: qplatformerbody.cpp:125
QVector GetControllerVerticalVelocity()
Gets the vertical velocity controlled by the character physics.
Definition: qplatformerbody.cpp:319
int GetJumpDurationFrameCount()
Gets the duration for a jump in frames.
Definition: qplatformerbody.cpp:120
QPlatformerBody::CollisionTestInfo GetLeftWall(float offset)
Checks for a collision with a wall on the left.
Definition: qplatformerbody.cpp:254
float GetWalkDecelerationRate()
Gets the walking deceleration rate.
Definition: qplatformerbody.cpp:290
QPlatformerBody * SetSpecificPlatformLayers(int layersBit)
Sets specific platform layers bit. This determines which platform layers the platformer physics will ...
Definition: qplatformerbody.cpp:158
virtual void PostUpdate()
Definition: qplatformerbody.cpp:390
QPlatformerBody::CollisionTestInfo GetFloor(float offset)
Checks for a collision with the floor.
Definition: qplatformerbody.cpp:259
QPlatformerBody * SetControllerHorizontalVelocity(QVector value)
Sets the horizontal velocity controlled by the character physics.
Definition: qplatformerbody.cpp:302
QPlatformerBody * Jump(float force, bool unconditional=false)
Initiates a jump with the specified force.
Definition: qplatformerbody.cpp:324
float GetJumpFallGravityMultiplier()
Gets the gravity multiplier for the falling phase of a jump.
Definition: qplatformerbody.cpp:142
QPlatformerBody * SetControllerVerticalVelocity(QVector value)
Sets the vertical velocity controlled by the character physics.
Definition: qplatformerbody.cpp:313
float GetGravityMultiplier()
Gets the gravity multiplier.
Definition: qplatformerbody.cpp:80
QPlatformerBody * SetGravityMultiplier(float value)
Sets the gravity multiplier.
Definition: qplatformerbody.cpp:74
QPlatformerBody::CollisionTestInfo GetCeiling(float offset)
Checks for a collision with the ceiling.
Definition: qplatformerbody.cpp:275
QPlatformerBody * SetJumpDurationFrameCount(int value)
Sets the duration for a jump in frames.
Definition: qplatformerbody.cpp:114
QPlatformerBody * SetFloorMaxAngle(float value)
Sets the maximum angle for the floor.
Definition: qplatformerbody.cpp:169
QPlatformerBody * SetWalkDecelerationRate(float value)
Sets the walking deceleration rate.
Definition: qplatformerbody.cpp:107
float GetWalkSpeed()
Gets the walking speed of the body.
Definition: qplatformerbody.cpp:91
QPlatformerBody * SetWalkSpeed(float value)
Sets the walking speed of the body.
Definition: qplatformerbody.cpp:85
QPlatformerBody * SetMovingFloorSnapOffset(float value)
Sets the snap offset for moving floors.
Definition: qplatformerbody.cpp:50
QPlatformerBody * ReleaseJump()
Releases the jump phase.
Definition: qplatformerbody.cpp:361
bool GetIsJumpReleased()
Checks if the jump phase has been released.
Definition: qplatformerbody.cpp:385
QPlatformerBody * SetJumpFallGravityMultiplier(float value)
Sets the gravity multiplier for the falling phase of a jump.
Definition: qplatformerbody.cpp:136
QVector GetGravity()
Gets the gravity vector.
Definition: qplatformerbody.cpp:69
QPlatformerBody * SetGravity(QVector value)
Sets the gravity vector.
Definition: qplatformerbody.cpp:61
QPlatformerBody * Walk(int side)
Moves the body in a specified horizontal direction.
Definition: qplatformerbody.cpp:295
bool GetIsJumping()
Checks if the body is currently jumping.
Definition: qplatformerbody.cpp:380
QPlatformerBody * SetFloorMaxAngleDegree(float value)
Sets the maximum angle for the floor in degrees.
Definition: qplatformerbody.cpp:180
int GetMaxJumpCount()
Gets the maximum number of jumps allowed.
Definition: qplatformerbody.cpp:153
bool GetIsFalling()
Checks if the body is falling.
Definition: qplatformerbody.cpp:370
bool GetIsOnCeiling()
Checks if the body is currently touching the ceiling.
Definition: qplatformerbody.cpp:626
float GetFloorMaxAngle()
Gets the maximum angle for the floor.
Definition: qplatformerbody.cpp:175
QPlatformerBody::CollisionTestInfo GetPlatformCollisions(QVector testPosition, QVector nearestOnAxis=QVector::Zero())
Performs a platform collision test.
Definition: qplatformerbody.cpp:191
QPlatformerBody::CollisionTestInfo GetRightWall(float offset)
Checks for a collision with a wall on the right.
Definition: qplatformerbody.cpp:238
QRigidBody is a type of body that is simulated with the dynamics of Rigid body. A rigid body is a typ...
Definition: qrigidbody.h:40
Definition: qplatformerbody.h:107
Definition: qvector.h:44