Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "Headers/PhysicsComponent.h"

#include "Core/Headers/Console.h"
#include "Core/Math/Headers/Transform.h"
#include "Graphs/Headers/SceneGraphNode.h"
#include "Geometry/Shapes/Headers/Object3D.h"
#include "Physics/Headers/PXDevice.h"
#include "Physics/Headers/PhysicsAsset.h"
#include "Physics/Headers/PhysicsAPIWrapper.h"

namespace Divide {

PhysicsComponent::PhysicsComponent(SceneGraphNode& parentSGN, PhysicsGroup physicsGroup, PXDevice& context)
    : SGNComponent(SGNComponent::ComponentType::PHYSICS, parentSGN),
      TransformInterface(),
      _physicsCollisionGroup(physicsGroup),
      _dirty(true),
      _dirtyInterp(true),
      _parentDirty(true),
      _isUniformScaled(true),
      _prevInterpValue(0.0)
{
    if (physicsDriven()) {
        _transformInterface.reset(context.createRigidActor(parentSGN));
    } else {
        _transformInterface = std::make_unique<Transform>();
    }

    reset();
}

PhysicsComponent::~PhysicsComponent()
{
}

void PhysicsComponent::update(const U64 deltaTime) {
    if (!physicsDriven()) {
        _prevTransformValues = getTransform()->getValues();
    }

    _parentDirty = isParentTransformDirty();

    if (_transformUpdatedMask.hasSetFlags()) {
        if (_transformUpdatedMask.getFlag(TransformType::SCALE)) {
            _isUniformScaled = getScale().isUniform();
        }

        for (DELEGATE_CBK<void>& cbk : _transformCallbacks) {
            cbk();
        }
        
        _transformUpdatedMask.clearAllFlags();
    }
}

void PhysicsComponent::reset() {
    _worldMatrix.identity();
    _prevTransformValues._translation.set(0.0f);
    _prevTransformValues._scale.set(1.0f);
    _prevTransformValues._orientation.identity();
    while (!_transformStack.empty()) {
        _transformStack.pop();
    }
    _transformUpdatedMask.setAllFlags();
    _dirty = true;
    _dirtyInterp = true;
    _prevInterpValue = 0.0;
}

void PhysicsComponent::ignoreView(const bool state, const I64 cameraGUID) {
    _ignoreViewSettings._cameraGUID = cameraGUID;
    setTransformDirty(TransformType::VIEW_OFFSET);
}

void PhysicsComponent::setViewOffset(const vec3<F32>& posOffset, const mat4<F32>& rotationOffset) {
    _ignoreViewSettings._posOffset.set(posOffset);
    _ignoreViewSettings._transformOffset.set(rotationOffset);

    _dirty = _dirtyInterp = true;

    setTransformDirty(TransformType::VIEW_OFFSET);
}

void PhysicsComponent::cookCollisionMesh(const stringImpl& sceneName) {
   
}

void PhysicsComponent::setTransformDirty(TransformType type) {
    _transformUpdatedMask.setFlag(type);
    _dirty = true;
    _dirtyInterp = true;
}

void PhysicsComponent::setPosition(const vec3<F32>& position) {
    _transformInterface->setPosition(position);
    setTransformDirty(TransformType::TRANSLATION);
}

void PhysicsComponent::setScale(const vec3<F32>& scale) {
    _transformInterface->setScale(scale);
    setTransformDirty(TransformType::SCALE);
}

void PhysicsComponent::setRotation(const Quaternion<F32>& quat) {
    _transformInterface->setRotation(quat);
    setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::setRotation(const vec3<F32>& axis, F32 degrees,  bool inDegrees) {
    _transformInterface->setRotation(axis, degrees, inDegrees);
    setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::setRotation(F32 pitch, F32 yaw, F32 roll, bool inDegrees) {
    _transformInterface->setRotation(pitch, yaw, roll, inDegrees);
    setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::translate(const vec3<F32>& axisFactors) {
    _transformInterface->translate(axisFactors);
    setTransformDirty(TransformType::TRANSLATION);
}

void PhysicsComponent::scale(const vec3<F32>& axisFactors) {
    _transformInterface->scale(axisFactors);
    setTransformDirty(TransformType::SCALE);
}

void PhysicsComponent::rotate(const vec3<F32>& axis, F32 degrees, bool inDegrees) {
    _transformInterface->rotate(axis, degrees, inDegrees);
    setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::rotate(F32 pitch, F32 yaw, F32 roll, bool inDegrees) {
    _transformInterface->rotate(pitch, yaw, roll, inDegrees);
    setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::rotate(const Quaternion<F32>& quat) {
    _transformInterface->rotate(quat);
    setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::rotateSlerp(const Quaternion<F32>& quat, const D64 deltaTime) {
   _transformInterface->rotateSlerp(quat, deltaTime);
   setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::setScaleX(const F32 ammount) {
    _transformInterface->setScaleX(ammount);
    setTransformDirty(TransformType::SCALE);
}

void PhysicsComponent::setScaleY(const F32 ammount) {
    _transformInterface->setScaleY(ammount);
    setTransformDirty(TransformType::SCALE);
}

void PhysicsComponent::setScaleZ(const F32 ammount) {
    _transformInterface->setScaleZ(ammount);
    setTransformDirty(TransformType::SCALE);
}

void PhysicsComponent::scaleX(const F32 scale) {
    _transformInterface->scaleX(scale);
    setTransformDirty(TransformType::SCALE);
}

void PhysicsComponent::scaleY(const F32 scale) {
    _transformInterface->scaleY(scale);
    setTransformDirty(TransformType::SCALE);
}

void PhysicsComponent::scaleZ(const F32 scale) {
    _transformInterface->scaleZ(scale);
    setTransformDirty(TransformType::SCALE);
}

void PhysicsComponent::rotateX(const F32 angle, bool inDegrees) {
    _transformInterface->rotateX(angle, inDegrees);
    setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::rotateY(const F32 angle, bool inDegrees) {
    _transformInterface->rotateY(angle, inDegrees);
    setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::rotateZ(const F32 angle, bool inDegrees) {
    _transformInterface->rotateZ(angle, inDegrees);
    setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::setRotationX(const F32 angle, bool inDegrees) {
    _transformInterface->setRotationX(angle, inDegrees);
    setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::setRotationY(const F32 angle, bool inDegrees) {
    _transformInterface->setRotationY(angle, inDegrees);
    setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::setRotationZ(const F32 angle, bool inDegrees) {
    _transformInterface->setRotationZ(angle, inDegrees);
    setTransformDirty(TransformType::ROTATION);
}

void PhysicsComponent::setPositionX(const F32 positionX) {
    _transformInterface->setPositionX(positionX);
    setTransformDirty(TransformType::TRANSLATION);
}

void PhysicsComponent::setPositionY(const F32 positionY) {
    _transformInterface->setPositionY(positionY);
    setTransformDirty(TransformType::TRANSLATION);
}

void PhysicsComponent::setPositionZ(const F32 positionZ) {
    _transformInterface->setPositionZ(positionZ);
    setTransformDirty(TransformType::TRANSLATION);
}

void PhysicsComponent::pushTransforms() {
    if (!physicsDriven()) {
        _transformStack.push(getTransform()->getValues());
    }
}

bool PhysicsComponent::popTransforms() {
    if (!physicsDriven() && !_transformStack.empty()) {
        _prevTransformValues = _transformStack.top();
        getTransform()->setValues(_prevTransformValues);
        _transformStack.pop();

        setTransformDirty(TransformType::TRANSLATION);
        setTransformDirty(TransformType::SCALE);
        setTransformDirty(TransformType::ROTATION);
        return true;
    }
    return false;
}

bool PhysicsComponent::isParentTransformDirty() const {
    SceneGraphNode_ptr parent = _parentSGN.getParent().lock();
    while (parent != nullptr) {
        PhysicsComponent* parentComp = parent->get<PhysicsComponent>();
        if(parentComp->_dirtyInterp || parentComp->_dirty)
        {
            return true;
        }

        parent = parent->getParent().lock();
    }

    return false;
}

void PhysicsComponent::clean(bool interp) {
    if (interp) {
        _dirtyInterp = false;
    } else {
        _dirty = false;
    }
}

const mat4<F32>& PhysicsComponent::getMatrix() {
    return _transformInterface->getMatrix();
}

const mat4<F32>& PhysicsComponent::getWorldMatrix(D64 interpolationFactor) {
    bool dirty = (_dirtyInterp ||
                  !COMPARE(_prevInterpValue, interpolationFactor) ||
                  _parentDirty);

    if (dirty) {
        SceneGraphNode_wptr grandParentPtr = _parentSGN.getParent();
        _worldMatrixInterp.set(getLocalPosition(interpolationFactor),
                               getLocalScale(interpolationFactor),
                               GetMatrix(getLocalOrientation(interpolationFactor)));
        if (!grandParentPtr.expired()) {
            _worldMatrixInterp *= grandParentPtr.lock()->get<PhysicsComponent>()->getWorldMatrix(interpolationFactor);
        }
        clean(true);
    }

    if (_ignoreViewSettings._cameraGUID != -1) {
        _worldMatrixInterp = _worldMatrixInterp * _ignoreViewSettings._transformOffset;
    }

    return _worldMatrixInterp;
}

const mat4<F32>& PhysicsComponent::getWorldMatrix() {
    if (_dirty || _parentDirty){
        _worldMatrix.set(getMatrix());

        SceneGraphNode_wptr grandParentPtr = _parentSGN.getParent();
        if (!grandParentPtr.expired()) {
            _worldMatrix *= grandParentPtr.lock()->get<PhysicsComponent>()->getWorldMatrix();
        }
        clean(false);
    }

    if (_ignoreViewSettings._cameraGUID != -1) {
        _worldMatrix = _worldMatrix * _ignoreViewSettings._transformOffset;
    }

    return _worldMatrix;
}

/// Return the position
vec3<F32> PhysicsComponent::getPosition() const {
    vec3<F32> position(getLocalPosition());

    SceneGraphNode_ptr grandParent = _parentSGN.getParent().lock();
    if (grandParent) {
        position += grandParent->get<PhysicsComponent>()->getPosition();
    }

    return position;
}

vec3<F32> PhysicsComponent::getLocalPosition() const {
    vec3<F32> pos;
    getPosition(pos);
    return pos;
}

/// Return the position
vec3<F32> PhysicsComponent::getPosition(D64 interpolationFactor) const {
    vec3<F32> position(getLocalPosition(interpolationFactor));

    SceneGraphNode_ptr grandParent = _parentSGN.getParent().lock();
    if (grandParent) {
        position += grandParent->get<PhysicsComponent>()->getPosition(interpolationFactor);
    }
    
    return position;
}

vec3<F32> PhysicsComponent::getLocalPosition(D64 interpolationFactor) const {
    if (physicsDriven()) {
        return getLocalPosition();
    }

    return Lerp(_prevTransformValues._translation,
                getLocalPosition(),
                to_float(interpolationFactor));
}

vec3<F32> PhysicsComponent::getScale() const {
    vec3<F32> scale(getLocalScale());

    SceneGraphNode_ptr grandParent = _parentSGN.getParent().lock();
    if (grandParent) {
        scale *= grandParent->get<PhysicsComponent>()->getScale();
    }

    return scale;
}

vec3<F32> PhysicsComponent::getLocalScale() const {
    vec3<F32> scale;
    getScale(scale);
    return scale;
}

/// Return the scale factor
vec3<F32> PhysicsComponent::getScale(D64 interpolationFactor) const {
    vec3<F32> scale(getLocalScale(interpolationFactor));

    SceneGraphNode_ptr grandParent = _parentSGN.getParent().lock();
    if (grandParent) {
        scale *= grandParent->get<PhysicsComponent>()->getScale(interpolationFactor);
    }

    return scale;
}

vec3<F32> PhysicsComponent::getLocalScale(D64 interpolationFactor) const {
    if (physicsDriven()) {
        return getLocalScale();
    }

    return Lerp(_prevTransformValues._scale,
                getLocalScale(),
                to_float(interpolationFactor));
}

/// Return the orientation quaternion
Quaternion<F32> PhysicsComponent::getOrientation() const {
    Quaternion<F32> orientation(getLocalOrientation());

    SceneGraphNode_ptr grandParent = _parentSGN.getParent().lock();
    if (grandParent) {
        orientation.set(grandParent->get<PhysicsComponent>()->getOrientation() * orientation);
    }

    return orientation;

}

Quaternion<F32> PhysicsComponent::getLocalOrientation() const {
    Quaternion<F32> quat;
    getOrientation(quat);
    return quat;
}

Quaternion<F32> PhysicsComponent::getOrientation(D64 interpolationFactor) const {
    Quaternion<F32> orientation(getLocalOrientation(interpolationFactor));

    SceneGraphNode_ptr grandParent = _parentSGN.getParent().lock();
    if (grandParent) {
        orientation.set(grandParent->get<PhysicsComponent>()->getOrientation(interpolationFactor) * orientation);
    }

    return orientation;
}

Quaternion<F32> PhysicsComponent::getLocalOrientation(D64 interpolationFactor) const {
    if (physicsDriven()) {
        return getLocalOrientation();
    }

    return Slerp(_prevTransformValues._orientation,
                 getLocalOrientation(),
                 to_float(interpolationFactor));
}

bool PhysicsComponent::isUniformScaled() const {
    return getLocalScale().isUniform();
}

Transform* PhysicsComponent::getTransform() const {
    assert(!physicsDriven());
    return static_cast<Transform*>(_transformInterface.get());
}

PhysicsAsset* PhysicsComponent::getPhysicsAsset() const {
    assert(physicsDriven());
    return static_cast<PhysicsAsset*>(_transformInterface.get());
}

bool PhysicsComponent::physicsDriven() const {
    return (_physicsCollisionGroup != PhysicsGroup::GROUP_IGNORE &&
            _physicsCollisionGroup != PhysicsGroup::GROUP_COUNT);
}

// Transform interface access
void PhysicsComponent::getScale(vec3<F32>& scaleOut) const {
    _transformInterface->getScale(scaleOut);
}

void PhysicsComponent::getPosition(vec3<F32>& posOut) const {
    _transformInterface->getPosition(posOut);
}

void PhysicsComponent::getOrientation(Quaternion<F32>& quatOut) const {
    _transformInterface->getOrientation(quatOut);
}

};

Commits for Divide-Framework/trunk/Source Code/Graphs/Components/PhysicsComponent.cpp

Diff revisions: vs.
Revision Author Commited Message
842 Diff Diff IonutCava picture IonutCava Wed 01 Feb, 2017 17:25:15 +0000

[IonutCava]
- Start to implement scripting support via ChaiScript: http://chaiscript.com/
- Cleanup DELEGATE_CBK alias

821 Diff Diff IonutCava picture IonutCava Tue 17 Jan, 2017 17:15:45 +0000

[IonutCava]
- Profile guided optimizations

804 Diff Diff IonutCava picture IonutCava Thu 01 Dec, 2016 17:20:59 +0000

[IonutCava]
- Singleton elimination update Part I: get it to compile
— The following classes are no longer Singletons: GFXDevice, GL_API, DX_API, SFXWrapper, FmodWrapper, SDLWrapper, ALWrapper, PXDevice, InputInterface, RenderPassManager, SceneManager and ResourceManager;
— Refactor system to a Context / Component based implementation (Pass relevant context to objects: e.g. GFXDevice object to Textures, GUI to GUIElements, etc)
— Make devices and managers components of the kernel
— Allow multiple Resource caches to co-exist. This may prove useful for later when a more fragmented memory model is need (per frame / per scene / global caches / etc)

  • next steps: part II – cleanup/refactor new code, part III – optimise code, part IV – remove remaining Singletons (e.g. Application, ParamHandler, FrameListenerManager, Recast, PostFX and DebugInterface)
801 Diff Diff IonutCava picture IonutCava Sun 27 Nov, 2016 21:28:01 +0000

[IonutCava]
- More performance analysis guided optimizations.
- Some refactoring to allow later removal of Singletons status for: GFXDevice, SFXDevice, PXDevice, GUI and Input.
— Pass a PlatformContext around objects to access these classes.

783 Diff Diff IonutCava picture IonutCava Fri 14 Oct, 2016 15:50:34 +0000

[IonutCava]
- More cleanup of the Frustum class
- Change random number generation system to use the C++11 <random> features. (default engine set to Mersenne Twister and using a uniform distribution as default)

730 Diff Diff IonutCava picture IonutCava Thu 16 Jun, 2016 15:35:07 +0000

[IonutCava]
- More shadow mapping fixes
- Small performance improvements
- Proper per scene XML data loading (e.g. fog)

713 Diff Diff IonutCava picture IonutCava Wed 25 May, 2016 15:43:38 +0000

[IonutCava]
- Removed all unique_ptr’s from the code with classic new/delete pairs. They were seriously not needed.
- Added the concept of SceneComponent to scene specific classes: aiManager, lightPool, sceneGraph, physicsInterface etc
— This allowed the removal of the global GET_ACTIVE_SCENEGRAPH call;

709 Diff Diff IonutCava picture IonutCava Thu 19 May, 2016 16:21:46 +0000

[IonutCava]
- Massive rewrite of the resource system:
— ResourceCache now uses a map of weak pointers and passes shared pointers to the call point of CreateResource
— RemoveResource is no longer needed, but resource based smart pointers use a custom deleter that calls unload()
— Keep a shared_ptr of the resource in the entity that needs it and pass shared_ptr references from getter methods
-— All of this removed the need for calls to RemoveResource, REGISTER_TRACKED_DEPENDENCY and UNREGISTER_TRACKED_DEPENDENCY reducing the number of mutex locks and atomic exchanges
- Singleton cleanup:
— Removed ShaderManager singleton and merged it’s data and responsibilities in static methods in both ShaderProgram and Shader classes.
Explanation: Due to the complex interdependency chains in the system, copying, updating, tracking the cache in a thread safe way became a lot more slower than a straight forward smart pointer based implementation. (e.g. scene graph nodes have 3d objects and material instances, 3d objects have materials, materials have textures and shaders, etc)

696 Diff Diff IonutCava picture IonutCava Mon 09 May, 2016 16:14:09 +0000

[IonutCava]
- More physics system preparation:
— Define specific px groups for nodes: static, kinematic, dynamic, ragdoll, etc

695 IonutCava picture IonutCava Sun 08 May, 2016 22:45:32 +0000

[IonutCava]
- Better separation of rigid body based physics and standard transform system