1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <nel/pacs/u_move_container.h>
21 #include <nel/pacs/u_move_primitive.h>
24 #include <nel/misc/vectord.h>
25 #include <nel/misc/quat.h>
26 #include <nel/misc/path.h>
29 #include <nel/3d/u_scene.h>
30 #include <nel/3d/u_instance.h>
31 #include <nel/3d/u_instance_material.h>
33 #define BRAKE_FORCE (-0.1) // (m.s-2)
34 #define GRAVITY_FORCE (-15.0)
35 #define SHOCK_ABSORB (1.0) //
38 using namespace NLNET
;
39 using namespace NLMISC
;
40 using namespace NLPACS
;
42 // ***************************************************************************
44 CObjectDyn::CObjectDyn (double width
, double depth
, double height
, double orientation
, const CVectorD
& pos
,
45 const CVectorD
& speed
, bool obstacle
, UMoveContainer
&container
, UScene
&scene
,
46 UMovePrimitive::TReaction reaction
, NLPACS::UMovePrimitive::TTrigger trigger
,
47 uint8 worldImage
, uint8 nbImage
, uint8 insertWorldImage
)
49 // Create a box instance
50 _Instance
= scene
.createInstance(CPath::lookup("rectangle.shape"));
53 nlerror("Failed to load shape: %s", CPath::lookup("rectangle.shape").c_str());
57 Freezed
= reaction
== UMovePrimitive::DoNothing
;
60 if (_Instance
.getNumMaterials())
63 for (i
=0; i
<_Instance
.getNumMaterials(); i
++)
65 UInstanceMaterial material
= _Instance
.getMaterial(i
);
66 if (trigger
!= UMovePrimitive::NotATrigger
)
68 // material.setBlend(true);
69 // material.setBlendFunc(UInstanceMaterial::srcalpha, UInstanceMaterial::invsrcalpha);
70 material
.setDiffuse (CRGBA(255,255,255,255));
71 // material.setOpacity(128);
77 material
.setDiffuse (CRGBA(128,0,0,255));
78 material
.setOpacity(255);
88 _Instance
.setScale (CVectorD (width
, depth
, height
));
89 _Instance
.setRotQuat (CQuat (CVectorD (0, 0, 1), (float)orientation
));
91 // Create a collision volume
92 _MovePrimitive
= container
.addCollisionablePrimitive (worldImage
, nbImage
);
93 _MovePrimitive
->insertInWorldImage (insertWorldImage
);
95 // Setup the collision primitive
96 _MovePrimitive
->setPrimitiveType (UMovePrimitive::_2DOrientedBox
);
97 _MovePrimitive
->setHeight ((float)height
);
98 _MovePrimitive
->setOrientation (orientation
, insertWorldImage
);
99 _MovePrimitive
->setSize ((float)width
, (float)depth
);
101 // This primitive is an obstacle (it blocks others)
102 _MovePrimitive
->setObstacle (obstacle
);
104 // Setup reaction type
105 _MovePrimitive
->setReactionType (reaction
);
107 // Setup absorption value
108 _MovePrimitive
->setAbsorbtion (0.9f
);
111 _MovePrimitive
->UserData
=(uint64
)this;
113 // Setup trigger type
114 _MovePrimitive
->setTriggerType (trigger
);
120 _MovePrimitive
->setGlobalPosition (pos
, insertWorldImage
);
123 // ***************************************************************************
125 CObjectDyn::CObjectDyn (double diameter
, double height
, const CVectorD
& pos
, const CVectorD
& speed
,
126 bool obstacle
, UMoveContainer
&container
, UScene
&scene
, UMovePrimitive::TReaction reaction
,
127 NLPACS::UMovePrimitive::TTrigger trigger
, uint8 worldImage
, uint8 nbImage
, uint8 insertWorldImage
)
129 // Create a box instance
130 _Instance
= scene
.createInstance(CPath::lookup("cylinder.shape"));
131 if(_Instance
.empty())
133 nlerror("Failed to load shape: %s",CPath::lookup("cylinder.shape").c_str());
137 Freezed
= reaction
== UMovePrimitive::DoNothing
;
139 // Setup the instance
140 if (_Instance
.getNumMaterials())
143 for (i
=0; i
<_Instance
.getNumMaterials(); i
++)
145 UInstanceMaterial material
= _Instance
.getMaterial(i
);
146 if (trigger
!= UMovePrimitive::NotATrigger
)
148 // material.setBlend(true);
149 // material.setBlendFunc(UInstanceMaterial::srcalpha, UInstanceMaterial::invsrcalpha);
150 material
.setDiffuse (CRGBA(255,255,255));
151 // material.setOpacity(128);
157 material
.setDiffuse (CRGBA(128,0,0));
158 material
.setOpacity(255);
167 // Setup the instance
168 _Instance
.setScale (CVectorD (diameter
, diameter
, height
));
170 // Create a collision volume
171 _MovePrimitive
= container
.addCollisionablePrimitive (worldImage
, nbImage
);
172 _MovePrimitive
->insertInWorldImage (insertWorldImage
);
174 // Setup the primitive
175 _MovePrimitive
->setPrimitiveType (UMovePrimitive::_2DOrientedCylinder
);
176 _MovePrimitive
->setHeight ((float)height
);
177 _MovePrimitive
->setRadius ((float)diameter
/2.f
);
179 // This primitive is an obstacle (it blocks others)
180 _MovePrimitive
->setObstacle (obstacle
);
182 // Setup reaction type
183 _MovePrimitive
->setReactionType (reaction
);
185 // Setup reaction type
186 _MovePrimitive
->setAbsorbtion (0.9f
);
189 _MovePrimitive
->UserData
=(uint64
)this;
191 // Setup trigger type
192 _MovePrimitive
->setTriggerType (trigger
);
197 _MovePrimitive
->setGlobalPosition (pos
, insertWorldImage
);
200 // ***************************************************************************
202 void CObjectDyn::tryMove (double deltaTime
, UMoveContainer
&container
, uint8 worldImage
)
205 if ( (_MovePrimitive
->getSpeed(worldImage
).norm()>0.f
) || _Speed
.norm()>0.f
)
208 CVectorD newSpeed
=_Speed
;
209 newSpeed
.normalize();
210 newSpeed
*=BRAKE_FORCE
*deltaTime
;
214 if (_Speed
*newSpeed
<=0)
215 newSpeed
=CVectorD (0,0,0);
221 _MovePrimitive
->move (_Speed
, worldImage
);
229 // ***************************************************************************
231 void CObjectDyn::doMove (double deltaTime
, uint8 worldImage
)
234 setSpeed (_MovePrimitive
->getSpeed(worldImage
));
237 setPos (_MovePrimitive
->getFinalPosition (worldImage
));
240 // ***************************************************************************
242 void CObjectDyn::setPos (const CVectorD
& pos
)
245 if (!_Instance
.empty())
246 _Instance
.setPos (pos
);
249 // ***************************************************************************
251 void CObjectDyn::setGlobalPos (UGlobalPosition
& gpos
, CVectorD
& pos
, uint8 worldimage
)
253 // Place the primitive the first time
254 _MovePrimitive
->setGlobalPosition (gpos
, worldimage
);
258 // ***************************************************************************
260 void CObjectDyn::setSpeed (const CVectorD
& speed
)
265 // ***************************************************************************
267 void CObjectDyn::remove (NLPACS::UMoveContainer
&container
, UScene
&scene
)
269 // Remove from container
270 container
.removePrimitive (_MovePrimitive
);
273 if (!_Instance
.empty())
274 scene
.deleteInstance (_Instance
);
277 // ***************************************************************************