Push the current work on the Physics2D module
This commit is contained in:
105
include/Nazara/Physics2D/Collider2D.hpp
Normal file
105
include/Nazara/Physics2D/Collider2D.hpp
Normal file
@@ -0,0 +1,105 @@
|
||||
// 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
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_COLLIDER2D_HPP
|
||||
#define NAZARA_COLLIDER2D_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/ObjectRef.hpp>
|
||||
#include <Nazara/Core/ObjectLibrary.hpp>
|
||||
#include <Nazara/Core/Signal.hpp>
|
||||
#include <Nazara/Math/Vector2.hpp>
|
||||
#include <Nazara/Physics2D/Config.hpp>
|
||||
#include <Nazara/Physics2D/Enums.hpp>
|
||||
#include <vector>
|
||||
|
||||
struct cpShape;
|
||||
struct cpSpace;
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class Collider2D;
|
||||
class RigidBody2D;
|
||||
|
||||
using Collider2DConstRef = ObjectRef<const Collider2D>;
|
||||
using Collider2DLibrary = ObjectLibrary<Collider2D>;
|
||||
using Collider2DRef = ObjectRef<Collider2D>;
|
||||
|
||||
class NAZARA_PHYSICS2D_API Collider2D : public RefCounted
|
||||
{
|
||||
friend Collider2DLibrary;
|
||||
friend RigidBody2D;
|
||||
|
||||
public:
|
||||
Collider2D() = default;
|
||||
Collider2D(const Collider2D&) = delete;
|
||||
Collider2D(Collider2D&&) = delete;
|
||||
virtual ~Collider2D();
|
||||
|
||||
virtual float ComputeInertialMatrix(float mass) const = 0;
|
||||
|
||||
virtual ColliderType2D GetType() const = 0;
|
||||
|
||||
Collider2D& operator=(const Collider2D&) = delete;
|
||||
Collider2D& operator=(Collider2D&&) = delete;
|
||||
|
||||
// Signals:
|
||||
NazaraSignal(OnColliderRelease, const Collider2D* /*collider*/);
|
||||
|
||||
protected:
|
||||
virtual std::vector<cpShape*> CreateShapes(RigidBody2D* body) const = 0;
|
||||
|
||||
static Collider2DLibrary::LibraryMap s_library;
|
||||
};
|
||||
|
||||
class CircleCollider2D;
|
||||
|
||||
using CircleCollider2DConstRef = ObjectRef<const CircleCollider2D>;
|
||||
using CircleCollider2DRef = ObjectRef<CircleCollider2D>;
|
||||
|
||||
class NAZARA_PHYSICS2D_API CircleCollider2D : public Collider2D
|
||||
{
|
||||
public:
|
||||
CircleCollider2D(float radius, const Vector2f& offset = Vector2f::Zero());
|
||||
|
||||
float ComputeInertialMatrix(float mass) const override;
|
||||
|
||||
inline float GetRadius() const;
|
||||
ColliderType2D GetType() const override;
|
||||
|
||||
template<typename... Args> static CircleCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::vector<cpShape*> CreateShapes(RigidBody2D* body) const override;
|
||||
|
||||
Vector2f m_offset;
|
||||
float m_radius;
|
||||
};
|
||||
|
||||
class NullCollider2D;
|
||||
|
||||
using NullCollider2DConstRef = ObjectRef<const NullCollider2D>;
|
||||
using NullCollider2DRef = ObjectRef<NullCollider2D>;
|
||||
|
||||
class NAZARA_PHYSICS2D_API NullCollider2D : public Collider2D
|
||||
{
|
||||
public:
|
||||
NullCollider2D() = default;
|
||||
|
||||
float ComputeInertialMatrix(float mass) const override;
|
||||
|
||||
ColliderType2D GetType() const override;
|
||||
|
||||
template<typename... Args> static NullCollider2DRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
std::vector<cpShape*> CreateShapes(RigidBody2D* body) const override;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Physics2D/Collider2D.inl>
|
||||
|
||||
#endif // NAZARA_COLLIDER2D_HPP
|
||||
35
include/Nazara/Physics2D/Collider2D.inl
Normal file
35
include/Nazara/Physics2D/Collider2D.inl
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Physics 3D module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <memory>
|
||||
#include <Nazara/Physics2D/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline float CircleCollider2D::GetRadius() const
|
||||
{
|
||||
return m_radius;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
CircleCollider2DRef CircleCollider2D::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<CircleCollider2D> object(new CircleCollider2D(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
NullCollider2DRef NullCollider2D::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<NullCollider2D> object(new NullCollider2D(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#include <Nazara/Physics2D/DebugOff.hpp>
|
||||
51
include/Nazara/Physics2D/Config.hpp
Normal file
51
include/Nazara/Physics2D/Config.hpp
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
Nazara Engine - Physics 3D module
|
||||
|
||||
Copyright (C) 2015 Jérôme "Lynix" Leclercq (Lynix680@gmail.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_CONFIG_PHYSICS2D_HPP
|
||||
#define NAZARA_CONFIG_PHYSICS2D_HPP
|
||||
|
||||
/// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci
|
||||
|
||||
// Utilise un manager de mémoire pour gérer les allocations dynamiques (détecte les leaks au prix d'allocations/libérations dynamiques plus lentes)
|
||||
#define NAZARA_PHYSICS2D_MANAGE_MEMORY 0
|
||||
|
||||
// Active les tests de sécurité basés sur le code (Conseillé pour le développement)
|
||||
#define NAZARA_PHYSICS2D_SAFE 1
|
||||
|
||||
/// Vérification des valeurs et types de certaines constantes
|
||||
#include <Nazara/Physics2D/ConfigCheck.hpp>
|
||||
|
||||
#if defined(NAZARA_STATIC)
|
||||
#define NAZARA_PHYSICS2D_API
|
||||
#else
|
||||
#ifdef NAZARA_PHYSICS2D_BUILD
|
||||
#define NAZARA_PHYSICS2D_API NAZARA_EXPORT
|
||||
#else
|
||||
#define NAZARA_PHYSICS2D_API NAZARA_IMPORT
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // NAZARA_CONFIG_PHYSICS3D_HPP
|
||||
18
include/Nazara/Physics2D/ConfigCheck.hpp
Normal file
18
include/Nazara/Physics2D/ConfigCheck.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Physics 3D module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_CONFIG_CHECK_PHYSICS_HPP
|
||||
#define NAZARA_CONFIG_CHECK_PHYSICS_HPP
|
||||
|
||||
/// Ce fichier sert à vérifier la valeur des constantes du fichier Config.hpp
|
||||
|
||||
// On force la valeur de MANAGE_MEMORY en mode debug
|
||||
#if defined(NAZARA_DEBUG) && !NAZARA_PHYSICS_MANAGE_MEMORY
|
||||
#undef NAZARA_PHYSICS_MANAGE_MEMORY
|
||||
#define NAZARA_PHYSICS3D_MANAGE_MEMORY 0
|
||||
#endif
|
||||
|
||||
#endif // NAZARA_CONFIG_CHECK_PHYSICS_HPP
|
||||
8
include/Nazara/Physics2D/Debug.hpp
Normal file
8
include/Nazara/Physics2D/Debug.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Physics 3D module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Physics3D/Config.hpp>
|
||||
#if NAZARA_PHYSICS_MANAGE_MEMORY
|
||||
#include <Nazara/Core/Debug/NewRedefinition.hpp>
|
||||
#endif
|
||||
9
include/Nazara/Physics2D/DebugOff.hpp
Normal file
9
include/Nazara/Physics2D/DebugOff.hpp
Normal file
@@ -0,0 +1,9 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Physics 3D module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
// On suppose que Debug.hpp a déjà été inclus, tout comme Config.hpp
|
||||
#if NAZARA_PHYSICS_MANAGE_MEMORY
|
||||
#undef delete
|
||||
#undef new
|
||||
#endif
|
||||
24
include/Nazara/Physics2D/Enums.hpp
Normal file
24
include/Nazara/Physics2D/Enums.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
// 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
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_ENUMS_PHYSICS2D_HPP
|
||||
#define NAZARA_ENUMS_PHYSICS2D_HPP
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
enum ColliderType2D
|
||||
{
|
||||
ColliderType2D_Box,
|
||||
ColliderType2D_Convex,
|
||||
ColliderType2D_Circle,
|
||||
ColliderType2D_Null,
|
||||
ColliderType2D_Segment,
|
||||
|
||||
ColliderType2D_Max = ColliderType2D_Segment
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_ENUMS_PHYSICS2D_HPP
|
||||
46
include/Nazara/Physics2D/PhysWorld2D.hpp
Normal file
46
include/Nazara/Physics2D/PhysWorld2D.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Physics 3D module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_PHYSWORLD2D_HPP
|
||||
#define NAZARA_PHYSWORLD2D_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Math/Vector2.hpp>
|
||||
#include <Nazara/Physics2D/Config.hpp>
|
||||
|
||||
struct cpSpace;
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_PHYSICS2D_API PhysWorld2D
|
||||
{
|
||||
public:
|
||||
PhysWorld2D();
|
||||
PhysWorld2D(const PhysWorld2D&) = delete;
|
||||
PhysWorld2D(PhysWorld2D&&) = delete; ///TODO
|
||||
~PhysWorld2D();
|
||||
|
||||
Vector2f GetGravity() const;
|
||||
cpSpace* GetHandle() const;
|
||||
float GetStepSize() const;
|
||||
|
||||
void SetGravity(const Vector2f& gravity);
|
||||
void SetSolverModel(unsigned int model);
|
||||
void SetStepSize(float stepSize);
|
||||
|
||||
void Step(float timestep);
|
||||
|
||||
PhysWorld2D& operator=(const PhysWorld2D&) = delete;
|
||||
PhysWorld2D& operator=(PhysWorld2D&&) = delete; ///TODO
|
||||
|
||||
private:
|
||||
cpSpace* m_handle;
|
||||
float m_stepSize;
|
||||
float m_timestepAccumulator;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_PHYSWORLD2D_HPP
|
||||
33
include/Nazara/Physics2D/Physics2D.hpp
Normal file
33
include/Nazara/Physics2D/Physics2D.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Physics 3D module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_PHYSICS2D_HPP
|
||||
#define NAZARA_PHYSICS2D_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Initializer.hpp>
|
||||
#include <Nazara/Physics2D/Config.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_PHYSICS2D_API Physics2D
|
||||
{
|
||||
public:
|
||||
Physics2D() = delete;
|
||||
~Physics2D() = delete;
|
||||
|
||||
static bool Initialize();
|
||||
|
||||
static bool IsInitialized();
|
||||
|
||||
static void Uninitialize();
|
||||
|
||||
private:
|
||||
static unsigned int s_moduleReferenceCounter;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_PHYSICS2D_HPP
|
||||
76
include/Nazara/Physics2D/RigidBody2D.hpp
Normal file
76
include/Nazara/Physics2D/RigidBody2D.hpp
Normal file
@@ -0,0 +1,76 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Physics 3D module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_RIGIDBODY2D_HPP
|
||||
#define NAZARA_RIGIDBODY2D_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Enums.hpp>
|
||||
#include <Nazara/Math/Matrix4.hpp>
|
||||
#include <Nazara/Math/Quaternion.hpp>
|
||||
#include <Nazara/Math/Rect.hpp>
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
#include <Nazara/Physics2D/Config.hpp>
|
||||
#include <Nazara/Physics2D/Collider2D.hpp>
|
||||
|
||||
struct cpBody;
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class PhysWorld2D;
|
||||
|
||||
class NAZARA_PHYSICS2D_API RigidBody2D
|
||||
{
|
||||
public:
|
||||
RigidBody2D(PhysWorld2D* world, float mass);
|
||||
RigidBody2D(PhysWorld2D* world, float mass, Collider2DRef geom);
|
||||
RigidBody2D(const RigidBody2D& object);
|
||||
RigidBody2D(RigidBody2D&& object);
|
||||
~RigidBody2D();
|
||||
|
||||
void AddForce(const Vector2f& force, CoordSys coordSys = CoordSys_Global);
|
||||
void AddForce(const Vector2f& force, const Vector2f& point, CoordSys coordSys = CoordSys_Global);
|
||||
void AddTorque(float torque);
|
||||
|
||||
Rectf GetAABB() const;
|
||||
float GetAngularVelocity() const;
|
||||
Vector2f GetCenterOfGravity(CoordSys coordSys = CoordSys_Local) const;
|
||||
const Collider2DRef& GetGeom() const;
|
||||
float GetGravityFactor() const;
|
||||
cpBody* GetHandle() const;
|
||||
float GetMass() const;
|
||||
Vector2f GetPosition() const;
|
||||
float GetRotation() const;
|
||||
Vector2f GetVelocity() const;
|
||||
|
||||
bool IsMoveable() const;
|
||||
bool IsSleeping() const;
|
||||
|
||||
void SetAngularVelocity(float angularVelocity);
|
||||
void SetGeom(Collider2DRef geom);
|
||||
void SetGravityFactor(float gravityFactor);
|
||||
void SetMass(float mass);
|
||||
void SetMassCenter(const Vector2f& center);
|
||||
void SetPosition(const Vector2f& position);
|
||||
void SetRotation(float rotation);
|
||||
void SetVelocity(const Vector2f& velocity);
|
||||
|
||||
RigidBody2D& operator=(const RigidBody2D& object);
|
||||
RigidBody2D& operator=(RigidBody2D&& object);
|
||||
|
||||
private:
|
||||
std::vector<cpShape*> m_shapes;
|
||||
Collider2DRef m_geom;
|
||||
Vector3f m_forceAccumulator;
|
||||
Vector3f m_torqueAccumulator;
|
||||
cpBody* m_handle;
|
||||
PhysWorld2D* m_world;
|
||||
float m_gravityFactor;
|
||||
float m_mass;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_RIGIDBODY3D_HPP
|
||||
Reference in New Issue
Block a user