Physics2D: Expose elasticity/friction/surface velocity

This commit is contained in:
Lynix
2018-10-09 23:22:28 +02:00
parent dc6fbfc90f
commit 30348525d7
8 changed files with 294 additions and 14 deletions

View File

@@ -20,9 +20,12 @@ namespace Nz
{
cpShape* shape = (*shapes)[i];
cpShapeSetCollisionType(shape, cpFloat(m_collisionId));
cpShapeSetElasticity(shape, cpFloat(m_elasticity));
cpShapeSetFilter(shape, filter);
cpShapeSetCollisionType(shape, m_collisionId);
cpShapeSetFriction(shape, cpFloat(m_friction));
cpShapeSetSensor(shape, (m_trigger) ? cpTrue : cpFalse);
cpShapeSetSurfaceVelocity(shape, cpv(cpFloat(m_surfaceVelocity.x), cpFloat(m_surfaceVelocity.y)));
}
return shapeCount;

View File

@@ -52,7 +52,10 @@ namespace Nz
CopyBodyData(object.GetHandle(), m_handle);
for (std::size_t i = 0; i < m_shapes.size(); ++i)
{
CopyShapeData(object.m_shapes[i], m_shapes[i]);
m_shapes[i]->bb = cpShapeCacheBB(object.m_shapes[i]);
}
}
RigidBody2D::RigidBody2D(RigidBody2D&& object) :
@@ -186,8 +189,20 @@ namespace Nz
}
RadianAnglef RigidBody2D::GetAngularVelocity() const
return float(cpBodyGetAngularVelocity(m_handle));
{
return float(cpBodyGetAngularVelocity(m_handle));
}
float Nz::RigidBody2D::GetElasticity(std::size_t shapeIndex) const
{
assert(shapeIndex < m_shapes.size());
return float(cpShapeGetElasticity(m_shapes[shapeIndex]));
}
float Nz::RigidBody2D::GetFriction(std::size_t shapeIndex) const
{
assert(shapeIndex < m_shapes.size());
return float(cpShapeGetFriction(m_shapes[shapeIndex]));
}
const Collider2DRef& RigidBody2D::GetGeom() const
@@ -247,6 +262,13 @@ namespace Nz
return std::distance(m_shapes.begin(), it);
}
Vector2f Nz::RigidBody2D::GetSurfaceVelocity(std::size_t shapeIndex) const
{
assert(shapeIndex < m_shapes.size());
cpVect vel = cpShapeGetSurfaceVelocity(m_shapes[shapeIndex]);
return Vector2f(static_cast<float>(vel.x), static_cast<float>(vel.y));
}
void* RigidBody2D::GetUserdata() const
{
return m_userData;
@@ -286,8 +308,32 @@ namespace Nz
void RigidBody2D::SetAngularVelocity(const RadianAnglef& angularVelocity)
{
cpBodySetAngularVelocity(m_handle, angularVelocity.angle);
}
void RigidBody2D::SetElasticity(float friction)
{
cpFloat frict(friction);
for (cpShape* shape : m_shapes)
cpShapeSetElasticity(shape, frict);
}
void RigidBody2D::SetElasticity(std::size_t shapeIndex, float friction)
{
assert(shapeIndex < m_shapes.size());
cpShapeSetElasticity(m_shapes[shapeIndex], cpFloat(friction));
}
void RigidBody2D::SetFriction(float friction)
{
cpFloat frict(friction);
for (cpShape* shape : m_shapes)
cpShapeSetFriction(shape, frict);
}
void RigidBody2D::SetFriction(std::size_t shapeIndex, float friction)
{
assert(shapeIndex < m_shapes.size());
cpShapeSetFriction(m_shapes[shapeIndex], cpFloat(friction));
}
void RigidBody2D::SetGeom(Collider2DRef geom, bool recomputeMoment)
@@ -414,6 +460,19 @@ namespace Nz
}
}
void RigidBody2D::SetSurfaceVelocity(const Vector2f& surfaceVelocity)
{
Vector2<cpFloat> velocity(surfaceVelocity.x, surfaceVelocity.y);
for (cpShape* shape : m_shapes)
cpShapeSetSurfaceVelocity(shape, cpv(velocity.x, velocity.y));
}
void RigidBody2D::SetSurfaceVelocity(std::size_t shapeIndex, const Vector2f& surfaceVelocity)
{
assert(shapeIndex < m_shapes.size());
cpShapeSetSurfaceVelocity(m_shapes[shapeIndex], cpv(cpFloat(surfaceVelocity.x), cpFloat(surfaceVelocity.y)));
}
void RigidBody2D::SetStatic(bool setStaticBody)
{
m_isStatic = setStaticBody;
@@ -549,4 +608,10 @@ namespace Nz
cpBodySetVelocity(to, cpBodyGetVelocity(from));
}
void RigidBody2D::CopyShapeData(cpShape* from, cpShape* to)
{
cpShapeSetElasticity(to, cpShapeGetElasticity(from));
cpShapeSetFriction(to, cpShapeGetFriction(from));
cpShapeSetSurfaceVelocity(to, cpShapeGetSurfaceVelocity(from));
}
}