From: Patrik Gornicz Date: Fri, 1 Aug 2008 23:03:01 +0000 (-0400) Subject: basic gravity working through effects X-Git-Tag: physics-premerge~179 X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=commitdiff_plain;h=6aad402ae2635bb96bd5a67e6054c8e65ea8df68;p=libbear.git basic gravity working through effects --- diff --git a/src/Effects/Effect.h b/src/Effects/Effect.h index 58c8afa..963ecb2 100644 --- a/src/Effects/Effect.h +++ b/src/Effects/Effect.h @@ -2,7 +2,9 @@ #define EFFECT_H #include "../Vector2.h" -#include "../Entities/PhysicsEntity.h" + +//#include "../Entities/PhysicsEntity.h" +class PhysicsEntity; /// ***** Header Class ***** class Effect diff --git a/src/Effects/Gravity.cpp b/src/Effects/Gravity.cpp index d7852d3..c8394b4 100644 --- a/src/Effects/Gravity.cpp +++ b/src/Effects/Gravity.cpp @@ -9,17 +9,17 @@ Gravity::~Gravity() } -Vector2 Gravity::positionDelta(const PhysicsEntity*, float) +Vector2 Gravity::positionDelta(const PhysicsEntity*, float) const { return Vector2(0, 0); } -Vector2 Gravity::velocityDelta(const PhysicsEntity*, float) +Vector2 Gravity::velocityDelta(const PhysicsEntity*, float) const { return Vector2(0, 0); } -Vector2 Gravity::forceDelta(const PhysicsEntity*, float) +Vector2 Gravity::forceDelta(const PhysicsEntity*, float) const { - return Vector2(0, 0.000001); + return Vector2(0, 0.001); } diff --git a/src/Effects/Gravity.h b/src/Effects/Gravity.h index f6dd459..dc04726 100644 --- a/src/Effects/Gravity.h +++ b/src/Effects/Gravity.h @@ -9,11 +9,11 @@ class Gravity: public Effect { public: Gravity(); - ~Gravity(); + virtual ~Gravity(); - Vector2 positionDelta(const PhysicsEntity*, float); - Vector2 velocityDelta(const PhysicsEntity*, float); - Vector2 forceDelta(const PhysicsEntity*, float); + Vector2 positionDelta(const PhysicsEntity*, float) const; + Vector2 velocityDelta(const PhysicsEntity*, float) const; + Vector2 forceDelta(const PhysicsEntity*, float) const; }; #endif // GRAVITY_H diff --git a/src/Entities/PhysicsEntity.cpp b/src/Entities/PhysicsEntity.cpp index c48d7d0..727b89f 100644 --- a/src/Entities/PhysicsEntity.cpp +++ b/src/Entities/PhysicsEntity.cpp @@ -2,16 +2,18 @@ #include "../debug.h" #include "../Vector2.h" +#include "../Effects/Effect.h" +#include "../Effects/Gravity.h" /// ***** Public Class Methods ***** PhysicsEntity::PhysicsEntity(const Vector2& pos) : Entity(pos), force(0,0), mass(1), CoR(1) { - + g = new Gravity(); } PhysicsEntity::~PhysicsEntity() { - + delete g; } void PhysicsEntity::update(float time_step) @@ -24,12 +26,26 @@ void PhysicsEntity::update(float time_step) Vector2 PhysicsEntity::positionAt(float time_step) const { - return force/mass / 2 * time_step * time_step + velocity * time_step + position; + Vector2 newPosition = position; + Vector2 newVelocity = velocity; + Vector2 newForce = force; + + newPosition += g->positionDelta(this, time_step); + newVelocity += g->velocityDelta(this, time_step); + newForce += g->forceDelta(this, time_step); + + return newForce/mass / 2 * time_step * time_step + newVelocity * time_step + newPosition; } Vector2 PhysicsEntity::velocityAt(float time_step) const { - return force/mass / 2 * time_step + velocity; + Vector2 newForce = force; + Vector2 newVelocity = velocity; + + newForce += g->forceDelta(this, time_step); + newVelocity += g->velocityDelta(this, time_step); + + return newForce/mass / 2 * time_step + newVelocity; } void PhysicsEntity::applyForce(const Vector2& force) diff --git a/src/Entities/PhysicsEntity.h b/src/Entities/PhysicsEntity.h index dd6455b..4df29b3 100644 --- a/src/Entities/PhysicsEntity.h +++ b/src/Entities/PhysicsEntity.h @@ -2,32 +2,35 @@ #define PHYSICS_H #include "Entity.h" +#include "../Effects/Effect.h" #include "../Vector2.h" /// ***** Header Class ***** class PhysicsEntity: public Entity { - protected: - PhysicsEntity(const Vector2&); + protected: + PhysicsEntity(const Vector2&); - public: - virtual ~PhysicsEntity(); + public: + virtual ~PhysicsEntity(); - virtual void update(float); + virtual void update(float); - virtual Vector2 positionAt(float) const; - virtual Vector2 velocityAt(float) const; + virtual Vector2 positionAt(float) const; + virtual Vector2 velocityAt(float) const; - virtual void applyForce(const Vector2&); - virtual void applyImpulse(const Vector2&); + virtual void applyForce(const Vector2&); + virtual void applyImpulse(const Vector2&); - protected: - Vector2 force; + protected: + Vector2 force; - float mass; + float mass; - // Coefficient of Restitution - float CoR; + // Coefficient of Restitution + float CoR; + + Effect* g; }; #endif // PHYSICS_H diff --git a/src/Makefile b/src/Makefile index b7fcb11..6d5b94e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -119,6 +119,10 @@ git-tar: run: all ${TARGET} +.PHONY: gdb +gdb: all + gdb ${TARGET} + .PHONY: val val: all valgrind --leak-check=full ${TARGET}