RavEngine
Loading...
Searching...
No Matches
ScInteraction.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef SC_INTERACTION_H
30#define SC_INTERACTION_H
31
32#include "foundation/Px.h"
33#include "ScInteractionFlags.h"
34#include "ScScene.h"
35#include "ScActorSim.h"
36#include "foundation/PxUserAllocated.h"
37#include "foundation/PxUtilities.h"
38
39namespace physx
40{
41#define PX_INVALID_INTERACTION_ACTOR_ID 0xffffffff
42#define PX_INVALID_INTERACTION_SCENE_ID 0xffffffff
43
44namespace Sc
45{
46 // Interactions are used for connecting actors into activation groups. An interaction always connects exactly two actors.
47 // An interaction is implicitly active if at least one of the two actors it connects is active.
48
50 {
51 PX_NOCOPY(Interaction)
52
53 protected:
54 Interaction(ActorSim& actor0, ActorSim& actor1, InteractionType::Enum interactionType, PxU8 flags);
55 ~Interaction() { PX_ASSERT(!readInteractionFlag(InteractionFlag::eIN_DIRTY_LIST)); }
56 public:
57 // Interactions automatically register themselves in the actors here
58 PX_FORCE_INLINE bool registerInActors(void* data = NULL);
59
60 // Interactions automatically unregister themselves from the actors here
61 PX_FORCE_INLINE void unregisterFromActors();
62
63 PX_FORCE_INLINE ActorSim& getActorSim0() const { return mActor0; }
64 PX_FORCE_INLINE ActorSim& getActorSim1() const { return mActor1; }
65
66 PX_FORCE_INLINE Scene& getScene() const { return mActor0.getScene(); }
67
68 PX_FORCE_INLINE InteractionType::Enum getType() const { return InteractionType::Enum(mInteractionType); }
69
70 PX_FORCE_INLINE PxU8 readInteractionFlag(PxU8 flag) const { return PxU8(mInteractionFlags & flag); }
71 PX_FORCE_INLINE void raiseInteractionFlag(InteractionFlag::Enum flag) { mInteractionFlags |= flag; }
72 PX_FORCE_INLINE void clearInteractionFlag(InteractionFlag::Enum flag) { mInteractionFlags &= ~flag; }
73
79 PX_FORCE_INLINE void setDirty(PxU32 dirtyFlags);
80
86 /*PX_FORCE_INLINE*/ void setClean(bool removeFromList);
87
88 PX_FORCE_INLINE PxIntBool needsRefiltering() const { return (getDirtyFlags() & InteractionDirtyFlag::eFILTER_STATE); }
89
90 PX_FORCE_INLINE PxIntBool isElementInteraction() const;
91
92 // Called when an interaction is activated or created.
93 // Return true if activation should proceed else return false (for example: joint interaction between two kinematics should not get activated)
94// virtual bool onActivate_(void* data) = 0;
95
96 // Called when an interaction is deactivated.
97 // Return true if deactivation should proceed else return false (for example: joint interaction between two kinematics can ignore deactivation because it always is deactivated)
98// virtual bool onDeactivate_() = 0;
99
100 PX_FORCE_INLINE void setInteractionId(PxU32 id) { mSceneId = id; }
101 PX_FORCE_INLINE PxU32 getInteractionId() const { return mSceneId; }
102 PX_FORCE_INLINE bool isRegistered() const { return mSceneId != PX_INVALID_INTERACTION_SCENE_ID; }
103
104 PX_FORCE_INLINE void setActorId(ActorSim* actor, PxU32 id);
105 PX_FORCE_INLINE PxU32 getActorId(const ActorSim* actor) const;
106
107 PX_FORCE_INLINE PxU8 getDirtyFlags() const { return mDirtyFlags; }
108
109 private:
110 void addToDirtyList();
111 void removeFromDirtyList();
112
113 ActorSim& mActor0;
114 ActorSim& mActor1;
115
116 // PT: TODO: merge the 6bits of the 3 PxU8s in the top bits of the 3 PxU32s
117 PxU32 mSceneId; // PT: TODO: merge this with mInteractionType
118
119 // PT: TODO: are those IDs even worth caching? Since the number of interactions per actor is (or should be) small,
120 // we could just do a linear search and save memory here...
121 PxU32 mActorId0; // PT: id of this interaction within mActor0's mInteractions array
122 PxU32 mActorId1; // PT: id of this interaction within mActor1's mInteractions array
123 protected:
124 const PxU8 mInteractionType; // PT: stored on a byte to save space, should be InteractionType enum, 5/6 bits needed here
125 PxU8 mInteractionFlags; // PT: 6 bits needed here, see InteractionFlag enum
126 PxU8 mDirtyFlags; // PT: 6 bits needed here, see InteractionDirtyFlag enum
127 PxU8 mPadding8;
128 };
129
130} // namespace Sc
131
133
134PX_FORCE_INLINE bool Sc::Interaction::registerInActors(void* data)
135{
136 bool active = activateInteraction(this, data);
137
138 mActor0.registerInteractionInActor(this);
139 mActor1.registerInteractionInActor(this);
140
141 return active;
142}
143
144PX_FORCE_INLINE void Sc::Interaction::unregisterFromActors()
145{
146 mActor0.unregisterInteractionFromActor(this);
147 mActor1.unregisterInteractionFromActor(this);
148}
149
150PX_FORCE_INLINE void Sc::Interaction::setActorId(ActorSim* actor, PxU32 id)
151{
152 PX_ASSERT(id != PX_INVALID_INTERACTION_ACTOR_ID);
153 PX_ASSERT(&mActor0 == actor || &mActor1 == actor);
154 if(&mActor0 == actor)
155 mActorId0 = id;
156 else
157 mActorId1 = id;
158}
159
160PX_FORCE_INLINE PxU32 Sc::Interaction::getActorId(const ActorSim* actor) const
161{
162 PX_ASSERT(&mActor0 == actor || &mActor1 == actor);
163 return &mActor0 == actor ? mActorId0 : mActorId1;
164}
165
166PX_FORCE_INLINE PxIntBool Sc::Interaction::isElementInteraction() const
167{
168 const PxIntBool res = readInteractionFlag(InteractionFlag::eRB_ELEMENT);
169 PX_ASSERT( (res &&
170 ((getType() == InteractionType::eOVERLAP) ||
171 (getType() == InteractionType::eTRIGGER) ||
172 (getType() == InteractionType::eMARKER))) ||
173 (!res &&
174 ((getType() == InteractionType::eCONSTRAINTSHADER) ||
175 (getType() == InteractionType::eARTICULATION))));
176
177 return res;
178}
179
181{
182 PX_ASSERT(getType() != InteractionType::eARTICULATION);
183
184 mDirtyFlags |= PxTo8(dirtyFlags);
185 if(!readInteractionFlag(InteractionFlag::eIN_DIRTY_LIST))
186 {
187 addToDirtyList();
188 raiseInteractionFlag(InteractionFlag::eIN_DIRTY_LIST);
189 }
190}
191
192//PX_FORCE_INLINE void Sc::Interaction::setClean(bool removeFromList)
193//{
194// if (readInteractionFlag(InteractionFlag::eIN_DIRTY_LIST))
195// {
196// if (removeFromList) // if we process all dirty interactions anyway, then we can just clear the list at the end and save the work here.
197// removeFromDirtyList();
198// clearInteractionFlag(InteractionFlag::eIN_DIRTY_LIST);
199// }
200//
201// mDirtyFlags = 0;
202//}
203
204
205}
206
207#endif
Definition PxUserAllocated.h:43
Definition ScActorSim.h:83
Definition ScInteraction.h:50
PX_FORCE_INLINE void setDirty(PxU32 dirtyFlags)
Mark the interaction as dirty. This will put the interaction into a list that is processed once per s...
Definition ScInteraction.h:180
void setClean(bool removeFromList)
Clear all flags that mark the interaction as dirty and optionally remove the interaction from the lis...
Definition ScInteraction.cpp:60
Definition ScScene.h:240
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39