4 Player::Player():cube_size(Vector3(5,5,5)),bullet_size(cube_size
*0.3),fired(0),node(NULL
),speed(0.0),angle(0.0) {
10 void Player::init(SceneManager
*sm
, const std::string
&name
) {
12 createCubeMesh(sm
, name
+"_mesh", "Red", 5);
14 node
= sm
->getRootSceneNode()->createChildSceneNode(name
);
15 Entity
*ent
= sm
->createEntity(name
+"_ent",name
+"_mesh");
16 // ent->setQueryFlags(PLAYER_QUERY_MASK);
17 node
->attachObject(ent
);
19 tmp_ent
= sm
->createEntity(name
+"_ent_bt_tmp",name
+"_mesh");
21 bullet_node
= sm
->getRootSceneNode()->createChildSceneNode(name
+"_bt");
26 btCollisionShape
*pl_shape
= new btBoxShape(btVector3(2.5,2.5,2.5));
27 btVector3
inertia(0,0,0);
29 pl_shape
->calculateLocalInertia(mass
, inertia
);
31 btTransform transform
;
32 transform
.setIdentity();
33 btDefaultMotionState
*ms
= new btDefaultMotionState(transform
);
34 btRigidBody::btRigidBodyConstructionInfo
rb_info(mass
, ms
, pl_shape
, inertia
);
35 body
= new btRigidBody(rb_info
);
36 // body->setSleepingThresholds(0.0, 0.0);
37 body
->setActivationState(DISABLE_DEACTIVATION
);
38 body
->setAngularFactor(0.0);
41 void Player::setKeys(OIS::KeyCode up
, OIS::KeyCode down
, OIS::KeyCode left
, OIS::KeyCode right
, OIS::KeyCode fire
) {
49 void Player::moveForward() {
55 void Player::moveBack() {
61 void Player::turnLeft() {
65 void Player::turnRight() {
69 void Player::fire(btDynamicsWorld
*col_world
) {
70 if (fired
< MAX_BULLETS
) {
71 Vector3 dir
= aim_to
- node
->getWorldPosition();
74 Vector3 pos
= node
->getWorldPosition()+dir
*(cube_size
/2+bullet_size
*2);
77 btCollisionShape
*shape
= new btBoxShape(btVector3(2.5,2.5,2.5)*0.3);
78 btVector3
inertia(0,0,0);
79 shape
->calculateLocalInertia(1.0, inertia
);
83 tran
.setOrigin(btVector3(pos
.x
, pos
.y
, pos
.z
));
84 btDefaultMotionState
*ms
= new btDefaultMotionState(tran
);
85 btRigidBody::btRigidBodyConstructionInfo
rb_info(1.0, ms
, shape
, inertia
);
86 b
.body
= new btRigidBody(rb_info
);
87 b
.body
->setActivationState(DISABLE_DEACTIVATION
);
88 b
.body
->activate(true);
89 b
.body
->setAngularFactor(0.0);
90 col_world
->addRigidBody(b
.body
);
91 b
.body
->applyCentralImpulse(btVector3(dir
.x
,dir
.y
,dir
.z
)*50);
92 // b.body->applyCentralForce(btVector3(0,0,2));
94 b
.node
= bullet_node
->createChildSceneNode();
95 b
.node
->attachObject(tmp_ent
);
96 b
.node
->scale(0.3, 0.3, 0.3);
97 b
.node
->setPosition(pos
);
98 b
.node
->setDirection(dir
, Node::TS_WORLD
);
99 b
.node
->setVisible(true);
103 bullets
.push_back(b
);
109 void Player::aim(const Vector3
&p
) {
113 void Player::setPos(Vector3 p
) {
115 body
->getMotionState()->getWorldTransform(tran
);
116 tran
.setOrigin(btVector3(p
.x
, p
.y
, p
.z
));
117 body
->getMotionState()->setWorldTransform(tran
);
118 body
->setCenterOfMassTransform(tran
);
120 node
->setPosition(p
);
122 void Player::setDir(Vector3 d
) {
123 //node->setDirection(d);
126 const Vector3
& Player::getPos() {
127 return node
->getPosition();
130 Vector3
Player::getDir() {
131 return -node
->getOrientation().zAxis();
134 void Player::preupdate(float dt
) {
137 void Player::postupdate(float dt
) {
138 angle
+= turn_speed
*0.3;
141 // body->setAngularVelocity(btVector3(0,0,0));
142 body
->getMotionState()->getWorldTransform(tran
);
143 tran
.setRotation(btQuaternion(btVector3(0.0,1.0,0.0),angle
));
144 // body->setAngularVelocity(btVector3(0,0,0));
146 btVector3 fwd
= tran
.getBasis()[2];
149 body
->setLinearVelocity(fwd
*speed
);
150 body
->getMotionState()->setWorldTransform(tran
);
151 body
->setCenterOfMassTransform(tran
);
153 btVector3 p
= body
->getCenterOfMassPosition();
154 // btVector3 d = body->getOrientation() * btVector3(0,0,1);
156 node
->setPosition(p
.x(),p
.y(),p
.z());
157 node
->setDirection(fwd
[0],fwd
[1],fwd
[2], Node::TS_WORLD
);
159 for (std::vector
<Bullet
>::iterator i
= bullets
.begin(); i
!= bullets
.end(); i
++) {
160 p
= i
->body
->getCenterOfMassPosition();
161 i
->node
->setPosition(p
.x(), p
.y(), p
.z());
168 Bullet
* Player::getBulletByNode(SceneNode
*node
) {
169 // for (int i = 0; i < MAX_BULLETS; i++)
170 // if (node == bullets[i].node)