101 lines
2.2 KiB
C++
101 lines
2.2 KiB
C++
/* -----------------------------------------------------------------------------
|
|
GSFramework
|
|
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
|
----------------------------------------------------------------------------- */
|
|
|
|
|
|
#include "physic/physic_shape.h"
|
|
#include "math/matrix4.h"
|
|
|
|
using namespace GS::S3D;
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
GS::Matrix4 PhysicShape::GetMatrix()
|
|
{ return Matrix4::TransformationMatrix(position, rotation, dimensions); }
|
|
void PhysicShape::SetMatrix(const GS::Matrix4 &m)
|
|
{ m.Decompose(&position, &dimensions, &rotation); }
|
|
//------------------------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------------------------
|
|
bool PhysicShape::Set(Type t, const char *p)
|
|
{
|
|
Free();
|
|
|
|
type = t;
|
|
path = p;
|
|
return true;
|
|
}
|
|
bool PhysicShape::Set(Type t, const GS::Vector4 &d)
|
|
{
|
|
Free();
|
|
|
|
type = t;
|
|
dimensions = d;
|
|
return true;
|
|
}
|
|
bool PhysicShape::Set(float *ph, int w, int h, float r)
|
|
{
|
|
Free();
|
|
|
|
type = TypeHeightmap;
|
|
|
|
// Resample the terrain heightfield to a meter resolution.
|
|
width = (int)((float)w * r);
|
|
height = (int)((float)h * r);
|
|
|
|
//
|
|
float i = (float)w / (float)width, j = (float)h / (float)height;
|
|
|
|
heightmap.Allocate(width * height);
|
|
|
|
if (float *po = heightmap)
|
|
{
|
|
float y = 0;
|
|
for (int v = 0; v < height; ++v)
|
|
{
|
|
float x = 0;
|
|
for (int u = 0; u < width; ++u)
|
|
{
|
|
// *po++ = ph[int(h - y - 1) * w + int(x)];
|
|
*po++ = ph[int(y) * w + int(x)];
|
|
x += i;
|
|
}
|
|
y += j;
|
|
}
|
|
}
|
|
|
|
resolution = 1.f; // Resampled
|
|
return true;
|
|
}
|
|
void PhysicShape::Free()
|
|
{
|
|
type = TypeNone;
|
|
heightmap.Free();
|
|
}
|
|
//------------------------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------------------------
|
|
PhysicShape::PhysicShape()
|
|
{
|
|
type = TypeNone;
|
|
mass = 0; // Static.
|
|
|
|
position.Set();
|
|
rotation.Set();
|
|
dimensions.Set(1, 1, 1);
|
|
scale.Set(1, 1, 1);
|
|
|
|
restitution = 0.5;
|
|
static_friction = 0.5;
|
|
dynamic_friction = 0.5;
|
|
|
|
width = height = 0;
|
|
resolution = 1;
|
|
}
|
|
PhysicShape::~PhysicShape()
|
|
{
|
|
Free();
|
|
}
|
|
//------------------------------------------------------------------------------
|