-
Notifications
You must be signed in to change notification settings - Fork 1
/
PhysObject.h
169 lines (126 loc) · 4.06 KB
/
PhysObject.h
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
#pragma once
#ifndef PHYSOBJECT_H_
#define PHYSOBJECT_H_
#include "StdAfx.h"
#include "Util.h"
#include "ObjectReplicator.h"
namespace Sarona
{
class PhysWorld;
class PhysObjectMotionState;
class JSObject;
class PhysObject
: public ZCom_NodeEventInterceptor // Receive regular events
{
friend class PhysWorld;
friend class JSObject;
private:
// ZCom stuff
scoped_ptr<ZCom_Node> m_zcomNode;
// Replicator handle. Weak ref; it is managed by zoidcom and ought to outlive this object
ObjectReplicator* m_replicator;
// Global obj refs
btDynamicsWorld* m_dynamicsWorld;
PhysWorld* m_world;
// Bullet fields
btCollisionShape* m_shape; // ATN: No ownership!
scoped_ptr<PhysObjectMotionState> m_motionstate;
scoped_ptr<btRigidBody> m_rigidbody;
bool recUserEvent(ZCom_Node *_node, ZCom_ConnID _from,
eZCom_NodeRole _remoterole, ZCom_BitStream &_data,
zU32 _estimated_time_sent);
bool recInit(ZCom_Node *_node, ZCom_ConnID _from,
eZCom_NodeRole _remoterole);
bool recSyncRequest(ZCom_Node *_node, ZCom_ConnID _from,
eZCom_NodeRole _remoterole);
bool recRemoved(ZCom_Node *_node, ZCom_ConnID _from,
eZCom_NodeRole _remoterole);
bool recFileIncoming(ZCom_Node *_node, ZCom_ConnID _from,
eZCom_NodeRole _remoterole, ZCom_FileTransID _fid,
ZCom_BitStream &_request);
bool recFileData(ZCom_Node *_node, ZCom_ConnID _from,
eZCom_NodeRole _remoterole, ZCom_FileTransID _fid) ;
bool recFileAborted(ZCom_Node *_node, ZCom_ConnID _from,
eZCom_NodeRole _remoterole, ZCom_FileTransID _fid) ;
bool recFileComplete(ZCom_Node *_node, ZCom_ConnID _from,
eZCom_NodeRole _remoterole, ZCom_FileTransID _fid);
// Waiting for deletion
bool m_deleteme;
PhysObject(PhysWorld* world, btDynamicsWorld* dynworld, const btTransform& initialpos);
// Data to be shared with peers through replicator interface:
string m_mesh;
string m_texture;
btVector3 m_meshScale;
// Local data
btScalar m_mass;
string m_body;
btVector3 m_bodyScale;
// whether an update is required.
bool m_dirty;
void recreateBody(); // Used to update physics simulation parameters such as mass or mesh type
// btCollisionShape* getShape(const string& shapetext);
public:
typedef ZCom_NodeID Id; // Used to implement weak refs to these objects
void setMass(btScalar kilos = -1);
//void setPosition(const btVector3& pos);
void setTexture(const std::string&);
void setBody(const std::string&);
void setMesh(const std::string&);
void setMeshScale(btVector3 in);
void setBodyScale(btVector3 in);
void push(const btVector3&);
// Death handling
void kill();
bool isZombie();
// If dirty, send status update to all peers.
void sendUpdate();
// Used to reference this object in the network
ZCom_NodeID getNetworkId();
~PhysObject(void);
};
class PhysObjectMotionState : public btMotionState
{
public:
PhysObjectMotionState(const btTransform &pos, ObjectReplicator *obj)
: m_body(NULL)
, m_obj(obj)
, m_pos(pos)
{}
void setBody(btRigidBody* body)
{
m_body = body;
}
void reset()
{
m_obj = NULL;
}
virtual ~PhysObjectMotionState() {
}
virtual void getWorldTransform(btTransform &pos) const {
pos = m_pos;
}
virtual void setWorldTransform(const btTransform &worldTrans) {
if(!m_obj || !m_body) return; // silently return before we set a node
btVector3 pos = worldTrans.getOrigin();//matr.getTranslation();
btQuaternion rot = worldTrans.getRotation();
// read velocity and omega from rigidbody
btVector3 vel;
if(m_body)
{
vel = m_body->getLinearVelocity();
}
m_obj->Input(pos,rot, vel, btVector4(0,0,0,0));
// irr::core::matrix4 matr;
// btTransformToIrrlichtMatrix(worldTrans, matr);
// m_obj->setRotation(matr.getRotationDegrees());
// m_obj->setPosition(matr.getTranslation());
m_pos = worldTrans;
//std::cout << "SERVER: " << pos.x() << ", " << pos.y() << ", " << pos.z() << std::endl;
}
protected:
btRigidBody* m_body;
ObjectReplicator *m_obj;
btTransform m_pos;
};
}
#endif