[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    playground/games/astrododge/src
From:       Rivo Laks <rivolaks () hot ! ee>
Date:       2008-07-01 17:43:24
Message-ID: 1214934204.168964.7022.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 826865 by rivol:

- Add GameObject::collision() method which is called for colliding objects
- Ship now has health level which is decreased when you collide with something (depending
  on the force of collision - bigger asteroids and more speed means more damage)

 M  +1 -0      gameobject.cpp  
 M  +1 -0      gameobject.h  
 M  +35 -1     gameworld.cpp  
 M  +5 -0      gameworld.h  
 M  +12 -0     ship.cpp  
 M  +3 -0      ship.h  


--- trunk/playground/games/astrododge/src/gameobject.cpp #826864:826865
@@ -32,6 +32,7 @@
 
     // ODE stuff
     body = dBodyCreate(GameWorld::instance()->odeWorld());
+    dBodySetData(body, this);
     setMass(radius*radius);
 
     geom = dCreateSphere(GameWorld::instance()->odeSpace(), radius);
--- trunk/playground/games/astrododge/src/gameobject.h #826864:826865
@@ -49,6 +49,7 @@
     virtual void openglTransform() const;
 
     virtual void addForce(const Vector3f& force);
+    virtual void collision(const Vector3f& force)  {}
 
     virtual void render();
 
--- trunk/playground/games/astrododge/src/gameworld.cpp #826864:826865
@@ -39,8 +39,25 @@
 {
     reinterpret_cast<GameWorld*>(data)->handleCollisionBetween(o0,o1);
 }
+Vector3f ode2Eigen(const dVector3& v)
+{
+    return Vector3f(v[0], v[1], v[2]);
+}
 
+struct GameWorld::OdeContactFeedback
+{
+    OdeContactFeedback(GameObject* obj1, GameObject* obj2)
+    {
+        object1 = obj1;
+        object2 = obj2;
+    }
 
+    GameObject* object1;
+    GameObject* object2;
+    dJointFeedback feedback;
+};
+
+
 GameWorld* GameWorld::mInstance = 0;
 
 GameWorld::GameWorld(QObject *parent, InputHandler* input)
@@ -96,6 +113,8 @@
         dSpaceCollide(mOdeSpace, this, &nearCallback);
         // Advance the simulation
         dWorldQuickStep(mOdeWorld, thisstep);
+
+        processContactFeedbacks();
     }
     mTime += elapsed;
 
@@ -124,6 +143,8 @@
         for (int i = 0; i < collisions; i++) {
             dBodyID geom1 = dGeomGetBody(o1);
             dBodyID geom2 = dGeomGetBody(o2);
+            GameObject* obj1 = reinterpret_cast<GameObject*>(dBodyGetData(geom1));
+            GameObject* obj2 = reinterpret_cast<GameObject*>(dBodyGetData(geom2));
 
             contacts[i].surface.mode = dContactBounce;
             contacts[i].surface.mu = dInfinity;
@@ -134,11 +155,24 @@
             contacts[i].surface.soft_cfm = 0.01;*/
 
             dJointID c = dJointCreateContact (mOdeWorld, mOdeContactGroup, &contacts[i]);
+            OdeContactFeedback* fb = new OdeContactFeedback(obj1, obj2);
+            dJointSetFeedback(c, &fb->feedback);
+            mContactFeedbacks.append(fb);
             dJointAttach (c, geom1, geom2);
         }
     }
 }
 
+void GameWorld::processContactFeedbacks()
+{
+    for (int i = 0; i < mContactFeedbacks.count(); i++) {
+        mContactFeedbacks[i]->object1->collision(ode2Eigen(mContactFeedbacks[i]->feedback.f1));
+        mContactFeedbacks[i]->object2->collision(ode2Eigen(mContactFeedbacks[i]->feedback.f2));
+        delete mContactFeedbacks[i];
+    }
+    mContactFeedbacks.clear();
+}
+
 void GameWorld::init()
 {
     mOdeWorld = dWorldCreate();
@@ -238,7 +272,7 @@
 
     mShip->lastshoottime = mTime;
     Bullet* b = new Bullet;
-    b->setPosition(mShip->position() + mShip->dir()*10);
+    b->setPosition(mShip->position() + mShip->dir()*12);
     b->setVelocity(mShip->dir() * 500);
     addObject(b);
     mBullets.append(b);
--- trunk/playground/games/astrododge/src/gameworld.h #826864:826865
@@ -77,8 +77,12 @@
 
     void collision(GameObject* a, GameObject* b);
 
+    void processContactFeedbacks();
 
+
 private:
+    class OdeContactFeedback;
+
     static GameWorld* mInstance;
     InputHandler* mInput;
 
@@ -88,6 +92,7 @@
     bool mGamePaused;
     float mTime;
     int mNextObjectId;
+    QList<OdeContactFeedback*> mContactFeedbacks;
 
     dWorldID mOdeWorld;
     dSpaceID mOdeSpace;
--- trunk/playground/games/astrododge/src/ship.cpp #826864:826865
@@ -17,13 +17,16 @@
 
 #include "ship.h"
 
+#include <KDebug>
 
+
 Ship::Ship() : GameObject(8)
 {
     acceleration = 10000;
     rotvelocity = Vector3f(0, 0, 0);
 
     lastshoottime = -1000;
+    health = 100;
 }
 
 Ship::~Ship()
@@ -40,3 +43,12 @@
     return orientation.transform(Vector3f(0, 1, 0));
 }
 
+void Ship::collision(const Vector3f& force)
+{
+    float forceNorm = force.norm();
+    float damage = forceNorm / 1000.0f;
+
+    kDebug() << "force:" << forceNorm << "= damage" << damage;
+    health = qMax(0.0f, health - damage);
+    kDebug() << "health is now" << health;
+}
--- trunk/playground/games/astrododge/src/ship.h #826864:826865
@@ -36,6 +36,8 @@
 
     virtual Type type() const  { return T_Ship; }
 
+    virtual void collision(const Vector3f& force);
+
     Vector3f dir() const;
     Vector3f up() const;
 
@@ -43,6 +45,7 @@
     Vector3f rotvelocity;
 
     float lastshoottime;
+    float health;
 
     TrackBall orientation;
 };
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic