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
|
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
#ifndef CRAB_H
#define CRAB_H
#include "foundation/Px.h"
#include "foundation/PxSimpleTypes.h"
#include "common/PxPhysXCommonConfig.h"
#include "task/PxTask.h"
#include <vector>
#define UPDATE_FREQUENCY_RESET 10
struct CrabState
{
enum Enum
{
eWAITING,
eMOVE_FWD,
eMOVE_BKWD,
eROTATE_LEFT,
eROTATE_RIGHT,
ePANIC,
eNUM_STATES,
};
};
namespace physx
{
class PxRigidDynamic;
class PxRevoluteJoint;
class PxJoint;
class PxScene;
}
// Edge Labels for Theo Jansen's Mechanism
// _.
// ,' | m
// ,' |2b .....
// ,' 2a | e |n
// +-------+---------+
// | |
// | |2c
// | |
// +-------+
// `. |
// `. |2d
// `. |
// `|
struct LegParameters
{
physx::PxReal a; // half x-dim leg
physx::PxReal b; // half height upper triangle
physx::PxReal c; // half height distance joints (square)
physx::PxReal d; // half height lower triangle
physx::PxReal e; // x distance from main body centre
physx::PxReal m; // half extent of the motor box
physx::PxReal n; // y offset of the motor from the main body centre
};
class Crab;
class CrabManager;
struct SqRayBuffer
{
SqRayBuffer(Crab* crab, physx::PxU32 numRays, physx::PxU32 numHits);
~SqRayBuffer();
Crab* mCrab;
physx::PxBatchQuery* mBatchQuery;
physx::PxRaycastQueryResult* mRayCastResults;
physx::PxRaycastHit* mRayCastHits;
physx::PxU32 mQueryResultSize;
physx::PxU32 mHitSize;
void* mOrigAddresses[2];
void setScene(physx::PxScene* scene);
private:
SqRayBuffer& operator=(const SqRayBuffer&);
};
struct SqSweepBuffer
{
SqSweepBuffer(Crab* crab, physx::PxU32 numRays, physx::PxU32 numHits);
~SqSweepBuffer();
Crab* mCrab;
physx::PxBatchQuery* mBatchQuery;
physx::PxSweepQueryResult* mSweepResults;
physx::PxSweepHit* mSweepHits;
physx::PxU32 mQueryResultSize;
physx::PxU32 mHitSize;
void* mOrigAddresses[2];
void setScene(physx::PxScene* scene);
private:
SqSweepBuffer& operator=(const SqSweepBuffer&);
};
class Crab
{
public:
Crab(CrabManager* crabManager, physx::PxU32 updateFrequency);
~Crab();
void update(physx::PxReal dt);
void setAcceleration(physx::PxReal leftAcc, physx::PxReal rightAcc);
void flushAccelerationBuffer();
PX_INLINE const physx::PxRigidDynamic* getCrabBody() const { return mCrabBody; }
PX_INLINE physx::PxRigidDynamic* getCrabBody() { return mCrabBody; }
void run();
void setScene(physx::PxScene* scene);
void initMembers();
Crab* create(const physx::PxVec3& _crabPos, const physx::PxReal crabDepth, const physx::PxReal scale, const physx::PxReal legMass, const physx::PxU32 numLegs);
physx::PxVec3 getPlaceOnFloor(physx::PxVec3 pos);
void createLeg(physx::PxRigidDynamic* mainBody, physx::PxVec3 localPos, physx::PxReal mass, const LegParameters& params, physx::PxReal scale, physx::PxRigidDynamic* motor, physx::PxVec3 motorAttachmentPos);
void scanForObstacles();
void updateState();
void initState(CrabState::Enum state);
void setManager(CrabManager* crabManager) { mManager = crabManager; }
CrabManager* getManager(){ return mManager; }
private:
CrabManager* mManager;
physx::PxRigidDynamic* mCrabBody;
//physx::PxRevoluteJoint* mMotorJoint[2];
physx::PxD6Joint* mMotorJoint[2];
physx::PxMaterial* mMaterial;
physx::PxReal mAcceleration[2];
//SqRayBuffer* mSqRayBuffer;
SqSweepBuffer* mSqSweepBuffer;
physx::PxReal mLegHeight;
CrabState::Enum mCrabState;
physx::PxReal mStateTime;
physx::PxReal mDistances[10];
physx::PxReal mAccumTime;
physx::PxReal mElapsedTime;
physx::PxReal mAccelerationBuffer[2];
physx::PxU32 mUpdateFrequency;
};
#endif
|