started adding bullets physics
[ltanks.git] / player.cc
blobafab162b5988f3e6cf39aa8181f3b68526e4ad9d
1 #include "player.h"
2 #include "utils.h"
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) {
7 Player::~Player() {
10 void Player::init(SceneManager *sm, const std::string &name) {
11 // bullet
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");
23 sceneMgr = sm;
25 // rigid body
26 btCollisionShape *pl_shape = new btBoxShape(btVector3(2.5,2.5,2.5));
27 btVector3 inertia(0,0,0);
28 float mass = 1.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) {
42 key_up = up;
43 key_down = down;
44 key_left = left;
45 key_right = right;
46 key_fire = fire;
49 void Player::moveForward() {
50 // if (speed < 1.0)
51 // speed += 0.001;
52 speed = 5.0;
55 void Player::moveBack() {
56 // if (speed > -0.1)
57 // speed -= 0.001;
58 speed = -5.0;
61 void Player::turnLeft() {
62 turn_speed = -0.03;
65 void Player::turnRight() {
66 turn_speed = 0.03;
69 void Player::fire(btDynamicsWorld *col_world) {
70 if (fired < MAX_BULLETS) {
71 Vector3 dir = aim_to - node->getWorldPosition();
72 dir.y = 0.0;
73 dir.normalise();
74 Vector3 pos = node->getWorldPosition()+dir*(cube_size/2+bullet_size*2);
76 Bullet b;
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);
81 btTransform tran;
82 tran.setIdentity();
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);
101 b.ttl = 30;
103 bullets.push_back(b);
105 fired++;
109 void Player::aim(const Vector3 &p) {
110 aim_to = p;
113 void Player::setPos(Vector3 p) {
114 btTransform tran;
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;
140 btTransform tran;
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];
147 fwd.normalize();
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());
164 speed = 0.0;
165 turn_speed = 0.0;
168 Bullet* Player::getBulletByNode(SceneNode *node) {
169 // for (int i = 0; i < MAX_BULLETS; i++)
170 // if (node == bullets[i].node)
171 // return bullets+i;
173 return NULL;