source: trunk/test/Physics/Include/EasyConstraint.h @ 1888

Last change on this file since 1888 was 1888, checked in by lolbot, 7 years ago

fixed 23 files out of 277:

  • fixed 1270 CR characters
  • fixed 56 trailing spaces
  • fixed 5085 tabs
File size: 7.6 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//            (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
6//   This program is free software; you can redistribute it and/or
7//   modify it under the terms of the Do What The Fuck You Want To
8//   Public License, Version 2, as published by Sam Hocevar. See
9//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
10//
11
12//
13// The EasyPhysic class
14// ------------------
15//
16
17#if !defined __EASYCONSTRAINT_EASYCONSTRAINT_H__
18#define __EASYCONSTRAINT_EASYCONSTRAINT_H__
19
20#ifdef HAVE_PHYS_USE_BULLET
21#include "core.h"
22#include "EasyPhysics.h"
23#endif
24
25namespace lol
26{
27
28namespace phys
29{
30
31class EasyConstraint
32{
33
34    friend class Simulation;
35    friend class EasyPhysic;
36
37#ifdef HAVE_PHYS_USE_BULLET
38
39public:
40    EasyConstraint() :
41        m_typed_constraint(NULL),
42        m_p2p_constraint(NULL),
43        m_hinge_constraint(NULL),
44        m_slider_constraint(NULL),
45        m_cone_twist_constraint(NULL),
46        m_6dof_constraint(NULL),
47        m_owner_simulation(NULL),
48        m_a_physobj(NULL),
49        m_b_physobj(NULL),
50        m_a_transform(lol::mat4(1.f)),
51        m_b_transform(lol::mat4(1.f)),
52        m_using_ref_a(false),
53        m_disable_a2b_collision(false)
54
55    {
56    }
57    ~EasyConstraint()
58    {
59        delete m_typed_constraint;
60        m_p2p_constraint = NULL;
61        m_hinge_constraint = NULL;
62        m_slider_constraint = NULL;
63        m_cone_twist_constraint = NULL;
64        m_6dof_constraint = NULL;
65    }
66
67    void AddToSimulation(class Simulation* current_simulation);
68    void RemoveFromSimulation(class Simulation* current_simulation);
69
70private:
71
72    //check if Init can be done
73    bool CanProceedWithInit()
74    {
75        if (!m_a_physobj || !m_b_physobj)
76            return false;
77
78        if (!m_a_physobj->m_rigid_body || !m_b_physobj->m_rigid_body)
79            return false;
80
81        return true;
82    }
83
84    //-------------------------------------------------------------------------
85    //Init constraint functions
86    //--
87    void CustomInitConstraintToPoint2Point()
88    {
89        m_p2p_constraint = new btPoint2PointConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
90                                                        LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT));
91        m_typed_constraint = m_p2p_constraint;
92    }
93
94    void CustomInitConstraintToHinge()
95    {
96        m_hinge_constraint = new btHingeConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
97                                                        btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
98                                                        btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
99                                                        m_using_ref_a);
100        m_typed_constraint = m_hinge_constraint;
101    }
102
103    void CustomInitConstraintToSlider()
104    {
105        m_slider_constraint = new btSliderConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
106                                                        btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
107                                                        btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
108                                                        m_using_ref_a);
109        m_typed_constraint = m_slider_constraint;
110    }
111
112    void CustomInitConstraintToConeTwist()
113    {
114        m_cone_twist_constraint = new btConeTwistConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
115                                                        btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
116                                                        btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)));
117        m_typed_constraint = m_cone_twist_constraint;
118    }
119
120    void CustomInitConstraintTo6Dof()
121    {
122        m_6dof_constraint = new btGeneric6DofConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
123                                                        btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
124                                                        btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
125                                                        m_using_ref_a);
126        m_typed_constraint = m_6dof_constraint;
127    }
128
129    btTypedConstraint*            m_typed_constraint;
130    btPoint2PointConstraint*    m_p2p_constraint;
131    btHingeConstraint*            m_hinge_constraint;
132    btSliderConstraint*            m_slider_constraint;
133    btConeTwistConstraint*        m_cone_twist_constraint;
134    btGeneric6DofConstraint*    m_6dof_constraint;
135
136#else  // NO PHYSIC IMPLEMENTATION
137
138public:
139    EasyConstraint() :
140        m_a_physobj(NULL),
141        m_b_physobj(NULL),
142        m_a_transform(lol::mat4(1.f)),
143        m_b_transform(lol::mat4(1.f)),
144        m_using_ref_a(false),
145        m_disable_a2b_collision(false)
146    {
147    }
148
149private:
150
151    void AddToSimulation(class Simulation* current_simulation) { }
152    void RemoveFromSimulation(class Simulation* current_simulation) { }
153
154    //check if Init can be done
155    bool CanProceedWithInit() { return false; }
156    void CustomInitConstraintToPoint2Point() { }
157    void CustomInitConstraintToHinge() { }
158    void CustomInitConstraintToSlider() { }
159    void CustomInitConstraintToConeTwist() { }
160    void CustomInitConstraintTo6Dof() { }
161
162#endif // PHYSIC IMPLEMENTATION
163
164public:
165    void InitConstraintToPoint2Point()    { if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); }
166    void InitConstraintToHinge()        { if (CanProceedWithInit()) CustomInitConstraintToHinge(); }
167    void InitConstraintToSlider()        { if (CanProceedWithInit()) CustomInitConstraintToSlider(); }
168    void InitConstraintToConeTwist()    { if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); }
169    void InitConstraintTo6Dof()            { if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); }
170
171    //Set given physic object to the proper slot.
172    void SetPhysObjA(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(false, NewPhysObj, NewTransform); }
173    void SetPhysObjB(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(true, NewPhysObj, NewTransform); }
174    void SetPhysObj(bool SetToB, EasyPhysic* NewPhysObj, lol::mat4 NewTransform)
175    {
176        if (SetToB)
177        {
178            m_b_physobj = NewPhysObj;
179            m_b_transform = NewTransform;
180        }
181        else
182        {
183            m_a_physobj = NewPhysObj;
184            m_a_transform = NewTransform;
185        }
186    }
187
188    //Set whether or not the physic engine should use the A object as the reference (most constraint transform are local).
189    void SetRefAsA(bool NewUseRefA)
190    {
191        m_using_ref_a = NewUseRefA;
192    }
193
194    //Set whether or not to disable the collision between the bodies
195    void DisableCollisionBetweenObjs(bool DisableCollision)
196    {
197        m_disable_a2b_collision = DisableCollision;
198    }
199
200private:
201    Simulation*                    m_owner_simulation;
202    EasyPhysic*                    m_a_physobj;
203    EasyPhysic*                    m_b_physobj;
204    lol::mat4                    m_a_transform;
205    lol::mat4                    m_b_transform;
206    bool                        m_using_ref_a;
207    bool                        m_disable_a2b_collision;
208
209};
210
211} /* namespace phys */
212
213} /* namespace lol */
214
215#endif /* __EASYCONSTRAINT_EASYCONSTRAINT_H__ */
216
Note: See TracBrowser for help on using the repository browser.