PGE Engine
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
collision_checks.h
1 /*
2  * Platformer Game Engine by Wohlstand, a free platform for game making
3  * Copyright (c) 2015 Vitaly Novichkov <admin@wohlnet.ru>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef COLLISION_CHECKS_H
20 #define COLLISION_CHECKS_H
21 
22 #include "lvl_base_object.h"
23 #include <tgmath.h>
24 /*
25 PGE_PointF c1 = posRect.center();
26 PGE_RectF &r1 = posRect;
27 PGE_PointF cc = collided->posRect.center();
28 PGE_RectF rc = collided->posRect;
29 */
30 inline bool isCollideFloorToponly(PGE_Phys_Object &body, PGE_Phys_Object *collided)
31 {
32  float summSpeedY=(body._velocityY+body._velocityY_add)-(collided->_velocityY+collided->_velocityY_add);
33  float summSpeedYprv=body.colliding_ySpeed-collided->colliding_ySpeed;
34  return (
35  (
36  (summSpeedY >= 0.0)
37  &&
38  ( (body.posRect.bottom()-(summSpeedYprv>=0 ? 1 : 0 )) < collided->posRect.top()+summSpeedYprv)
39  &&
40  (
41  (body.posRect.left()<collided->posRect.right()-1 ) &&
42  (body.posRect.right()>collided->posRect.left()+1 )
43  )
44  )
45  ||
46  (body.posRect.bottom() <= collided->posRect.top())
47  );
48 }
49 
50 inline bool isCollideFloor(PGE_Phys_Object &body, PGE_Phys_Object *collided)
51 {
52  return (
53  (
54  (body._velocityY+body._velocityY_add >= 0.0)
55  &&
56  (floor(body.posRect.bottom())+collided->colliding_ySpeed < collided->posRect.top()+
57  fabs(body._velocityY+body._velocityX_add)
58  +2.0+body.colliding_ySpeed)
59  &&( !( (body.posRect.left()>=collided->posRect.right()-0.2) || (body.posRect.right() <= collided->posRect.left()+0.2) ) )
60  ) || (body.posRect.bottom() <= collided->posRect.top())
61  );
62 }
63 
64 inline bool isCollideCelling(PGE_Phys_Object &body, PGE_Phys_Object *collided, float _heightDelta=0.0f, bool forceCollideCenter=false)
65 {
66  return ( (
67  ( ((!forceCollideCenter)&&(body._velocityY+body._velocityY_add<0.0))||
68  (forceCollideCenter&&(body._velocityY+body._velocityY_add<=0.0)) )
69  &&
70  (body.posRect.top()+collided->colliding_ySpeed > collided->posRect.bottom()
71  +body.colliding_ySpeed-2.0+_heightDelta)
72  &&( !( (body.posRect.left()>=collided->posRect.right()-0.5 ) ||
73  (body.posRect.right() <= collided->posRect.left()+0.5 ) ) )
74  )
75  );
76 }
77 
78 inline bool isCollideLeft(PGE_Phys_Object &body, PGE_Phys_Object *collided)
79 {
80  return (body._velocityX+body._velocityX_add<0.0) && (body.posRect.center().x() > collided->posRect.center().x()) &&
81  (body.posRect.left()+collided->colliding_xSpeed >= collided->posRect.right()+body.colliding_xSpeed-collided->_width_half)
82  && ( (body.posRect.top()<collided->posRect.bottom())&&
83  (body.posRect.bottom()>collided->posRect.top()) );
84 }
85 
86 inline bool isCollideRight(PGE_Phys_Object &body, PGE_Phys_Object *collided)
87 {
88  return (body._velocityX+body._velocityX_add>0.0) && (body.posRect.center().x() < collided->posRect.center().x()) &&
89  ( body.posRect.right()+collided->colliding_xSpeed <= collided->posRect.left()+body.colliding_xSpeed+collided->_width_half)
90  && ( (body.posRect.top() < collided->posRect.bottom())&&
91  (body.posRect.bottom() > collided->posRect.top()) );
92 }
93 
94 inline double SL_HeightTopRight(PGE_Phys_Object &body, PGE_Phys_Object *collided)
95 {
96  return (fabs(collided->posRect.left()-body.posRect.right()))
97  *(collided->posRect.height()/collided->posRect.width());
98 }
99 
100 inline double SL_HeightTopLeft(PGE_Phys_Object &body, PGE_Phys_Object *collided)
101 {
102  return (fabs(collided->posRect.right()-body.posRect.left()))
103  *(collided->posRect.height()/collided->posRect.width());
104 }
105 
106 inline bool isCollideSlopeFloor(PGE_Phys_Object &body, PGE_Phys_Object *collided, int direction=-1)
107 {
108  double floorH;
109  if(direction<0)
110  floorH = SL_HeightTopLeft(body, collided);
111  else
112  floorH = SL_HeightTopRight(body, collided);
113  double slopeTop = collided->posRect.bottom()-floorH;
114  if(slopeTop<collided->top()) slopeTop=collided->posRect.top();
115  else if(slopeTop>collided->bottom()) slopeTop=collided->posRect.bottom();
116  return (body.posRect.bottom()>=slopeTop)&&
117  (body.posRect.bottom()<=collided->posRect.bottom())&&
118  (!( (body.posRect.left()+collided->colliding_xSpeed>=collided->posRect.right()+body.colliding_xSpeed) ||
119  (body.posRect.right()+collided->colliding_xSpeed<=collided->posRect.left()+body.colliding_xSpeed) ) )
120  ;
121 }
122 
123 inline bool isCollideSlopeCelling(PGE_Phys_Object &body, PGE_Phys_Object *collided, int direction=-1)
124 {
125  double cellingH;
126  if(direction>0)
127  cellingH = SL_HeightTopLeft(body, collided);
128  else
129  cellingH = SL_HeightTopRight(body, collided);
130  double slopeBottom = collided->posRect.top()+cellingH;
131  if(slopeBottom >collided->bottom()) slopeBottom =collided->posRect.bottom();
132  else if(slopeBottom <collided->top()) slopeBottom = collided->posRect.top();
133  return (body.posRect.top()<=slopeBottom )&&
134  (body.posRect.top()>=collided->posRect.top())&&
135  (!( (body.posRect.left()+collided->colliding_xSpeed>=collided->posRect.right()+body.colliding_xSpeed) ||
136  (body.posRect.right()+collided->colliding_xSpeed<=collided->posRect.left()+body.colliding_xSpeed) ) );
137 }
138 
139 #endif // COLLISION_CHECKS_H
140 
double _velocityY_add
additional Y acceleration
Definition: lvl_base_object.h:139
double _width_half
Half of width.
Definition: lvl_base_object.h:146
double _velocityX_add
additional X acceleration
Definition: lvl_base_object.h:138
PGE_RectF posRect
Real body geometry and position.
Definition: lvl_base_object.h:128
double _velocityY
current Y speed (pixels per 1/65 of second)
Definition: lvl_base_object.h:133
double _velocityX
current X speed (pixels per 1/65 of second)
Definition: lvl_base_object.h:132
The PGE_Phys_Object class.
Definition: lvl_base_object.h:51