Permalink
Browse files

Added rotational friction to Entity.

  • Loading branch information...
1 parent 019ab07 commit 56c1c9c81d8c078eaeaa456b911bc603556b7503 namaste committed Oct 12, 2008
Showing with 32 additions and 8 deletions.
  1. +2 −1 soylent/GoD/avatar.h
  2. +12 −1 soylent/GoD/entity.h
  3. +18 −6 soylent/GoD/main.cpp
View
@@ -12,9 +12,10 @@
class Avatar : public Entity
{
public:
- Avatar(cpSpace *space, cpBody *body, cpShape *shape)
+ Avatar(cpSpace *space, cpBody *body, cpShape *shape, cpFloat radius)
: Entity(space, body, shape)
{
+ myRadius = radius;
thrust = 900.0f;
uFrict = 0.03f;
}
View
@@ -9,13 +9,15 @@
class Entity
{
public:
- Entity(cpSpace *space, cpBody *body, cpShape *shape)
+ Entity(cpSpace *space, cpBody *body, cpShape *shape, cpFloat radius = 1)
{
+ myRadius = radius;
myBody = body;
myBody->data = this;
cpSpaceAddBody(space, myBody);
cpSpaceAddShape(space, shape);
+
thrust = 900.0f;
uFrict = 0.03f;
}
@@ -30,6 +32,7 @@ class Entity
cpBody* myBody;
cpFloat thrust;
cpFloat uFrict;
+ cpFloat myRadius;
void ApplyFriction()
{
@@ -38,6 +41,14 @@ class Entity
cpFloat mag = cpvlength(vel) * uFrict;
if(mag != 0) Impulse(cpvmult(dir, mag));
+
+ //Apply rotational friction too
+ mag = myBody->w * uFrict / 2;
+ if(mag != 0)
+ {
+ cpBodyApplyImpulse(myBody, cpv(0, -mag), cpv(myRadius, 0));
+ cpBodyApplyImpulse(myBody, cpv(0, mag), cpv(-myRadius, 0));
+ }
}
void Impulse(cpVect force)
View
@@ -19,6 +19,7 @@
#include "avatar.h"
+using namespace std;
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
@@ -29,7 +30,7 @@ cpSpace *space;
cpBody *staticBody;
Avatar *avatar;
-Entity **entities;
+list<Entity*> entities;
Input_Manager *inputManager;
FTFont *font;
@@ -86,6 +87,7 @@ void setup()
cpBody *body;
cpShape *shape;
+ Entity *entity;
//we are making polygons (pentagons)
cpVect verts[NUM_VERTS];
@@ -99,20 +101,24 @@ void setup()
body = cpBodyNew(1.0, cpMomentForPoly(1.0, NUM_VERTS, verts, cpvzero));
// body = cpBodyNew(1.0, cpMomentForCircle(1.0, 0.0, 10.0, cpvzero));
body->p = cpv((i+1)*SCREEN_WIDTH/float(NUM_POLYS+1) - SCREEN_WIDTH/2.0f, SCREEN_HEIGHT/6.0f);
- cpSpaceAddBody(space, body);
+ //cpSpaceAddBody(space, body);
shape = cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero);
// shape = cpCircleShapeNew(body, 10.0, cpvzero);
shape->e = 0.0f; shape->u = 0.04f;
- cpSpaceAddShape(space, shape);
+ //cpSpaceAddShape(space, shape);
+
+ entity = new Entity(space, body, shape, 10);
+ entities.push_back(entity);
}
//Avatar
body = cpBodyNew(1.0, cpMomentForPoly(1.0, NUM_VERTS, verts, cpvzero));
body->p = cpv(0, -SCREEN_HEIGHT/6);
shape = cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero);
shape->e = 0.0f; shape->u = 0.04f;
- avatar = new Avatar(space, body, shape);
+ avatar = new Avatar(space, body, shape, 10);
+ entities.push_back(avatar);
const int numEvents = 8;
SDL_Event events[numEvents];
@@ -169,9 +175,9 @@ void logic ()
static void
eachBody(cpBody *body, void *unused)
{
- if(body->data != NULL) {
+ /*if(body->data != NULL) {
if(body->data == avatar) avatar->Update();
- }
+ }*/
if(body->p.x > SCREEN_WIDTH/2.0) {
body->p.x = -SCREEN_WIDTH/2.0;
@@ -189,6 +195,12 @@ eachBody(cpBody *body, void *unused)
void update()
{
+ list<Entity*>::iterator it;
+ for( it=entities.begin(); it != entities.end(); it++ )
+ {
+ (*it)->Update();
+ }
+
int steps = 1;
cpFloat dt = 1.0/FRAME_CAP/(cpFloat)steps;

0 comments on commit 56c1c9c

Please sign in to comment.