61 lines
1.2 KiB
C++
61 lines
1.2 KiB
C++
// Copyright (C) 2015 Jérôme Leclercq
|
|
// This file is part of the "Nazara Engine - Physics 2D module"
|
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
|
|
|
#include <Nazara/Physics2D/PhysWorld2D.hpp>
|
|
#include <chipmunk/chipmunk.h>
|
|
#include <Nazara/Physics2D/Debug.hpp>
|
|
|
|
namespace Nz
|
|
{
|
|
PhysWorld2D::PhysWorld2D() :
|
|
m_stepSize(0.005f),
|
|
m_timestepAccumulator(0.f)
|
|
{
|
|
m_handle = cpSpaceNew();
|
|
cpSpaceSetUserData(m_handle, this);
|
|
}
|
|
|
|
PhysWorld2D::~PhysWorld2D()
|
|
{
|
|
cpSpaceFree(m_handle);
|
|
}
|
|
|
|
Vector2f PhysWorld2D::GetGravity() const
|
|
{
|
|
cpVect gravity = cpSpaceGetGravity(m_handle);
|
|
return Vector2f(gravity.x, gravity.y);
|
|
}
|
|
|
|
cpSpace* PhysWorld2D::GetHandle() const
|
|
{
|
|
return m_handle;
|
|
}
|
|
|
|
float PhysWorld2D::GetStepSize() const
|
|
{
|
|
return m_stepSize;
|
|
}
|
|
|
|
void PhysWorld2D::SetGravity(const Vector2f& gravity)
|
|
{
|
|
cpSpaceSetGravity(m_handle, cpv(gravity.x, gravity.y));
|
|
}
|
|
|
|
void PhysWorld2D::SetStepSize(float stepSize)
|
|
{
|
|
m_stepSize = stepSize;
|
|
}
|
|
|
|
void PhysWorld2D::Step(float timestep)
|
|
{
|
|
m_timestepAccumulator += timestep;
|
|
|
|
while (m_timestepAccumulator >= m_stepSize)
|
|
{
|
|
cpSpaceStep(m_handle, m_stepSize);
|
|
m_timestepAccumulator -= m_stepSize;
|
|
}
|
|
}
|
|
}
|