Merge remote-tracking branch 'origin/GamePhysics' into Camera

This commit is contained in:
Dander7BD 2014-02-12 11:45:48 +01:00
commit 786c6bb3c1
12 changed files with 219 additions and 15 deletions

View File

@ -104,10 +104,13 @@ void AttatchmentMassDriver::ForcePush(const GameLogic::WEAPON_FIRE &usage, float
Oyster::Math::Float4x4 hitSpace = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/8,1,1,50); Oyster::Math::Float4x4 hitSpace = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/8,1,1,50);
Oyster::Collision3D::Frustrum hitFrustum = Oyster::Collision3D::Frustrum(Oyster::Math3D::ViewProjectionMatrix(aim,hitSpace)); Oyster::Collision3D::Frustrum hitFrustum = Oyster::Collision3D::Frustrum(Oyster::Math3D::ViewProjectionMatrix(aim,hitSpace));
Oyster::Collision3D::Cone *hitCone;
forcePushData args; forcePushData args;
args.pushForce = pushForce; args.pushForce = pushForce;
Oyster::Physics::API::Instance().ApplyEffect(hitFrustum,&args,ForcePushAction); //Oyster::Physics::API::Instance().ApplyEffect(hitFrustum,&args,ForcePushAction);
} }
/******************************************************** /********************************************************
@ -126,7 +129,7 @@ void AttatchmentMassDriver::ForceZip(const WEAPON_FIRE &usage, float dt)
void AttatchmentMassDriver::ForcePull(const WEAPON_FIRE &usage, float dt) void AttatchmentMassDriver::ForcePull(const WEAPON_FIRE &usage, float dt)
{ {
if(hasObject) return; //this test checks if the weapon already has something picked up, if so then it cant use this function //if(hasObject) return; //this test checks if the weapon already has something picked up, if so then it cant use this function
PickUpObject(usage,dt); //first test if there is a nearby object to pickup PickUpObject(usage,dt); //first test if there is a nearby object to pickup
@ -142,19 +145,15 @@ void AttatchmentMassDriver::ForcePull(const WEAPON_FIRE &usage, float dt)
forcePushData args; forcePushData args;
args.pushForce = -pushForce; args.pushForce = -pushForce;
Oyster::Physics::API::Instance().ApplyEffect(hitFrustum,&args,ForcePushAction); //Oyster::Physics::API::Instance().ApplyEffect(hitFrustum,&args,ForcePushAction);
} }
void AttatchmentMassDriver::PickUpObject(const WEAPON_FIRE &usage, float dt) void AttatchmentMassDriver::PickUpObject(const WEAPON_FIRE &usage, float dt)
{ {
Oyster::Math::Float3 pos = owner->GetPosition() + owner->GetLookDir().GetNormalized()*5; Oyster::Math::Float3 pos = owner->GetPosition() + owner->GetLookDir().GetNormalized()*5;
Oyster::Collision3D::Sphere hitSphere = Oyster::Collision3D::Sphere(pos,20); Oyster::Collision3D::Sphere *hitSphere = new Oyster::Collision3D::Sphere(pos,20);
/*Oyster::Math::Float4x4 aim = Oyster::Math3D::ViewMatrix_LookAtDirection(owner->GetLookDir(), owner->GetRigidBody()->GetGravityNormal(), owner->GetPosition());
Oyster::Math::Float4x4 hitSpace = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/4,1,1,20);
Oyster::Collision3D::Frustrum hitFrustum = Oyster::Collision3D::Frustrum(Oyster::Math3D::ViewProjectionMatrix(aim,hitSpace));
*/
Oyster::Physics::API::Instance().ApplyEffect(hitSphere,this,AttemptPickUp); Oyster::Physics::API::Instance().ApplyEffect(hitSphere,this,AttemptPickUp);
delete hitSphere;
} }

View File

@ -12,6 +12,8 @@ using namespace ::Utility::Value;
API_Impl API_instance; API_Impl API_instance;
API & API::Instance() API & API::Instance()
{ {
return API_instance; return API_instance;
@ -251,9 +253,73 @@ void API_Impl::ReleaseFromLimbo( const ICustomBody* objRef )
} }
void API_Impl::ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void* args, void(hitAction)(ICustomBody*, void*) ) void API_Impl::ApplyEffect(Oyster::Collision3D::ICollideable* collideable, void* args, EventAction_ApplyEffect effect)
{ {
btRigidBody* body;
btCollisionShape* shape;
btMotionState* state;
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(0, NULL, NULL);
Sphere* sphere;
Box* box;
Cone* cone;
switch(collideable->type)
{
case ICollideable::Type::Type_sphere:
sphere = dynamic_cast<Sphere*>(collideable);
// Add collision shape
shape = new btSphereShape(sphere->radius);
// Add motion state
state = new btDefaultMotionState(btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f),btVector3(sphere->center.x, sphere->center.y, sphere->center.z)));
// Add rigid body
rigidBodyCI = btRigidBody::btRigidBodyConstructionInfo(0, state, shape);
body = new btRigidBody(rigidBodyCI);
break;
case ICollideable::Type::Type_box:
box = dynamic_cast<Box*>(collideable);
// Add collision shape
shape = new btBoxShape(btVector3(box->boundingOffset.x, box->boundingOffset.y, box->boundingOffset.z));
// Add motion state
state = new btDefaultMotionState(btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f),btVector3(box->center.x, box->center.y, box->center.z)));
// Add rigid body
rigidBodyCI = btRigidBody::btRigidBodyConstructionInfo(0, state, shape);
body = new btRigidBody(rigidBodyCI);
break;
case ICollideable::Type::Type_cone:
cone = dynamic_cast<Cone*>(collideable);
// Add collision shape
shape = new btConeShape(cone->radius, cone->height.GetLength());
// Add motion state
state = new btDefaultMotionState(btTransform(btQuaternion(btVector3(cone->height.x, cone->height.y, cone->height.z).normalized(), 0.0f),btVector3(cone->position.x, cone->position.y, cone->position.z)));
// Add rigid body
rigidBodyCI = btRigidBody::btRigidBodyConstructionInfo (0, state, shape);
body = new btRigidBody(rigidBodyCI);
break;
default:
return;
}
ContactSensorCallback callback(*body, effect, args);
this->dynamicsWorld->contactTest(body, callback);
delete state;
state = NULL;
delete shape;
shape = NULL;
delete body;
body = NULL;
} }
namespace Oyster namespace Oyster

View File

@ -12,6 +12,42 @@ namespace Oyster
class API_Impl : public API class API_Impl : public API
{ {
public: public:
struct ContactSensorCallback : public btCollisionWorld::ContactResultCallback
{
ContactSensorCallback(btRigidBody& contactBody, EventAction_ApplyEffect effect, void* args)
: btCollisionWorld::ContactResultCallback(), body(contactBody), func(effect), args(args) {}
btRigidBody& body;
EventAction_ApplyEffect func;
void* args;
virtual bool needsCollision(btBroadphaseProxy* proxy) const
{
if(!btCollisionWorld::ContactResultCallback::needsCollision(proxy))
return false;
return body.checkCollideWithOverride(static_cast<btCollisionObject*>(proxy->m_clientObject));
}
virtual btScalar addSingleResult(btManifoldPoint& cp, const btCollisionObjectWrapper* colObj0, int partId0, int index0, const btCollisionObjectWrapper* colObj1, int partId1, int index1)
{
btVector3 pt;
if(colObj0->m_collisionObject == &body)
{
pt = cp.m_localPointA;
this->func((ICustomBody*)(colObj1->getCollisionObject()->getUserPointer()), this->args);
}
else
{
assert(colObj1->m_collisionObject == &body && "Body does not match either collision object");
pt = cp.m_localPointB;
this->func((ICustomBody*)(colObj0->getCollisionObject()->getUserPointer()), this->args);
}
return 0;
}
};
API_Impl(); API_Impl();
virtual ~API_Impl(); virtual ~API_Impl();
@ -33,7 +69,7 @@ namespace Oyster
void UpdateWorld(); void UpdateWorld();
void ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void* args, void(hitAction)(ICustomBody*, void*) ); void ApplyEffect(Oyster::Collision3D::ICollideable* collideable, void* args, EventAction_ApplyEffect effect);
private: private:
btBroadphaseInterface* broadphase; btBroadphaseInterface* broadphase;

View File

@ -67,6 +67,11 @@ void SimpleRigidBody::SetState( const SimpleRigidBody::State &state )
this->state = state; this->state = state;
} }
void SimpleRigidBody::ApplyImpulse(Float3 impulse)
{
this->rigidBody->applyImpulse(btVector3(impulse.x, impulse.y, impulse.z), btVector3(0.0f, 0.0f, 0.0f));
}
void SimpleRigidBody::SetCollisionShape(btCollisionShape* shape) void SimpleRigidBody::SetCollisionShape(btCollisionShape* shape)
{ {
this->collisionShape = shape; this->collisionShape = shape;

View File

@ -18,6 +18,8 @@ namespace Oyster
State& GetState( State &targetMem ) const; State& GetState( State &targetMem ) const;
void SetState( const State &state ); void SetState( const State &state );
void ApplyImpulse(Math::Float3 impulse);
void SetCollisionShape(btCollisionShape* shape); void SetCollisionShape(btCollisionShape* shape);
void SetMotionState(btDefaultMotionState* motionState); void SetMotionState(btDefaultMotionState* motionState);
void SetRigidBody(btRigidBody* rigidBody); void SetRigidBody(btRigidBody* rigidBody);

View File

@ -45,6 +45,7 @@ namespace Oyster
typedef Struct::Gravity Gravity; typedef Struct::Gravity Gravity;
typedef void (*EventAction_Destruction)( ::Utility::DynamicMemory::UniquePointer<ICustomBody> proto ); typedef void (*EventAction_Destruction)( ::Utility::DynamicMemory::UniquePointer<ICustomBody> proto );
typedef void (*EventAction_ApplyEffect)(ICustomBody* collidedBody, void* args);
/** Gets the Physics instance. */ /** Gets the Physics instance. */
static API & Instance(); static API & Instance();
@ -99,7 +100,7 @@ namespace Oyster
* @param hitAction: A function that contains the effect. Parameterlist contains the custom body * @param hitAction: A function that contains the effect. Parameterlist contains the custom body
the collideable hits, and the arguments sent to the function. the collideable hits, and the arguments sent to the function.
********************************************************/ ********************************************************/
virtual void ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void* args, void(hitAction)(ICustomBody*, void*) ) = 0; virtual void ApplyEffect(Oyster::Collision3D::ICollideable* collideable, void* args, EventAction_ApplyEffect effect) = 0;
protected: protected:
virtual ~API() {} virtual ~API() {}
@ -134,6 +135,8 @@ namespace Oyster
virtual void SetSubscription(EventAction_AfterCollisionResponse function) = 0; virtual void SetSubscription(EventAction_AfterCollisionResponse function) = 0;
virtual void SetSubscription(EventAction_Move function) = 0; virtual void SetSubscription(EventAction_Move function) = 0;
virtual void ApplyImpulse(::Oyster::Math::Float3 impulse) = 0;
virtual void SetLinearVelocity(::Oyster::Math::Float3 velocity) = 0; virtual void SetLinearVelocity(::Oyster::Math::Float3 velocity) = 0;
virtual void SetPosition(::Oyster::Math::Float3 position) = 0; virtual void SetPosition(::Oyster::Math::Float3 position) = 0;
virtual void SetRotation(::Oyster::Math::Float4 quaternion) = 0; virtual void SetRotation(::Oyster::Math::Float4 quaternion) = 0;

View File

@ -0,0 +1,44 @@
/////////////////////////////////////////////////////////////////////
// Created by Erik Persson 2014
/////////////////////////////////////////////////////////////////////
#include "Cone.h"
#include "OysterCollision3D.h"
using namespace ::Oyster::Collision3D;
using namespace ::Oyster::Math3D;
Cone::Cone( ) : ICollideable(Type_cone)
{
this->radius = 1;
this->height = Oyster::Math::Float3(0,0,0);
}
Cone::Cone( const ::Oyster::Math::Float3 &height, const Oyster::Math::Float3 &position, const ::Oyster::Math::Float &radius )
{
this->radius = radius;
this->height = height;
this->position = position;
}
Cone::Cone( const ::Oyster::Math::Float4 &height, const Oyster::Math::Float4 &position, const ::Oyster::Math::Float &radius )
{
this->radius = radius;
this->height = (Oyster::Math::Float3)height;
this->position = (Oyster::Math::Float3)position;
}
Cone::~Cone( )
{
}
Cone & Cone::operator = ( const Cone &cone )
{
this->radius = cone.radius;
this->height = cone.height;
this->position = cone.position;
return *this;
}

View File

@ -0,0 +1,39 @@
/////////////////////////////////////////////////////////////////////
// Created by Erik Persson 2014
/////////////////////////////////////////////////////////////////////
#pragma once
#ifndef OYSTER_COLLISION_3D_CONE_H
#define OYSTER_COLLISION_3D_CONE_H
#include "OysterMath.h"
#include "ICollideable.h"
namespace Oyster
{
namespace Collision3D
{
class Cone : public ICollideable
{
public:
Cone();
Cone( const ::Oyster::Math::Float3 &height, const Oyster::Math::Float3 &position, const ::Oyster::Math::Float &radius );
Cone( const ::Oyster::Math::Float4 &height, const Oyster::Math::Float4 &position, const ::Oyster::Math::Float &radius );
virtual ~Cone( );
Cone & operator = ( const Cone &Cone );
Oyster::Math::Float3 height;
Oyster::Math::Float3 position;
Oyster::Math::Float radius;
};
}
}
#endif

View File

@ -26,7 +26,8 @@ namespace Oyster { namespace Collision3D //! Contains a collection of 3D shapes
Type_box_axis_aligned, Type_box_axis_aligned,
Type_box, Type_box,
Type_frustrum, Type_frustrum,
Type_line Type_line,
Type_cone,
}; };
const Type type; const Type type;

View File

@ -17,6 +17,7 @@
#include "Box.h" #include "Box.h"
#include "Frustrum.h" #include "Frustrum.h"
#include "Line.h" #include "Line.h"
#include "Cone.h"
namespace Oyster { namespace Collision3D { namespace Utility namespace Oyster { namespace Collision3D { namespace Utility
{ {

View File

@ -154,6 +154,7 @@
<ClInclude Include="BoxAxisAligned.h" /> <ClInclude Include="BoxAxisAligned.h" />
<ClInclude Include="FluidDrag.h" /> <ClInclude Include="FluidDrag.h" />
<ClInclude Include="Frustrum.h" /> <ClInclude Include="Frustrum.h" />
<ClInclude Include="Cone.h" />
<ClInclude Include="ICollideable.h" /> <ClInclude Include="ICollideable.h" />
<ClInclude Include="Inertia.h" /> <ClInclude Include="Inertia.h" />
<ClInclude Include="Line.h" /> <ClInclude Include="Line.h" />
@ -172,6 +173,7 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="Box.cpp" /> <ClCompile Include="Box.cpp" />
<ClCompile Include="BoxAxisAligned.cpp" /> <ClCompile Include="BoxAxisAligned.cpp" />
<ClCompile Include="Cone.cpp" />
<ClCompile Include="FluidDrag.cpp" /> <ClCompile Include="FluidDrag.cpp" />
<ClCompile Include="Frustrum.cpp" /> <ClCompile Include="Frustrum.cpp" />
<ClCompile Include="ICollideable.cpp" /> <ClCompile Include="ICollideable.cpp" />

View File

@ -81,6 +81,9 @@
<ClInclude Include="Universe.h"> <ClInclude Include="Universe.h">
<Filter>Header Files\Collision</Filter> <Filter>Header Files\Collision</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Cone.h">
<Filter>Header Files\Collision</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Box.cpp"> <ClCompile Include="Box.cpp">
@ -131,5 +134,8 @@
<ClCompile Include="Inertia.cpp"> <ClCompile Include="Inertia.cpp">
<Filter>Source Files\Physics</Filter> <Filter>Source Files\Physics</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Cone.cpp">
<Filter>Source Files\Collision</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>