Fix crash with fame and new player
[ryzomcore.git] / nel / samples / pacs / object.cpp
blob5fd940f4918c15d9f61654d42374b2a55e7827f5
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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/>.
17 #include "object.h"
19 // Pacs includes
20 #include <nel/pacs/u_move_container.h>
21 #include <nel/pacs/u_move_primitive.h>
23 // Misc includes
24 #include <nel/misc/vectord.h>
25 #include <nel/misc/quat.h>
26 #include <nel/misc/path.h>
28 // 3d includes
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) //
37 using namespace NL3D;
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"));
51 if(_Instance.empty())
53 nlerror("Failed to load shape: %s", CPath::lookup("rectangle.shape").c_str());
56 // Freezed
57 Freezed = reaction == UMovePrimitive::DoNothing;
59 // Setup the instance
60 if (_Instance.getNumMaterials())
62 uint i;
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);
73 else
75 if (Freezed)
77 material.setDiffuse (CRGBA(128,0,0,255));
78 material.setOpacity(255);
80 else
87 // Setup the instance
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);
110 // Setup user data
111 _MovePrimitive->UserData=(uint64)this;
113 // Setup trigger type
114 _MovePrimitive->setTriggerType (trigger);
116 // Set pos and speed
117 setPos (pos);
118 setSpeed (speed);
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());
136 // Freezed
137 Freezed = reaction == UMovePrimitive::DoNothing;
139 // Setup the instance
140 if (_Instance.getNumMaterials())
142 uint i;
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);
153 else
155 if (Freezed)
157 material.setDiffuse (CRGBA(128,0,0));
158 material.setOpacity(255);
160 else
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);
188 // Setup user data
189 _MovePrimitive->UserData=(uint64)this;
191 // Setup trigger type
192 _MovePrimitive->setTriggerType (trigger);
194 // Set pos and speed
195 setPos (pos);
196 setSpeed (speed);
197 _MovePrimitive->setGlobalPosition (pos, insertWorldImage);
200 // ***************************************************************************
202 void CObjectDyn::tryMove (double deltaTime, UMoveContainer &container, uint8 worldImage)
204 // New speed
205 if ( (_MovePrimitive->getSpeed(worldImage).norm()>0.f) || _Speed.norm()>0.f )
207 // Brake
208 CVectorD newSpeed=_Speed;
209 newSpeed.normalize();
210 newSpeed*=BRAKE_FORCE*deltaTime;
211 newSpeed+=_Speed;
213 // Stop ?
214 if (_Speed*newSpeed<=0)
215 newSpeed=CVectorD (0,0,0);
217 // Set speed
218 setSpeed (newSpeed);
220 // Try this move
221 _MovePrimitive->move (_Speed, worldImage);
222 _TryMove=true;
224 else
225 _TryMove=false;
229 // ***************************************************************************
231 void CObjectDyn::doMove (double deltaTime, uint8 worldImage)
233 // New speed
234 setSpeed (_MovePrimitive->getSpeed(worldImage));
236 // New position
237 setPos (_MovePrimitive->getFinalPosition (worldImage));
240 // ***************************************************************************
242 void CObjectDyn::setPos (const CVectorD& pos)
244 _Position=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);
255 setPos (pos);
258 // ***************************************************************************
260 void CObjectDyn::setSpeed (const CVectorD& speed)
262 _Speed=speed;
265 // ***************************************************************************
267 void CObjectDyn::remove (NLPACS::UMoveContainer &container, UScene &scene)
269 // Remove from container
270 container.removePrimitive (_MovePrimitive);
272 // Remove instance
273 if (!_Instance.empty())
274 scene.deleteInstance (_Instance);
277 // ***************************************************************************