Initial commit
This commit is contained in:
14
.gitignore
vendored
Normal file
14
.gitignore
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
project/.vs/
|
||||
project/_Temp/
|
||||
|
||||
.vs/
|
||||
out/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
23
CMakeLists.txt
Normal file
23
CMakeLists.txt
Normal file
@@ -0,0 +1,23 @@
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
|
||||
project(GGP_Exam VERSION 1.0.0)
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Release)
|
||||
endif()
|
||||
|
||||
if (MSVC)
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "/Od")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "/O2")
|
||||
else()
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "-g")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
|
||||
endif()
|
||||
|
||||
add_subdirectory(inc)
|
||||
add_subdirectory(lib)
|
||||
add_subdirectory(project)
|
||||
|
||||
message("Source file exe: ${EXAM_LIB_DEBUG}")
|
||||
32
CmakeSettings.json
Normal file
32
CmakeSettings.json
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "x86-Debug",
|
||||
"generator": "Visual Studio 17 2022",
|
||||
"configurationType": "Debug",
|
||||
"buildRoot": "${projectDir}\\out\\build\\${name}", // Project files go here
|
||||
"cmakeCommandArgs": "",
|
||||
"inheritEnvironments": [ "msvc_x86" ],
|
||||
"variables": [
|
||||
{
|
||||
"name": "CMAKE_GENERATOR_PLATFORM",
|
||||
"value": "Win32"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "x86-Release",
|
||||
"generator": "Visual Studio 17 2022",
|
||||
"configurationType": "Release",
|
||||
"buildRoot": "${projectDir}\\out\\build\\${name}", // Project files go here
|
||||
"cmakeCommandArgs": "",
|
||||
"inheritEnvironments": [ "msvc_x86" ],
|
||||
"variables": [
|
||||
{
|
||||
"name": "CMAKE_GENERATOR_PLATFORM",
|
||||
"value": "Win32"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
BIN
GPP_ZombieAI_LastNameFirstName_2DAEXX.pptx
Normal file
BIN
GPP_ZombieAI_LastNameFirstName_2DAEXX.pptx
Normal file
Binary file not shown.
49
Readme.md
Normal file
49
Readme.md
Normal file
@@ -0,0 +1,49 @@
|
||||
# Zombie Survival Game
|
||||
|
||||
## Description
|
||||
This part is all about displaying your mastery of the various topics covered throughout the semester. You will program the AI for a player agent in a zombie survival game provided to you by the teachers.
|
||||
|
||||
The agent has limited information about its surroundings. It has a rough knowledge about the world in general and the entities within its line of sight. It’s up to you to put this information to good use.
|
||||
|
||||
The goal of the agent is to survive for as long as possible and get a high score. Your success depends on your ability to collect and use the helpful items scattered throughout the world and the way you handle the threat of the roaming enemy zombies.
|
||||
|
||||
Use the tools given to you, the concepts you’ve seen during class and other topics you’ve researched on your own. Besides having a fully working agent you will also explain its architecture on your exam presentation. In this presentation you will explain to us how you’ve built your agent, what the agent is capable of and how well it performs.
|
||||
|
||||
## Stages & score
|
||||
The game consists of multiple stages, each more difficult than the last. Every stage lasts 60 seconds and has its own number and composition of enemies and items. Stronger enemies will become more likely to spawn in later stages and useful items less likely. Starting in stage 3, “Purge Zones” will start to spawn. These will kill everything within range after a delay. They add another layer of complexity to your exploration behavior, but possibly also opportunities to find areas without zombies!
|
||||
|
||||
Additionally, your agent will get a score based on a reward and penalty system. Both the score and the stage you’ve reached serve as an indicator for the quality of your AI and as a metric to compare and compete with your fellow students! There will be a high scores channel on the official Discord server to share your progress.
|
||||
|
||||
### Some scoring tips:
|
||||
Positive score is added for the following actions:
|
||||
|
||||
- Each second you survive: 1
|
||||
- Killing/Hitting zombies : 15 for each kill, 5 for each hit
|
||||
- Picking up items: 2
|
||||
|
||||
Negative score is given for:
|
||||
- Missing shots: -5 (a shotgun needs to miss all its shots to be counted as a miss)
|
||||
|
||||
## Project Requirements
|
||||
For the implementation of the AI agent you must use the following techniques:
|
||||
- At least one decision making structure (FSM, Behavior Tree, ...)
|
||||
- Movement logic (steering behaviors, combined steering behaviors, ...)
|
||||
|
||||
Students are permitted to use the framework implementations of the above techniques.
|
||||
|
||||
Any custom implementations or extra’s will be rewarded. A custom technique could count as a research topic for the other part of the exam.
|
||||
|
||||
## Evaluation criteria
|
||||
- Agent
|
||||
- World exploration
|
||||
- Movement *(steering behaviors, combined steering)*
|
||||
- Item handling
|
||||
- Enemy handling
|
||||
- Decision making
|
||||
- Extra(s)
|
||||
- Code quality & structure
|
||||
- Knowledge / insight:
|
||||
- Answering questions about the written code and the topics covered in class
|
||||
- GitHub Usage
|
||||
- Regular commits
|
||||
- Commit messages
|
||||
BIN
_DEMO_DEBUG/GPP_EXAM_DEBUG.exe
Normal file
BIN
_DEMO_DEBUG/GPP_EXAM_DEBUG.exe
Normal file
Binary file not shown.
BIN
_DEMO_DEBUG/GPP_EXAM_DEBUG.pdb
Normal file
BIN
_DEMO_DEBUG/GPP_EXAM_DEBUG.pdb
Normal file
Binary file not shown.
BIN
_DEMO_DEBUG/GPP_Plugin_d.dll
Normal file
BIN
_DEMO_DEBUG/GPP_Plugin_d.dll
Normal file
Binary file not shown.
BIN
_DEMO_DEBUG/GPP_Plugin_d.exp
Normal file
BIN
_DEMO_DEBUG/GPP_Plugin_d.exp
Normal file
Binary file not shown.
BIN
_DEMO_DEBUG/GPP_Plugin_d.lib
Normal file
BIN
_DEMO_DEBUG/GPP_Plugin_d.lib
Normal file
Binary file not shown.
BIN
_DEMO_DEBUG/GPP_Plugin_d.pdb
Normal file
BIN
_DEMO_DEBUG/GPP_Plugin_d.pdb
Normal file
Binary file not shown.
BIN
_DEMO_DEBUG/GameLevel.gppl
Normal file
BIN
_DEMO_DEBUG/GameLevel.gppl
Normal file
Binary file not shown.
BIN
_DEMO_DEBUG/SDL2.dll
Normal file
BIN
_DEMO_DEBUG/SDL2.dll
Normal file
Binary file not shown.
1710
_DEMO_DEBUG/imgui.ini
Normal file
1710
_DEMO_DEBUG/imgui.ini
Normal file
File diff suppressed because it is too large
Load Diff
9
_DEMO_RELEASE/4WindowExamRunner.bat
Normal file
9
_DEMO_RELEASE/4WindowExamRunner.bat
Normal file
@@ -0,0 +1,9 @@
|
||||
start GPP_EXAM_RELEASE.exe -x 30 -y 30 -s 1
|
||||
|
||||
start GPP_EXAM_RELEASE.exe -x 1000 -y 30 -s 1
|
||||
|
||||
start GPP_EXAM_RELEASE.exe -x 30 -y 600 -s 1
|
||||
|
||||
start GPP_EXAM_RELEASE.exe -x 1000 -y 600 -s 1
|
||||
echo new ActiveXObject("WScript.Shell").AppActivate("GPP_TEST_RELEASE.exe"); > tmp.js
|
||||
cscript //nologo tmp.js & del tmp.js
|
||||
BIN
_DEMO_RELEASE/GPP_EXAM_RELEASE.exe
Normal file
BIN
_DEMO_RELEASE/GPP_EXAM_RELEASE.exe
Normal file
Binary file not shown.
BIN
_DEMO_RELEASE/GPP_EXAM_RELEASE.pdb
Normal file
BIN
_DEMO_RELEASE/GPP_EXAM_RELEASE.pdb
Normal file
Binary file not shown.
BIN
_DEMO_RELEASE/GPP_Framework.pdb
Normal file
BIN
_DEMO_RELEASE/GPP_Framework.pdb
Normal file
Binary file not shown.
BIN
_DEMO_RELEASE/GPP_Plugin.dll
Normal file
BIN
_DEMO_RELEASE/GPP_Plugin.dll
Normal file
Binary file not shown.
BIN
_DEMO_RELEASE/GameLevel.gppl
Normal file
BIN
_DEMO_RELEASE/GameLevel.gppl
Normal file
Binary file not shown.
BIN
_DEMO_RELEASE/SDL2.dll
Normal file
BIN
_DEMO_RELEASE/SDL2.dll
Normal file
Binary file not shown.
4160
_DEMO_RELEASE/imgui.ini
Normal file
4160
_DEMO_RELEASE/imgui.ini
Normal file
File diff suppressed because it is too large
Load Diff
68
inc/Box2D/Box2D.h
Normal file
68
inc/Box2D/Box2D.h
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef BOX2D_H
|
||||
#define BOX2D_H
|
||||
|
||||
/**
|
||||
\mainpage Box2D API Documentation
|
||||
|
||||
\section intro_sec Getting Started
|
||||
|
||||
For documentation please see http://box2d.org/documentation.html
|
||||
|
||||
For discussion please visit http://box2d.org/forum
|
||||
*/
|
||||
|
||||
// These include files constitute the main Box2D API
|
||||
|
||||
#include <Box2D/Common/b2Settings.h>
|
||||
#include <Box2D/Common/b2Draw.h>
|
||||
#include <Box2D/Common/b2Timer.h>
|
||||
|
||||
#include <Box2D/Collision/Shapes/b2CircleShape.h>
|
||||
#include <Box2D/Collision/Shapes/b2EdgeShape.h>
|
||||
#include <Box2D/Collision/Shapes/b2ChainShape.h>
|
||||
#include <Box2D/Collision/Shapes/b2PolygonShape.h>
|
||||
|
||||
#include <Box2D/Collision/b2BroadPhase.h>
|
||||
#include <Box2D/Collision/b2Distance.h>
|
||||
#include <Box2D/Collision/b2DynamicTree.h>
|
||||
#include <Box2D/Collision/b2TimeOfImpact.h>
|
||||
|
||||
#include <Box2D/Dynamics/b2Body.h>
|
||||
#include <Box2D/Dynamics/b2Fixture.h>
|
||||
#include <Box2D/Dynamics/b2WorldCallbacks.h>
|
||||
#include <Box2D/Dynamics/b2TimeStep.h>
|
||||
#include <Box2D/Dynamics/b2World.h>
|
||||
|
||||
#include <Box2D/Dynamics/Contacts/b2Contact.h>
|
||||
|
||||
#include <Box2D/Dynamics/Joints/b2DistanceJoint.h>
|
||||
#include <Box2D/Dynamics/Joints/b2FrictionJoint.h>
|
||||
#include <Box2D/Dynamics/Joints/b2GearJoint.h>
|
||||
#include <Box2D/Dynamics/Joints/b2MotorJoint.h>
|
||||
#include <Box2D/Dynamics/Joints/b2MouseJoint.h>
|
||||
#include <Box2D/Dynamics/Joints/b2PrismaticJoint.h>
|
||||
#include <Box2D/Dynamics/Joints/b2PulleyJoint.h>
|
||||
#include <Box2D/Dynamics/Joints/b2RevoluteJoint.h>
|
||||
#include <Box2D/Dynamics/Joints/b2RopeJoint.h>
|
||||
#include <Box2D/Dynamics/Joints/b2WeldJoint.h>
|
||||
#include <Box2D/Dynamics/Joints/b2WheelJoint.h>
|
||||
|
||||
#endif
|
||||
105
inc/Box2D/Collision/Shapes/b2ChainShape.h
Normal file
105
inc/Box2D/Collision/Shapes/b2ChainShape.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2010 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_CHAIN_SHAPE_H
|
||||
#define B2_CHAIN_SHAPE_H
|
||||
|
||||
#include <Box2D/Collision/Shapes/b2Shape.h>
|
||||
|
||||
class b2EdgeShape;
|
||||
|
||||
/// A chain shape is a free form sequence of line segments.
|
||||
/// The chain has two-sided collision, so you can use inside and outside collision.
|
||||
/// Therefore, you may use any winding order.
|
||||
/// Since there may be many vertices, they are allocated using b2Alloc.
|
||||
/// Connectivity information is used to create smooth collisions.
|
||||
/// WARNING: The chain will not collide properly if there are self-intersections.
|
||||
class b2ChainShape : public b2Shape
|
||||
{
|
||||
public:
|
||||
b2ChainShape();
|
||||
|
||||
/// The destructor frees the vertices using b2Free.
|
||||
~b2ChainShape();
|
||||
|
||||
/// Clear all data.
|
||||
void Clear();
|
||||
|
||||
/// Create a loop. This automatically adjusts connectivity.
|
||||
/// @param vertices an array of vertices, these are copied
|
||||
/// @param count the vertex count
|
||||
void CreateLoop(const b2Vec2* vertices, int32 count);
|
||||
|
||||
/// Create a chain with isolated end vertices.
|
||||
/// @param vertices an array of vertices, these are copied
|
||||
/// @param count the vertex count
|
||||
void CreateChain(const b2Vec2* vertices, int32 count);
|
||||
|
||||
/// Establish connectivity to a vertex that precedes the first vertex.
|
||||
/// Don't call this for loops.
|
||||
void SetPrevVertex(const b2Vec2& prevVertex);
|
||||
|
||||
/// Establish connectivity to a vertex that follows the last vertex.
|
||||
/// Don't call this for loops.
|
||||
void SetNextVertex(const b2Vec2& nextVertex);
|
||||
|
||||
/// Implement b2Shape. Vertices are cloned using b2Alloc.
|
||||
b2Shape* Clone(b2BlockAllocator* allocator) const;
|
||||
|
||||
/// @see b2Shape::GetChildCount
|
||||
int32 GetChildCount() const;
|
||||
|
||||
/// Get a child edge.
|
||||
void GetChildEdge(b2EdgeShape* edge, int32 index) const;
|
||||
|
||||
/// This always return false.
|
||||
/// @see b2Shape::TestPoint
|
||||
bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
|
||||
|
||||
/// Implement b2Shape.
|
||||
bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
|
||||
const b2Transform& transform, int32 childIndex) const;
|
||||
|
||||
/// @see b2Shape::ComputeAABB
|
||||
void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const;
|
||||
|
||||
/// Chains have zero mass.
|
||||
/// @see b2Shape::ComputeMass
|
||||
void ComputeMass(b2MassData* massData, float32 density) const;
|
||||
|
||||
/// The vertices. Owned by this class.
|
||||
b2Vec2* m_vertices;
|
||||
|
||||
/// The vertex count.
|
||||
int32 m_count;
|
||||
|
||||
b2Vec2 m_prevVertex, m_nextVertex;
|
||||
bool m_hasPrevVertex, m_hasNextVertex;
|
||||
};
|
||||
|
||||
inline b2ChainShape::b2ChainShape()
|
||||
{
|
||||
m_type = e_chain;
|
||||
m_radius = b2_polygonRadius;
|
||||
m_vertices = NULL;
|
||||
m_count = 0;
|
||||
m_hasPrevVertex = false;
|
||||
m_hasNextVertex = false;
|
||||
}
|
||||
|
||||
#endif
|
||||
91
inc/Box2D/Collision/Shapes/b2CircleShape.h
Normal file
91
inc/Box2D/Collision/Shapes/b2CircleShape.h
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_CIRCLE_SHAPE_H
|
||||
#define B2_CIRCLE_SHAPE_H
|
||||
|
||||
#include <Box2D/Collision/Shapes/b2Shape.h>
|
||||
|
||||
/// A circle shape.
|
||||
class b2CircleShape : public b2Shape
|
||||
{
|
||||
public:
|
||||
b2CircleShape();
|
||||
|
||||
/// Implement b2Shape.
|
||||
b2Shape* Clone(b2BlockAllocator* allocator) const;
|
||||
|
||||
/// @see b2Shape::GetChildCount
|
||||
int32 GetChildCount() const;
|
||||
|
||||
/// Implement b2Shape.
|
||||
bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
|
||||
|
||||
/// Implement b2Shape.
|
||||
bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
|
||||
const b2Transform& transform, int32 childIndex) const;
|
||||
|
||||
/// @see b2Shape::ComputeAABB
|
||||
void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const;
|
||||
|
||||
/// @see b2Shape::ComputeMass
|
||||
void ComputeMass(b2MassData* massData, float32 density) const;
|
||||
|
||||
/// Get the supporting vertex index in the given direction.
|
||||
int32 GetSupport(const b2Vec2& d) const;
|
||||
|
||||
/// Get the supporting vertex in the given direction.
|
||||
const b2Vec2& GetSupportVertex(const b2Vec2& d) const;
|
||||
|
||||
/// Get the vertex count.
|
||||
int32 GetVertexCount() const { return 1; }
|
||||
|
||||
/// Get a vertex by index. Used by b2Distance.
|
||||
const b2Vec2& GetVertex(int32 index) const;
|
||||
|
||||
/// Position
|
||||
b2Vec2 m_p;
|
||||
};
|
||||
|
||||
inline b2CircleShape::b2CircleShape()
|
||||
{
|
||||
m_type = e_circle;
|
||||
m_radius = 0.0f;
|
||||
m_p.SetZero();
|
||||
}
|
||||
|
||||
inline int32 b2CircleShape::GetSupport(const b2Vec2 &d) const
|
||||
{
|
||||
B2_NOT_USED(d);
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline const b2Vec2& b2CircleShape::GetSupportVertex(const b2Vec2 &d) const
|
||||
{
|
||||
B2_NOT_USED(d);
|
||||
return m_p;
|
||||
}
|
||||
|
||||
inline const b2Vec2& b2CircleShape::GetVertex(int32 index) const
|
||||
{
|
||||
B2_NOT_USED(index);
|
||||
b2Assert(index == 0);
|
||||
return m_p;
|
||||
}
|
||||
|
||||
#endif
|
||||
74
inc/Box2D/Collision/Shapes/b2EdgeShape.h
Normal file
74
inc/Box2D/Collision/Shapes/b2EdgeShape.h
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2010 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_EDGE_SHAPE_H
|
||||
#define B2_EDGE_SHAPE_H
|
||||
|
||||
#include <Box2D/Collision/Shapes/b2Shape.h>
|
||||
|
||||
/// A line segment (edge) shape. These can be connected in chains or loops
|
||||
/// to other edge shapes. The connectivity information is used to ensure
|
||||
/// correct contact normals.
|
||||
class b2EdgeShape : public b2Shape
|
||||
{
|
||||
public:
|
||||
b2EdgeShape();
|
||||
|
||||
/// Set this as an isolated edge.
|
||||
void Set(const b2Vec2& v1, const b2Vec2& v2);
|
||||
|
||||
/// Implement b2Shape.
|
||||
b2Shape* Clone(b2BlockAllocator* allocator) const;
|
||||
|
||||
/// @see b2Shape::GetChildCount
|
||||
int32 GetChildCount() const;
|
||||
|
||||
/// @see b2Shape::TestPoint
|
||||
bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
|
||||
|
||||
/// Implement b2Shape.
|
||||
bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
|
||||
const b2Transform& transform, int32 childIndex) const;
|
||||
|
||||
/// @see b2Shape::ComputeAABB
|
||||
void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const;
|
||||
|
||||
/// @see b2Shape::ComputeMass
|
||||
void ComputeMass(b2MassData* massData, float32 density) const;
|
||||
|
||||
/// These are the edge vertices
|
||||
b2Vec2 m_vertex1, m_vertex2;
|
||||
|
||||
/// Optional adjacent vertices. These are used for smooth collision.
|
||||
b2Vec2 m_vertex0, m_vertex3;
|
||||
bool m_hasVertex0, m_hasVertex3;
|
||||
};
|
||||
|
||||
inline b2EdgeShape::b2EdgeShape()
|
||||
{
|
||||
m_type = e_edge;
|
||||
m_radius = b2_polygonRadius;
|
||||
m_vertex0.x = 0.0f;
|
||||
m_vertex0.y = 0.0f;
|
||||
m_vertex3.x = 0.0f;
|
||||
m_vertex3.y = 0.0f;
|
||||
m_hasVertex0 = false;
|
||||
m_hasVertex3 = false;
|
||||
}
|
||||
|
||||
#endif
|
||||
101
inc/Box2D/Collision/Shapes/b2PolygonShape.h
Normal file
101
inc/Box2D/Collision/Shapes/b2PolygonShape.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_POLYGON_SHAPE_H
|
||||
#define B2_POLYGON_SHAPE_H
|
||||
|
||||
#include <Box2D/Collision/Shapes/b2Shape.h>
|
||||
|
||||
/// A convex polygon. It is assumed that the interior of the polygon is to
|
||||
/// the left of each edge.
|
||||
/// Polygons have a maximum number of vertices equal to b2_maxPolygonVertices.
|
||||
/// In most cases you should not need many vertices for a convex polygon.
|
||||
class b2PolygonShape : public b2Shape
|
||||
{
|
||||
public:
|
||||
b2PolygonShape();
|
||||
|
||||
/// Implement b2Shape.
|
||||
b2Shape* Clone(b2BlockAllocator* allocator) const;
|
||||
|
||||
/// @see b2Shape::GetChildCount
|
||||
int32 GetChildCount() const;
|
||||
|
||||
/// Create a convex hull from the given array of local points.
|
||||
/// The count must be in the range [3, b2_maxPolygonVertices].
|
||||
/// @warning the points may be re-ordered, even if they form a convex polygon
|
||||
/// @warning collinear points are handled but not removed. Collinear points
|
||||
/// may lead to poor stacking behavior.
|
||||
void Set(const b2Vec2* points, int32 count);
|
||||
|
||||
/// Build vertices to represent an axis-aligned box centered on the local origin.
|
||||
/// @param hx the half-width.
|
||||
/// @param hy the half-height.
|
||||
void SetAsBox(float32 hx, float32 hy);
|
||||
|
||||
/// Build vertices to represent an oriented box.
|
||||
/// @param hx the half-width.
|
||||
/// @param hy the half-height.
|
||||
/// @param center the center of the box in local coordinates.
|
||||
/// @param angle the rotation of the box in local coordinates.
|
||||
void SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle);
|
||||
|
||||
/// @see b2Shape::TestPoint
|
||||
bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
|
||||
|
||||
/// Implement b2Shape.
|
||||
bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
|
||||
const b2Transform& transform, int32 childIndex) const;
|
||||
|
||||
/// @see b2Shape::ComputeAABB
|
||||
void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const;
|
||||
|
||||
/// @see b2Shape::ComputeMass
|
||||
void ComputeMass(b2MassData* massData, float32 density) const;
|
||||
|
||||
/// Get the vertex count.
|
||||
int32 GetVertexCount() const { return m_count; }
|
||||
|
||||
/// Get a vertex by index.
|
||||
const b2Vec2& GetVertex(int32 index) const;
|
||||
|
||||
/// Validate convexity. This is a very time consuming operation.
|
||||
/// @returns true if valid
|
||||
bool Validate() const;
|
||||
|
||||
b2Vec2 m_centroid;
|
||||
b2Vec2 m_vertices[b2_maxPolygonVertices];
|
||||
b2Vec2 m_normals[b2_maxPolygonVertices];
|
||||
int32 m_count;
|
||||
};
|
||||
|
||||
inline b2PolygonShape::b2PolygonShape()
|
||||
{
|
||||
m_type = e_polygon;
|
||||
m_radius = b2_polygonRadius;
|
||||
m_count = 0;
|
||||
m_centroid.SetZero();
|
||||
}
|
||||
|
||||
inline const b2Vec2& b2PolygonShape::GetVertex(int32 index) const
|
||||
{
|
||||
b2Assert(0 <= index && index < m_count);
|
||||
return m_vertices[index];
|
||||
}
|
||||
|
||||
#endif
|
||||
101
inc/Box2D/Collision/Shapes/b2Shape.h
Normal file
101
inc/Box2D/Collision/Shapes/b2Shape.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_SHAPE_H
|
||||
#define B2_SHAPE_H
|
||||
|
||||
#include <Box2D/Common/b2BlockAllocator.h>
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
#include <Box2D/Collision/b2Collision.h>
|
||||
|
||||
/// This holds the mass data computed for a shape.
|
||||
struct b2MassData
|
||||
{
|
||||
/// The mass of the shape, usually in kilograms.
|
||||
float32 mass;
|
||||
|
||||
/// The position of the shape's centroid relative to the shape's origin.
|
||||
b2Vec2 center;
|
||||
|
||||
/// The rotational inertia of the shape about the local origin.
|
||||
float32 I;
|
||||
};
|
||||
|
||||
/// A shape is used for collision detection. You can create a shape however you like.
|
||||
/// Shapes used for simulation in b2World are created automatically when a b2Fixture
|
||||
/// is created. Shapes may encapsulate a one or more child shapes.
|
||||
class b2Shape
|
||||
{
|
||||
public:
|
||||
|
||||
enum Type
|
||||
{
|
||||
e_circle = 0,
|
||||
e_edge = 1,
|
||||
e_polygon = 2,
|
||||
e_chain = 3,
|
||||
e_typeCount = 4
|
||||
};
|
||||
|
||||
virtual ~b2Shape() {}
|
||||
|
||||
/// Clone the concrete shape using the provided allocator.
|
||||
virtual b2Shape* Clone(b2BlockAllocator* allocator) const = 0;
|
||||
|
||||
/// Get the type of this shape. You can use this to down cast to the concrete shape.
|
||||
/// @return the shape type.
|
||||
Type GetType() const;
|
||||
|
||||
/// Get the number of child primitives.
|
||||
virtual int32 GetChildCount() const = 0;
|
||||
|
||||
/// Test a point for containment in this shape. This only works for convex shapes.
|
||||
/// @param xf the shape world transform.
|
||||
/// @param p a point in world coordinates.
|
||||
virtual bool TestPoint(const b2Transform& xf, const b2Vec2& p) const = 0;
|
||||
|
||||
/// Cast a ray against a child shape.
|
||||
/// @param output the ray-cast results.
|
||||
/// @param input the ray-cast input parameters.
|
||||
/// @param transform the transform to be applied to the shape.
|
||||
/// @param childIndex the child shape index
|
||||
virtual bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
|
||||
const b2Transform& transform, int32 childIndex) const = 0;
|
||||
|
||||
/// Given a transform, compute the associated axis aligned bounding box for a child shape.
|
||||
/// @param aabb returns the axis aligned box.
|
||||
/// @param xf the world transform of the shape.
|
||||
/// @param childIndex the child shape
|
||||
virtual void ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const = 0;
|
||||
|
||||
/// Compute the mass properties of this shape using its dimensions and density.
|
||||
/// The inertia tensor is computed about the local origin.
|
||||
/// @param massData returns the mass data for this shape.
|
||||
/// @param density the density in kilograms per meter squared.
|
||||
virtual void ComputeMass(b2MassData* massData, float32 density) const = 0;
|
||||
|
||||
Type m_type;
|
||||
float32 m_radius;
|
||||
};
|
||||
|
||||
inline b2Shape::Type b2Shape::GetType() const
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
#endif
|
||||
257
inc/Box2D/Collision/b2BroadPhase.h
Normal file
257
inc/Box2D/Collision/b2BroadPhase.h
Normal file
@@ -0,0 +1,257 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_BROAD_PHASE_H
|
||||
#define B2_BROAD_PHASE_H
|
||||
|
||||
#include <Box2D/Common/b2Settings.h>
|
||||
#include <Box2D/Collision/b2Collision.h>
|
||||
#include <Box2D/Collision/b2DynamicTree.h>
|
||||
#include <algorithm>
|
||||
|
||||
struct b2Pair
|
||||
{
|
||||
int32 proxyIdA;
|
||||
int32 proxyIdB;
|
||||
};
|
||||
|
||||
/// The broad-phase is used for computing pairs and performing volume queries and ray casts.
|
||||
/// This broad-phase does not persist pairs. Instead, this reports potentially new pairs.
|
||||
/// It is up to the client to consume the new pairs and to track subsequent overlap.
|
||||
class b2BroadPhase
|
||||
{
|
||||
public:
|
||||
|
||||
enum
|
||||
{
|
||||
e_nullProxy = -1
|
||||
};
|
||||
|
||||
b2BroadPhase();
|
||||
~b2BroadPhase();
|
||||
|
||||
/// Create a proxy with an initial AABB. Pairs are not reported until
|
||||
/// UpdatePairs is called.
|
||||
int32 CreateProxy(const b2AABB& aabb, void* userData);
|
||||
|
||||
/// Destroy a proxy. It is up to the client to remove any pairs.
|
||||
void DestroyProxy(int32 proxyId);
|
||||
|
||||
/// Call MoveProxy as many times as you like, then when you are done
|
||||
/// call UpdatePairs to finalized the proxy pairs (for your time step).
|
||||
void MoveProxy(int32 proxyId, const b2AABB& aabb, const b2Vec2& displacement);
|
||||
|
||||
/// Call to trigger a re-processing of it's pairs on the next call to UpdatePairs.
|
||||
void TouchProxy(int32 proxyId);
|
||||
|
||||
/// Get the fat AABB for a proxy.
|
||||
const b2AABB& GetFatAABB(int32 proxyId) const;
|
||||
|
||||
/// Get user data from a proxy. Returns NULL if the id is invalid.
|
||||
void* GetUserData(int32 proxyId) const;
|
||||
|
||||
/// Test overlap of fat AABBs.
|
||||
bool TestOverlap(int32 proxyIdA, int32 proxyIdB) const;
|
||||
|
||||
/// Get the number of proxies.
|
||||
int32 GetProxyCount() const;
|
||||
|
||||
/// Update the pairs. This results in pair callbacks. This can only add pairs.
|
||||
template <typename T>
|
||||
void UpdatePairs(T* callback);
|
||||
|
||||
/// Query an AABB for overlapping proxies. The callback class
|
||||
/// is called for each proxy that overlaps the supplied AABB.
|
||||
template <typename T>
|
||||
void Query(T* callback, const b2AABB& aabb) const;
|
||||
|
||||
/// Ray-cast against the proxies in the tree. This relies on the callback
|
||||
/// to perform a exact ray-cast in the case were the proxy contains a shape.
|
||||
/// The callback also performs the any collision filtering. This has performance
|
||||
/// roughly equal to k * log(n), where k is the number of collisions and n is the
|
||||
/// number of proxies in the tree.
|
||||
/// @param input the ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
|
||||
/// @param callback a callback class that is called for each proxy that is hit by the ray.
|
||||
template <typename T>
|
||||
void RayCast(T* callback, const b2RayCastInput& input) const;
|
||||
|
||||
/// Get the height of the embedded tree.
|
||||
int32 GetTreeHeight() const;
|
||||
|
||||
/// Get the balance of the embedded tree.
|
||||
int32 GetTreeBalance() const;
|
||||
|
||||
/// Get the quality metric of the embedded tree.
|
||||
float32 GetTreeQuality() const;
|
||||
|
||||
/// Shift the world origin. Useful for large worlds.
|
||||
/// The shift formula is: position -= newOrigin
|
||||
/// @param newOrigin the new origin with respect to the old origin
|
||||
void ShiftOrigin(const b2Vec2& newOrigin);
|
||||
|
||||
private:
|
||||
|
||||
friend class b2DynamicTree;
|
||||
|
||||
void BufferMove(int32 proxyId);
|
||||
void UnBufferMove(int32 proxyId);
|
||||
|
||||
bool QueryCallback(int32 proxyId);
|
||||
|
||||
b2DynamicTree m_tree;
|
||||
|
||||
int32 m_proxyCount;
|
||||
|
||||
int32* m_moveBuffer;
|
||||
int32 m_moveCapacity;
|
||||
int32 m_moveCount;
|
||||
|
||||
b2Pair* m_pairBuffer;
|
||||
int32 m_pairCapacity;
|
||||
int32 m_pairCount;
|
||||
|
||||
int32 m_queryProxyId;
|
||||
};
|
||||
|
||||
/// This is used to sort pairs.
|
||||
inline bool b2PairLessThan(const b2Pair& pair1, const b2Pair& pair2)
|
||||
{
|
||||
if (pair1.proxyIdA < pair2.proxyIdA)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (pair1.proxyIdA == pair2.proxyIdA)
|
||||
{
|
||||
return pair1.proxyIdB < pair2.proxyIdB;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
inline void* b2BroadPhase::GetUserData(int32 proxyId) const
|
||||
{
|
||||
return m_tree.GetUserData(proxyId);
|
||||
}
|
||||
|
||||
inline bool b2BroadPhase::TestOverlap(int32 proxyIdA, int32 proxyIdB) const
|
||||
{
|
||||
const b2AABB& aabbA = m_tree.GetFatAABB(proxyIdA);
|
||||
const b2AABB& aabbB = m_tree.GetFatAABB(proxyIdB);
|
||||
return b2TestOverlap(aabbA, aabbB);
|
||||
}
|
||||
|
||||
inline const b2AABB& b2BroadPhase::GetFatAABB(int32 proxyId) const
|
||||
{
|
||||
return m_tree.GetFatAABB(proxyId);
|
||||
}
|
||||
|
||||
inline int32 b2BroadPhase::GetProxyCount() const
|
||||
{
|
||||
return m_proxyCount;
|
||||
}
|
||||
|
||||
inline int32 b2BroadPhase::GetTreeHeight() const
|
||||
{
|
||||
return m_tree.GetHeight();
|
||||
}
|
||||
|
||||
inline int32 b2BroadPhase::GetTreeBalance() const
|
||||
{
|
||||
return m_tree.GetMaxBalance();
|
||||
}
|
||||
|
||||
inline float32 b2BroadPhase::GetTreeQuality() const
|
||||
{
|
||||
return m_tree.GetAreaRatio();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void b2BroadPhase::UpdatePairs(T* callback)
|
||||
{
|
||||
// Reset pair buffer
|
||||
m_pairCount = 0;
|
||||
|
||||
// Perform tree queries for all moving proxies.
|
||||
for (int32 i = 0; i < m_moveCount; ++i)
|
||||
{
|
||||
m_queryProxyId = m_moveBuffer[i];
|
||||
if (m_queryProxyId == e_nullProxy)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// We have to query the tree with the fat AABB so that
|
||||
// we don't fail to create a pair that may touch later.
|
||||
const b2AABB& fatAABB = m_tree.GetFatAABB(m_queryProxyId);
|
||||
|
||||
// Query tree, create pairs and add them pair buffer.
|
||||
m_tree.Query(this, fatAABB);
|
||||
}
|
||||
|
||||
// Reset move buffer
|
||||
m_moveCount = 0;
|
||||
|
||||
// Sort the pair buffer to expose duplicates.
|
||||
std::sort(m_pairBuffer, m_pairBuffer + m_pairCount, b2PairLessThan);
|
||||
|
||||
// Send the pairs back to the client.
|
||||
int32 i = 0;
|
||||
while (i < m_pairCount)
|
||||
{
|
||||
b2Pair* primaryPair = m_pairBuffer + i;
|
||||
void* userDataA = m_tree.GetUserData(primaryPair->proxyIdA);
|
||||
void* userDataB = m_tree.GetUserData(primaryPair->proxyIdB);
|
||||
|
||||
callback->AddPair(userDataA, userDataB);
|
||||
++i;
|
||||
|
||||
// Skip any duplicate pairs.
|
||||
while (i < m_pairCount)
|
||||
{
|
||||
b2Pair* pair = m_pairBuffer + i;
|
||||
if (pair->proxyIdA != primaryPair->proxyIdA || pair->proxyIdB != primaryPair->proxyIdB)
|
||||
{
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to keep the tree balanced.
|
||||
//m_tree.Rebalance(4);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void b2BroadPhase::Query(T* callback, const b2AABB& aabb) const
|
||||
{
|
||||
m_tree.Query(callback, aabb);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void b2BroadPhase::RayCast(T* callback, const b2RayCastInput& input) const
|
||||
{
|
||||
m_tree.RayCast(callback, input);
|
||||
}
|
||||
|
||||
inline void b2BroadPhase::ShiftOrigin(const b2Vec2& newOrigin)
|
||||
{
|
||||
m_tree.ShiftOrigin(newOrigin);
|
||||
}
|
||||
|
||||
#endif
|
||||
277
inc/Box2D/Collision/b2Collision.h
Normal file
277
inc/Box2D/Collision/b2Collision.h
Normal file
@@ -0,0 +1,277 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_COLLISION_H
|
||||
#define B2_COLLISION_H
|
||||
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
#include <limits.h>
|
||||
|
||||
/// @file
|
||||
/// Structures and functions used for computing contact points, distance
|
||||
/// queries, and TOI queries.
|
||||
|
||||
class b2Shape;
|
||||
class b2CircleShape;
|
||||
class b2EdgeShape;
|
||||
class b2PolygonShape;
|
||||
|
||||
const uint8 b2_nullFeature = UCHAR_MAX;
|
||||
|
||||
/// The features that intersect to form the contact point
|
||||
/// This must be 4 bytes or less.
|
||||
struct b2ContactFeature
|
||||
{
|
||||
enum Type
|
||||
{
|
||||
e_vertex = 0,
|
||||
e_face = 1
|
||||
};
|
||||
|
||||
uint8 indexA; ///< Feature index on shapeA
|
||||
uint8 indexB; ///< Feature index on shapeB
|
||||
uint8 typeA; ///< The feature type on shapeA
|
||||
uint8 typeB; ///< The feature type on shapeB
|
||||
};
|
||||
|
||||
/// Contact ids to facilitate warm starting.
|
||||
union b2ContactID
|
||||
{
|
||||
b2ContactFeature cf;
|
||||
uint32 key; ///< Used to quickly compare contact ids.
|
||||
};
|
||||
|
||||
/// A manifold point is a contact point belonging to a contact
|
||||
/// manifold. It holds details related to the geometry and dynamics
|
||||
/// of the contact points.
|
||||
/// The local point usage depends on the manifold type:
|
||||
/// -e_circles: the local center of circleB
|
||||
/// -e_faceA: the local center of cirlceB or the clip point of polygonB
|
||||
/// -e_faceB: the clip point of polygonA
|
||||
/// This structure is stored across time steps, so we keep it small.
|
||||
/// Note: the impulses are used for internal caching and may not
|
||||
/// provide reliable contact forces, especially for high speed collisions.
|
||||
struct b2ManifoldPoint
|
||||
{
|
||||
b2Vec2 localPoint; ///< usage depends on manifold type
|
||||
float32 normalImpulse; ///< the non-penetration impulse
|
||||
float32 tangentImpulse; ///< the friction impulse
|
||||
b2ContactID id; ///< uniquely identifies a contact point between two shapes
|
||||
};
|
||||
|
||||
/// A manifold for two touching convex shapes.
|
||||
/// Box2D supports multiple types of contact:
|
||||
/// - clip point versus plane with radius
|
||||
/// - point versus point with radius (circles)
|
||||
/// The local point usage depends on the manifold type:
|
||||
/// -e_circles: the local center of circleA
|
||||
/// -e_faceA: the center of faceA
|
||||
/// -e_faceB: the center of faceB
|
||||
/// Similarly the local normal usage:
|
||||
/// -e_circles: not used
|
||||
/// -e_faceA: the normal on polygonA
|
||||
/// -e_faceB: the normal on polygonB
|
||||
/// We store contacts in this way so that position correction can
|
||||
/// account for movement, which is critical for continuous physics.
|
||||
/// All contact scenarios must be expressed in one of these types.
|
||||
/// This structure is stored across time steps, so we keep it small.
|
||||
struct b2Manifold
|
||||
{
|
||||
enum Type
|
||||
{
|
||||
e_circles,
|
||||
e_faceA,
|
||||
e_faceB
|
||||
};
|
||||
|
||||
b2ManifoldPoint points[b2_maxManifoldPoints]; ///< the points of contact
|
||||
b2Vec2 localNormal; ///< not use for Type::e_points
|
||||
b2Vec2 localPoint; ///< usage depends on manifold type
|
||||
Type type;
|
||||
int32 pointCount; ///< the number of manifold points
|
||||
};
|
||||
|
||||
/// This is used to compute the current state of a contact manifold.
|
||||
struct b2WorldManifold
|
||||
{
|
||||
/// Evaluate the manifold with supplied transforms. This assumes
|
||||
/// modest motion from the original state. This does not change the
|
||||
/// point count, impulses, etc. The radii must come from the shapes
|
||||
/// that generated the manifold.
|
||||
void Initialize(const b2Manifold* manifold,
|
||||
const b2Transform& xfA, float32 radiusA,
|
||||
const b2Transform& xfB, float32 radiusB);
|
||||
|
||||
b2Vec2 normal; ///< world vector pointing from A to B
|
||||
b2Vec2 points[b2_maxManifoldPoints]; ///< world contact point (point of intersection)
|
||||
float32 separations[b2_maxManifoldPoints]; ///< a negative value indicates overlap, in meters
|
||||
};
|
||||
|
||||
/// This is used for determining the state of contact points.
|
||||
enum b2PointState
|
||||
{
|
||||
b2_nullState, ///< point does not exist
|
||||
b2_addState, ///< point was added in the update
|
||||
b2_persistState, ///< point persisted across the update
|
||||
b2_removeState ///< point was removed in the update
|
||||
};
|
||||
|
||||
/// Compute the point states given two manifolds. The states pertain to the transition from manifold1
|
||||
/// to manifold2. So state1 is either persist or remove while state2 is either add or persist.
|
||||
void b2GetPointStates(b2PointState state1[b2_maxManifoldPoints], b2PointState state2[b2_maxManifoldPoints],
|
||||
const b2Manifold* manifold1, const b2Manifold* manifold2);
|
||||
|
||||
/// Used for computing contact manifolds.
|
||||
struct b2ClipVertex
|
||||
{
|
||||
b2Vec2 v;
|
||||
b2ContactID id;
|
||||
};
|
||||
|
||||
/// Ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
|
||||
struct b2RayCastInput
|
||||
{
|
||||
b2Vec2 p1, p2;
|
||||
float32 maxFraction;
|
||||
};
|
||||
|
||||
/// Ray-cast output data. The ray hits at p1 + fraction * (p2 - p1), where p1 and p2
|
||||
/// come from b2RayCastInput.
|
||||
struct b2RayCastOutput
|
||||
{
|
||||
b2Vec2 normal;
|
||||
float32 fraction;
|
||||
};
|
||||
|
||||
/// An axis aligned bounding box.
|
||||
struct b2AABB
|
||||
{
|
||||
/// Verify that the bounds are sorted.
|
||||
bool IsValid() const;
|
||||
|
||||
/// Get the center of the AABB.
|
||||
b2Vec2 GetCenter() const
|
||||
{
|
||||
return 0.5f * (lowerBound + upperBound);
|
||||
}
|
||||
|
||||
/// Get the extents of the AABB (half-widths).
|
||||
b2Vec2 GetExtents() const
|
||||
{
|
||||
return 0.5f * (upperBound - lowerBound);
|
||||
}
|
||||
|
||||
/// Get the perimeter length
|
||||
float32 GetPerimeter() const
|
||||
{
|
||||
float32 wx = upperBound.x - lowerBound.x;
|
||||
float32 wy = upperBound.y - lowerBound.y;
|
||||
return 2.0f * (wx + wy);
|
||||
}
|
||||
|
||||
/// Combine an AABB into this one.
|
||||
void Combine(const b2AABB& aabb)
|
||||
{
|
||||
lowerBound = b2Min(lowerBound, aabb.lowerBound);
|
||||
upperBound = b2Max(upperBound, aabb.upperBound);
|
||||
}
|
||||
|
||||
/// Combine two AABBs into this one.
|
||||
void Combine(const b2AABB& aabb1, const b2AABB& aabb2)
|
||||
{
|
||||
lowerBound = b2Min(aabb1.lowerBound, aabb2.lowerBound);
|
||||
upperBound = b2Max(aabb1.upperBound, aabb2.upperBound);
|
||||
}
|
||||
|
||||
/// Does this aabb contain the provided AABB.
|
||||
bool Contains(const b2AABB& aabb) const
|
||||
{
|
||||
bool result = true;
|
||||
result = result && lowerBound.x <= aabb.lowerBound.x;
|
||||
result = result && lowerBound.y <= aabb.lowerBound.y;
|
||||
result = result && aabb.upperBound.x <= upperBound.x;
|
||||
result = result && aabb.upperBound.y <= upperBound.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input) const;
|
||||
|
||||
b2Vec2 lowerBound; ///< the lower vertex
|
||||
b2Vec2 upperBound; ///< the upper vertex
|
||||
};
|
||||
|
||||
/// Compute the collision manifold between two circles.
|
||||
void b2CollideCircles(b2Manifold* manifold,
|
||||
const b2CircleShape* circleA, const b2Transform& xfA,
|
||||
const b2CircleShape* circleB, const b2Transform& xfB);
|
||||
|
||||
/// Compute the collision manifold between a polygon and a circle.
|
||||
void b2CollidePolygonAndCircle(b2Manifold* manifold,
|
||||
const b2PolygonShape* polygonA, const b2Transform& xfA,
|
||||
const b2CircleShape* circleB, const b2Transform& xfB);
|
||||
|
||||
/// Compute the collision manifold between two polygons.
|
||||
void b2CollidePolygons(b2Manifold* manifold,
|
||||
const b2PolygonShape* polygonA, const b2Transform& xfA,
|
||||
const b2PolygonShape* polygonB, const b2Transform& xfB);
|
||||
|
||||
/// Compute the collision manifold between an edge and a circle.
|
||||
void b2CollideEdgeAndCircle(b2Manifold* manifold,
|
||||
const b2EdgeShape* polygonA, const b2Transform& xfA,
|
||||
const b2CircleShape* circleB, const b2Transform& xfB);
|
||||
|
||||
/// Compute the collision manifold between an edge and a circle.
|
||||
void b2CollideEdgeAndPolygon(b2Manifold* manifold,
|
||||
const b2EdgeShape* edgeA, const b2Transform& xfA,
|
||||
const b2PolygonShape* circleB, const b2Transform& xfB);
|
||||
|
||||
/// Clipping for contact manifolds.
|
||||
int32 b2ClipSegmentToLine(b2ClipVertex vOut[2], const b2ClipVertex vIn[2],
|
||||
const b2Vec2& normal, float32 offset, int32 vertexIndexA);
|
||||
|
||||
/// Determine if two generic shapes overlap.
|
||||
bool b2TestOverlap( const b2Shape* shapeA, int32 indexA,
|
||||
const b2Shape* shapeB, int32 indexB,
|
||||
const b2Transform& xfA, const b2Transform& xfB);
|
||||
|
||||
// ---------------- Inline Functions ------------------------------------------
|
||||
|
||||
inline bool b2AABB::IsValid() const
|
||||
{
|
||||
b2Vec2 d = upperBound - lowerBound;
|
||||
bool valid = d.x >= 0.0f && d.y >= 0.0f;
|
||||
valid = valid && lowerBound.IsValid() && upperBound.IsValid();
|
||||
return valid;
|
||||
}
|
||||
|
||||
inline bool b2TestOverlap(const b2AABB& a, const b2AABB& b)
|
||||
{
|
||||
b2Vec2 d1, d2;
|
||||
d1 = b.lowerBound - a.upperBound;
|
||||
d2 = a.lowerBound - b.upperBound;
|
||||
|
||||
if (d1.x > 0.0f || d1.y > 0.0f)
|
||||
return false;
|
||||
|
||||
if (d2.x > 0.0f || d2.y > 0.0f)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
141
inc/Box2D/Collision/b2Distance.h
Normal file
141
inc/Box2D/Collision/b2Distance.h
Normal file
@@ -0,0 +1,141 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_DISTANCE_H
|
||||
#define B2_DISTANCE_H
|
||||
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
|
||||
class b2Shape;
|
||||
|
||||
/// A distance proxy is used by the GJK algorithm.
|
||||
/// It encapsulates any shape.
|
||||
struct b2DistanceProxy
|
||||
{
|
||||
b2DistanceProxy() : m_vertices(NULL), m_count(0), m_radius(0.0f) {}
|
||||
|
||||
/// Initialize the proxy using the given shape. The shape
|
||||
/// must remain in scope while the proxy is in use.
|
||||
void Set(const b2Shape* shape, int32 index);
|
||||
|
||||
/// Get the supporting vertex index in the given direction.
|
||||
int32 GetSupport(const b2Vec2& d) const;
|
||||
|
||||
/// Get the supporting vertex in the given direction.
|
||||
const b2Vec2& GetSupportVertex(const b2Vec2& d) const;
|
||||
|
||||
/// Get the vertex count.
|
||||
int32 GetVertexCount() const;
|
||||
|
||||
/// Get a vertex by index. Used by b2Distance.
|
||||
const b2Vec2& GetVertex(int32 index) const;
|
||||
|
||||
b2Vec2 m_buffer[2];
|
||||
const b2Vec2* m_vertices;
|
||||
int32 m_count;
|
||||
float32 m_radius;
|
||||
};
|
||||
|
||||
/// Used to warm start b2Distance.
|
||||
/// Set count to zero on first call.
|
||||
struct b2SimplexCache
|
||||
{
|
||||
float32 metric; ///< length or area
|
||||
uint16 count;
|
||||
uint8 indexA[3]; ///< vertices on shape A
|
||||
uint8 indexB[3]; ///< vertices on shape B
|
||||
};
|
||||
|
||||
/// Input for b2Distance.
|
||||
/// You have to option to use the shape radii
|
||||
/// in the computation. Even
|
||||
struct b2DistanceInput
|
||||
{
|
||||
b2DistanceProxy proxyA;
|
||||
b2DistanceProxy proxyB;
|
||||
b2Transform transformA;
|
||||
b2Transform transformB;
|
||||
bool useRadii;
|
||||
};
|
||||
|
||||
/// Output for b2Distance.
|
||||
struct b2DistanceOutput
|
||||
{
|
||||
b2Vec2 pointA; ///< closest point on shapeA
|
||||
b2Vec2 pointB; ///< closest point on shapeB
|
||||
float32 distance;
|
||||
int32 iterations; ///< number of GJK iterations used
|
||||
};
|
||||
|
||||
/// Compute the closest points between two shapes. Supports any combination of:
|
||||
/// b2CircleShape, b2PolygonShape, b2EdgeShape. The simplex cache is input/output.
|
||||
/// On the first call set b2SimplexCache.count to zero.
|
||||
void b2Distance(b2DistanceOutput* output,
|
||||
b2SimplexCache* cache,
|
||||
const b2DistanceInput* input);
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline int32 b2DistanceProxy::GetVertexCount() const
|
||||
{
|
||||
return m_count;
|
||||
}
|
||||
|
||||
inline const b2Vec2& b2DistanceProxy::GetVertex(int32 index) const
|
||||
{
|
||||
b2Assert(0 <= index && index < m_count);
|
||||
return m_vertices[index];
|
||||
}
|
||||
|
||||
inline int32 b2DistanceProxy::GetSupport(const b2Vec2& d) const
|
||||
{
|
||||
int32 bestIndex = 0;
|
||||
float32 bestValue = b2Dot(m_vertices[0], d);
|
||||
for (int32 i = 1; i < m_count; ++i)
|
||||
{
|
||||
float32 value = b2Dot(m_vertices[i], d);
|
||||
if (value > bestValue)
|
||||
{
|
||||
bestIndex = i;
|
||||
bestValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
return bestIndex;
|
||||
}
|
||||
|
||||
inline const b2Vec2& b2DistanceProxy::GetSupportVertex(const b2Vec2& d) const
|
||||
{
|
||||
int32 bestIndex = 0;
|
||||
float32 bestValue = b2Dot(m_vertices[0], d);
|
||||
for (int32 i = 1; i < m_count; ++i)
|
||||
{
|
||||
float32 value = b2Dot(m_vertices[i], d);
|
||||
if (value > bestValue)
|
||||
{
|
||||
bestIndex = i;
|
||||
bestValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
return m_vertices[bestIndex];
|
||||
}
|
||||
|
||||
#endif
|
||||
289
inc/Box2D/Collision/b2DynamicTree.h
Normal file
289
inc/Box2D/Collision/b2DynamicTree.h
Normal file
@@ -0,0 +1,289 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_DYNAMIC_TREE_H
|
||||
#define B2_DYNAMIC_TREE_H
|
||||
|
||||
#include <Box2D/Collision/b2Collision.h>
|
||||
#include <Box2D/Common/b2GrowableStack.h>
|
||||
|
||||
#define b2_nullNode (-1)
|
||||
|
||||
/// A node in the dynamic tree. The client does not interact with this directly.
|
||||
struct b2TreeNode
|
||||
{
|
||||
bool IsLeaf() const
|
||||
{
|
||||
return child1 == b2_nullNode;
|
||||
}
|
||||
|
||||
/// Enlarged AABB
|
||||
b2AABB aabb;
|
||||
|
||||
void* userData;
|
||||
|
||||
union
|
||||
{
|
||||
int32 parent;
|
||||
int32 next;
|
||||
};
|
||||
|
||||
int32 child1;
|
||||
int32 child2;
|
||||
|
||||
// leaf = 0, free node = -1
|
||||
int32 height;
|
||||
};
|
||||
|
||||
/// A dynamic AABB tree broad-phase, inspired by Nathanael Presson's btDbvt.
|
||||
/// A dynamic tree arranges data in a binary tree to accelerate
|
||||
/// queries such as volume queries and ray casts. Leafs are proxies
|
||||
/// with an AABB. In the tree we expand the proxy AABB by b2_fatAABBFactor
|
||||
/// so that the proxy AABB is bigger than the client object. This allows the client
|
||||
/// object to move by small amounts without triggering a tree update.
|
||||
///
|
||||
/// Nodes are pooled and relocatable, so we use node indices rather than pointers.
|
||||
class b2DynamicTree
|
||||
{
|
||||
public:
|
||||
/// Constructing the tree initializes the node pool.
|
||||
b2DynamicTree();
|
||||
|
||||
/// Destroy the tree, freeing the node pool.
|
||||
~b2DynamicTree();
|
||||
|
||||
/// Create a proxy. Provide a tight fitting AABB and a userData pointer.
|
||||
int32 CreateProxy(const b2AABB& aabb, void* userData);
|
||||
|
||||
/// Destroy a proxy. This asserts if the id is invalid.
|
||||
void DestroyProxy(int32 proxyId);
|
||||
|
||||
/// Move a proxy with a swepted AABB. If the proxy has moved outside of its fattened AABB,
|
||||
/// then the proxy is removed from the tree and re-inserted. Otherwise
|
||||
/// the function returns immediately.
|
||||
/// @return true if the proxy was re-inserted.
|
||||
bool MoveProxy(int32 proxyId, const b2AABB& aabb1, const b2Vec2& displacement);
|
||||
|
||||
/// Get proxy user data.
|
||||
/// @return the proxy user data or 0 if the id is invalid.
|
||||
void* GetUserData(int32 proxyId) const;
|
||||
|
||||
/// Get the fat AABB for a proxy.
|
||||
const b2AABB& GetFatAABB(int32 proxyId) const;
|
||||
|
||||
/// Query an AABB for overlapping proxies. The callback class
|
||||
/// is called for each proxy that overlaps the supplied AABB.
|
||||
template <typename T>
|
||||
void Query(T* callback, const b2AABB& aabb) const;
|
||||
|
||||
/// Ray-cast against the proxies in the tree. This relies on the callback
|
||||
/// to perform a exact ray-cast in the case were the proxy contains a shape.
|
||||
/// The callback also performs the any collision filtering. This has performance
|
||||
/// roughly equal to k * log(n), where k is the number of collisions and n is the
|
||||
/// number of proxies in the tree.
|
||||
/// @param input the ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
|
||||
/// @param callback a callback class that is called for each proxy that is hit by the ray.
|
||||
template <typename T>
|
||||
void RayCast(T* callback, const b2RayCastInput& input) const;
|
||||
|
||||
/// Validate this tree. For testing.
|
||||
void Validate() const;
|
||||
|
||||
/// Compute the height of the binary tree in O(N) time. Should not be
|
||||
/// called often.
|
||||
int32 GetHeight() const;
|
||||
|
||||
/// Get the maximum balance of an node in the tree. The balance is the difference
|
||||
/// in height of the two children of a node.
|
||||
int32 GetMaxBalance() const;
|
||||
|
||||
/// Get the ratio of the sum of the node areas to the root area.
|
||||
float32 GetAreaRatio() const;
|
||||
|
||||
/// Build an optimal tree. Very expensive. For testing.
|
||||
void RebuildBottomUp();
|
||||
|
||||
/// Shift the world origin. Useful for large worlds.
|
||||
/// The shift formula is: position -= newOrigin
|
||||
/// @param newOrigin the new origin with respect to the old origin
|
||||
void ShiftOrigin(const b2Vec2& newOrigin);
|
||||
|
||||
private:
|
||||
|
||||
int32 AllocateNode();
|
||||
void FreeNode(int32 node);
|
||||
|
||||
void InsertLeaf(int32 node);
|
||||
void RemoveLeaf(int32 node);
|
||||
|
||||
int32 Balance(int32 index);
|
||||
|
||||
int32 ComputeHeight() const;
|
||||
int32 ComputeHeight(int32 nodeId) const;
|
||||
|
||||
void ValidateStructure(int32 index) const;
|
||||
void ValidateMetrics(int32 index) const;
|
||||
|
||||
int32 m_root;
|
||||
|
||||
b2TreeNode* m_nodes;
|
||||
int32 m_nodeCount;
|
||||
int32 m_nodeCapacity;
|
||||
|
||||
int32 m_freeList;
|
||||
|
||||
/// This is used to incrementally traverse the tree for re-balancing.
|
||||
uint32 m_path;
|
||||
|
||||
int32 m_insertionCount;
|
||||
};
|
||||
|
||||
inline void* b2DynamicTree::GetUserData(int32 proxyId) const
|
||||
{
|
||||
b2Assert(0 <= proxyId && proxyId < m_nodeCapacity);
|
||||
return m_nodes[proxyId].userData;
|
||||
}
|
||||
|
||||
inline const b2AABB& b2DynamicTree::GetFatAABB(int32 proxyId) const
|
||||
{
|
||||
b2Assert(0 <= proxyId && proxyId < m_nodeCapacity);
|
||||
return m_nodes[proxyId].aabb;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void b2DynamicTree::Query(T* callback, const b2AABB& aabb) const
|
||||
{
|
||||
b2GrowableStack<int32, 256> stack;
|
||||
stack.Push(m_root);
|
||||
|
||||
while (stack.GetCount() > 0)
|
||||
{
|
||||
int32 nodeId = stack.Pop();
|
||||
if (nodeId == b2_nullNode)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const b2TreeNode* node = m_nodes + nodeId;
|
||||
|
||||
if (b2TestOverlap(node->aabb, aabb))
|
||||
{
|
||||
if (node->IsLeaf())
|
||||
{
|
||||
bool proceed = callback->QueryCallback(nodeId);
|
||||
if (proceed == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
stack.Push(node->child1);
|
||||
stack.Push(node->child2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void b2DynamicTree::RayCast(T* callback, const b2RayCastInput& input) const
|
||||
{
|
||||
b2Vec2 p1 = input.p1;
|
||||
b2Vec2 p2 = input.p2;
|
||||
b2Vec2 r = p2 - p1;
|
||||
b2Assert(r.LengthSquared() > 0.0f);
|
||||
r.Normalize();
|
||||
|
||||
// v is perpendicular to the segment.
|
||||
b2Vec2 v = b2Cross(1.0f, r);
|
||||
b2Vec2 abs_v = b2Abs(v);
|
||||
|
||||
// Separating axis for segment (Gino, p80).
|
||||
// |dot(v, p1 - c)| > dot(|v|, h)
|
||||
|
||||
float32 maxFraction = input.maxFraction;
|
||||
|
||||
// Build a bounding box for the segment.
|
||||
b2AABB segmentAABB;
|
||||
{
|
||||
b2Vec2 t = p1 + maxFraction * (p2 - p1);
|
||||
segmentAABB.lowerBound = b2Min(p1, t);
|
||||
segmentAABB.upperBound = b2Max(p1, t);
|
||||
}
|
||||
|
||||
b2GrowableStack<int32, 256> stack;
|
||||
stack.Push(m_root);
|
||||
|
||||
while (stack.GetCount() > 0)
|
||||
{
|
||||
int32 nodeId = stack.Pop();
|
||||
if (nodeId == b2_nullNode)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const b2TreeNode* node = m_nodes + nodeId;
|
||||
|
||||
if (b2TestOverlap(node->aabb, segmentAABB) == false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Separating axis for segment (Gino, p80).
|
||||
// |dot(v, p1 - c)| > dot(|v|, h)
|
||||
b2Vec2 c = node->aabb.GetCenter();
|
||||
b2Vec2 h = node->aabb.GetExtents();
|
||||
float32 separation = b2Abs(b2Dot(v, p1 - c)) - b2Dot(abs_v, h);
|
||||
if (separation > 0.0f)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (node->IsLeaf())
|
||||
{
|
||||
b2RayCastInput subInput;
|
||||
subInput.p1 = input.p1;
|
||||
subInput.p2 = input.p2;
|
||||
subInput.maxFraction = maxFraction;
|
||||
|
||||
float32 value = callback->RayCastCallback(subInput, nodeId);
|
||||
|
||||
if (value == 0.0f)
|
||||
{
|
||||
// The client has terminated the ray cast.
|
||||
return;
|
||||
}
|
||||
|
||||
if (value > 0.0f)
|
||||
{
|
||||
// Update segment bounding box.
|
||||
maxFraction = value;
|
||||
b2Vec2 t = p1 + maxFraction * (p2 - p1);
|
||||
segmentAABB.lowerBound = b2Min(p1, t);
|
||||
segmentAABB.upperBound = b2Max(p1, t);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
stack.Push(node->child1);
|
||||
stack.Push(node->child2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
58
inc/Box2D/Collision/b2TimeOfImpact.h
Normal file
58
inc/Box2D/Collision/b2TimeOfImpact.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_TIME_OF_IMPACT_H
|
||||
#define B2_TIME_OF_IMPACT_H
|
||||
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
#include <Box2D/Collision/b2Distance.h>
|
||||
|
||||
/// Input parameters for b2TimeOfImpact
|
||||
struct b2TOIInput
|
||||
{
|
||||
b2DistanceProxy proxyA;
|
||||
b2DistanceProxy proxyB;
|
||||
b2Sweep sweepA;
|
||||
b2Sweep sweepB;
|
||||
float32 tMax; // defines sweep interval [0, tMax]
|
||||
};
|
||||
|
||||
// Output parameters for b2TimeOfImpact.
|
||||
struct b2TOIOutput
|
||||
{
|
||||
enum State
|
||||
{
|
||||
e_unknown,
|
||||
e_failed,
|
||||
e_overlapped,
|
||||
e_touching,
|
||||
e_separated
|
||||
};
|
||||
|
||||
State state;
|
||||
float32 t;
|
||||
};
|
||||
|
||||
/// Compute the upper bound on time before two shapes penetrate. Time is represented as
|
||||
/// a fraction between [0,tMax]. This uses a swept separating axis and may miss some intermediate,
|
||||
/// non-tunneling collision. If you change the time interval, you should call this function
|
||||
/// again.
|
||||
/// Note: use b2Distance to compute the contact point and normal at the time of impact.
|
||||
void b2TimeOfImpact(b2TOIOutput* output, const b2TOIInput* input);
|
||||
|
||||
#endif
|
||||
62
inc/Box2D/Common/b2BlockAllocator.h
Normal file
62
inc/Box2D/Common/b2BlockAllocator.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_BLOCK_ALLOCATOR_H
|
||||
#define B2_BLOCK_ALLOCATOR_H
|
||||
|
||||
#include <Box2D/Common/b2Settings.h>
|
||||
|
||||
const int32 b2_chunkSize = 16 * 1024;
|
||||
const int32 b2_maxBlockSize = 640;
|
||||
const int32 b2_blockSizes = 14;
|
||||
const int32 b2_chunkArrayIncrement = 128;
|
||||
|
||||
struct b2Block;
|
||||
struct b2Chunk;
|
||||
|
||||
/// This is a small object allocator used for allocating small
|
||||
/// objects that persist for more than one time step.
|
||||
/// See: http://www.codeproject.com/useritems/Small_Block_Allocator.asp
|
||||
class b2BlockAllocator
|
||||
{
|
||||
public:
|
||||
b2BlockAllocator();
|
||||
~b2BlockAllocator();
|
||||
|
||||
/// Allocate memory. This will use b2Alloc if the size is larger than b2_maxBlockSize.
|
||||
void* Allocate(int32 size);
|
||||
|
||||
/// Free memory. This will use b2Free if the size is larger than b2_maxBlockSize.
|
||||
void Free(void* p, int32 size);
|
||||
|
||||
void Clear();
|
||||
|
||||
private:
|
||||
|
||||
b2Chunk* m_chunks;
|
||||
int32 m_chunkCount;
|
||||
int32 m_chunkSpace;
|
||||
|
||||
b2Block* m_freeLists[b2_blockSizes];
|
||||
|
||||
static int32 s_blockSizes[b2_blockSizes];
|
||||
static uint8 s_blockSizeLookup[b2_maxBlockSize + 1];
|
||||
static bool s_blockSizeLookupInitialized;
|
||||
};
|
||||
|
||||
#endif
|
||||
97
inc/Box2D/Common/b2Draw.h
Normal file
97
inc/Box2D/Common/b2Draw.h
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (c) 2011 Erin Catto http://box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_DRAW_H
|
||||
#define B2_DRAW_H
|
||||
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
|
||||
/// Color for debug drawing. Each value has the range [0,1].
|
||||
struct b2Color
|
||||
{
|
||||
b2Color() {}
|
||||
b2Color(float32 rIn, float32 gIn, float32 bIn, float32 aIn = 1.0f)
|
||||
{
|
||||
r = rIn; g = gIn; b = bIn; a = aIn;
|
||||
}
|
||||
|
||||
void Set(float32 rIn, float32 gIn, float32 bIn, float32 aIn = 1.0f)
|
||||
{
|
||||
r = rIn; g = gIn; b = bIn; a = aIn;
|
||||
}
|
||||
|
||||
float32 r, g, b, a;
|
||||
};
|
||||
|
||||
/// Implement and register this class with a b2World to provide debug drawing of physics
|
||||
/// entities in your game.
|
||||
class b2Draw
|
||||
{
|
||||
public:
|
||||
b2Draw();
|
||||
|
||||
virtual ~b2Draw() {}
|
||||
|
||||
enum
|
||||
{
|
||||
e_shapeBit = 0x0001, ///< draw shapes
|
||||
e_jointBit = 0x0002, ///< draw joint connections
|
||||
e_aabbBit = 0x0004, ///< draw axis aligned bounding boxes
|
||||
e_pairBit = 0x0008, ///< draw broad-phase pairs
|
||||
e_centerOfMassBit = 0x0010 ///< draw center of mass frame
|
||||
};
|
||||
|
||||
/// Set the drawing flags.
|
||||
void SetFlags(uint32 flags);
|
||||
|
||||
/// Get the drawing flags.
|
||||
uint32 GetFlags() const;
|
||||
|
||||
/// Append flags to the current flags.
|
||||
void AppendFlags(uint32 flags);
|
||||
|
||||
/// Clear flags from the current flags.
|
||||
void ClearFlags(uint32 flags);
|
||||
|
||||
/// Draw a closed polygon provided in CCW order.
|
||||
virtual void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) = 0;
|
||||
|
||||
/// Draw a solid closed polygon provided in CCW order.
|
||||
virtual void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) = 0;
|
||||
|
||||
/// Draw a circle.
|
||||
virtual void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color) = 0;
|
||||
|
||||
/// Draw a solid circle.
|
||||
virtual void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color) = 0;
|
||||
|
||||
/// Draw a line segment.
|
||||
virtual void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color) = 0;
|
||||
|
||||
/// Draw a transform. Choose your own length scale.
|
||||
/// @param xf a transform.
|
||||
virtual void DrawTransform(const b2Transform& xf) = 0;
|
||||
|
||||
/// Draw a point.
|
||||
virtual void DrawPoint(const b2Vec2& p, float32 size, const b2Color& color) = 0;
|
||||
|
||||
protected:
|
||||
uint32 m_drawFlags;
|
||||
};
|
||||
|
||||
#endif
|
||||
85
inc/Box2D/Common/b2GrowableStack.h
Normal file
85
inc/Box2D/Common/b2GrowableStack.h
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_GROWABLE_STACK_H
|
||||
#define B2_GROWABLE_STACK_H
|
||||
#include <Box2D/Common/b2Settings.h>
|
||||
#include <string.h>
|
||||
|
||||
/// This is a growable LIFO stack with an initial capacity of N.
|
||||
/// If the stack size exceeds the initial capacity, the heap is used
|
||||
/// to increase the size of the stack.
|
||||
template <typename T, int32 N>
|
||||
class b2GrowableStack
|
||||
{
|
||||
public:
|
||||
b2GrowableStack()
|
||||
{
|
||||
m_stack = m_array;
|
||||
m_count = 0;
|
||||
m_capacity = N;
|
||||
}
|
||||
|
||||
~b2GrowableStack()
|
||||
{
|
||||
if (m_stack != m_array)
|
||||
{
|
||||
b2Free(m_stack);
|
||||
m_stack = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void Push(const T& element)
|
||||
{
|
||||
if (m_count == m_capacity)
|
||||
{
|
||||
T* old = m_stack;
|
||||
m_capacity *= 2;
|
||||
m_stack = (T*)b2Alloc(m_capacity * sizeof(T));
|
||||
memcpy(m_stack, old, m_count * sizeof(T));
|
||||
if (old != m_array)
|
||||
{
|
||||
b2Free(old);
|
||||
}
|
||||
}
|
||||
|
||||
m_stack[m_count] = element;
|
||||
++m_count;
|
||||
}
|
||||
|
||||
T Pop()
|
||||
{
|
||||
b2Assert(m_count > 0);
|
||||
--m_count;
|
||||
return m_stack[m_count];
|
||||
}
|
||||
|
||||
int32 GetCount()
|
||||
{
|
||||
return m_count;
|
||||
}
|
||||
|
||||
private:
|
||||
T* m_stack;
|
||||
T m_array[N];
|
||||
int32 m_count;
|
||||
int32 m_capacity;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
726
inc/Box2D/Common/b2Math.h
Normal file
726
inc/Box2D/Common/b2Math.h
Normal file
@@ -0,0 +1,726 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_MATH_H
|
||||
#define B2_MATH_H
|
||||
|
||||
#include <Box2D/Common/b2Settings.h>
|
||||
#include <math.h>
|
||||
|
||||
/// This function is used to ensure that a floating point number is not a NaN or infinity.
|
||||
inline bool b2IsValid(float32 x)
|
||||
{
|
||||
int32 ix = *reinterpret_cast<int32*>(&x);
|
||||
return (ix & 0x7f800000) != 0x7f800000;
|
||||
}
|
||||
|
||||
/// This is a approximate yet fast inverse square-root.
|
||||
inline float32 b2InvSqrt(float32 x)
|
||||
{
|
||||
union
|
||||
{
|
||||
float32 x;
|
||||
int32 i;
|
||||
} convert;
|
||||
|
||||
convert.x = x;
|
||||
float32 xhalf = 0.5f * x;
|
||||
convert.i = 0x5f3759df - (convert.i >> 1);
|
||||
x = convert.x;
|
||||
x = x * (1.5f - xhalf * x * x);
|
||||
return x;
|
||||
}
|
||||
|
||||
#define b2Sqrt(x) sqrtf(x)
|
||||
#define b2Atan2(y, x) atan2f(y, x)
|
||||
|
||||
/// A 2D column vector.
|
||||
struct b2Vec2
|
||||
{
|
||||
/// Default constructor does nothing (for performance).
|
||||
b2Vec2(): x(0.f), y(0.f) {}
|
||||
|
||||
/// Construct using coordinates.
|
||||
b2Vec2(float32 xIn, float32 yIn) : x(xIn), y(yIn) {}
|
||||
|
||||
/// Set this vector to all zeros.
|
||||
void SetZero() { x = 0.0f; y = 0.0f; }
|
||||
|
||||
/// Set this vector to some specified coordinates.
|
||||
void Set(float32 x_, float32 y_) { x = x_; y = y_; }
|
||||
|
||||
/// Negate this vector.
|
||||
b2Vec2 operator -() const { b2Vec2 v; v.Set(-x, -y); return v; }
|
||||
|
||||
/// Read from and indexed element.
|
||||
float32 operator () (int32 i) const
|
||||
{
|
||||
return (&x)[i];
|
||||
}
|
||||
|
||||
/// Write to an indexed element.
|
||||
float32& operator () (int32 i)
|
||||
{
|
||||
return (&x)[i];
|
||||
}
|
||||
|
||||
/// Add a vector to this vector.
|
||||
void operator += (const b2Vec2& v)
|
||||
{
|
||||
x += v.x; y += v.y;
|
||||
}
|
||||
|
||||
/// Subtract a vector from this vector.
|
||||
void operator -= (const b2Vec2& v)
|
||||
{
|
||||
x -= v.x; y -= v.y;
|
||||
}
|
||||
|
||||
/// Multiply this vector by a scalar.
|
||||
void operator *= (float32 a)
|
||||
{
|
||||
x *= a; y *= a;
|
||||
}
|
||||
|
||||
/// Get the length of this vector (the norm).
|
||||
float32 Length() const
|
||||
{
|
||||
return b2Sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
/// Get the length squared. For performance, use this instead of
|
||||
/// b2Vec2::Length (if possible).
|
||||
float32 LengthSquared() const
|
||||
{
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
/// Convert this vector into a unit vector. Returns the length.
|
||||
float32 Normalize()
|
||||
{
|
||||
float32 length = Length();
|
||||
if (length < b2_epsilon)
|
||||
{
|
||||
return 0.0f;
|
||||
}
|
||||
float32 invLength = 1.0f / length;
|
||||
x *= invLength;
|
||||
y *= invLength;
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
/// Does this vector contain finite coordinates?
|
||||
bool IsValid() const
|
||||
{
|
||||
return b2IsValid(x) && b2IsValid(y);
|
||||
}
|
||||
|
||||
/// Get the skew vector such that dot(skew_vec, other) == cross(vec, other)
|
||||
b2Vec2 Skew() const
|
||||
{
|
||||
return b2Vec2(-y, x);
|
||||
}
|
||||
|
||||
b2Vec2& operator/=(const float& divider);
|
||||
float32 x, y;
|
||||
};
|
||||
|
||||
/// A 2D column vector with 3 elements.
|
||||
struct b2Vec3
|
||||
{
|
||||
/// Default constructor does nothing (for performance).
|
||||
b2Vec3() : x(0.f), y(0.f), z(0.f) {}
|
||||
|
||||
/// Construct using coordinates.
|
||||
b2Vec3(float32 xIn, float32 yIn, float32 zIn) : x(xIn), y(yIn), z(zIn) {}
|
||||
|
||||
/// Set this vector to all zeros.
|
||||
void SetZero() { x = 0.0f; y = 0.0f; z = 0.0f; }
|
||||
|
||||
/// Set this vector to some specified coordinates.
|
||||
void Set(float32 x_, float32 y_, float32 z_) { x = x_; y = y_; z = z_; }
|
||||
|
||||
/// Negate this vector.
|
||||
b2Vec3 operator -() const { b2Vec3 v; v.Set(-x, -y, -z); return v; }
|
||||
|
||||
/// Add a vector to this vector.
|
||||
void operator += (const b2Vec3& v)
|
||||
{
|
||||
x += v.x; y += v.y; z += v.z;
|
||||
}
|
||||
|
||||
/// Subtract a vector from this vector.
|
||||
void operator -= (const b2Vec3& v)
|
||||
{
|
||||
x -= v.x; y -= v.y; z -= v.z;
|
||||
}
|
||||
|
||||
/// Multiply this vector by a scalar.
|
||||
void operator *= (float32 s)
|
||||
{
|
||||
x *= s; y *= s; z *= s;
|
||||
}
|
||||
|
||||
float32 x, y, z;
|
||||
};
|
||||
|
||||
/// A 2-by-2 matrix. Stored in column-major order.
|
||||
struct b2Mat22
|
||||
{
|
||||
/// The default constructor does nothing (for performance).
|
||||
b2Mat22() {}
|
||||
|
||||
/// Construct this matrix using columns.
|
||||
b2Mat22(const b2Vec2& c1, const b2Vec2& c2)
|
||||
{
|
||||
ex = c1;
|
||||
ey = c2;
|
||||
}
|
||||
|
||||
/// Construct this matrix using scalars.
|
||||
b2Mat22(float32 a11, float32 a12, float32 a21, float32 a22)
|
||||
{
|
||||
ex.x = a11; ex.y = a21;
|
||||
ey.x = a12; ey.y = a22;
|
||||
}
|
||||
|
||||
/// Initialize this matrix using columns.
|
||||
void Set(const b2Vec2& c1, const b2Vec2& c2)
|
||||
{
|
||||
ex = c1;
|
||||
ey = c2;
|
||||
}
|
||||
|
||||
/// Set this to the identity matrix.
|
||||
void SetIdentity()
|
||||
{
|
||||
ex.x = 1.0f; ey.x = 0.0f;
|
||||
ex.y = 0.0f; ey.y = 1.0f;
|
||||
}
|
||||
|
||||
/// Set this matrix to all zeros.
|
||||
void SetZero()
|
||||
{
|
||||
ex.x = 0.0f; ey.x = 0.0f;
|
||||
ex.y = 0.0f; ey.y = 0.0f;
|
||||
}
|
||||
|
||||
b2Mat22 GetInverse() const
|
||||
{
|
||||
float32 a = ex.x, b = ey.x, c = ex.y, d = ey.y;
|
||||
b2Mat22 B;
|
||||
float32 det = a * d - b * c;
|
||||
if (det != 0.0f)
|
||||
{
|
||||
det = 1.0f / det;
|
||||
}
|
||||
B.ex.x = det * d; B.ey.x = -det * b;
|
||||
B.ex.y = -det * c; B.ey.y = det * a;
|
||||
return B;
|
||||
}
|
||||
|
||||
/// Solve A * x = b, where b is a column vector. This is more efficient
|
||||
/// than computing the inverse in one-shot cases.
|
||||
b2Vec2 Solve(const b2Vec2& b) const
|
||||
{
|
||||
float32 a11 = ex.x, a12 = ey.x, a21 = ex.y, a22 = ey.y;
|
||||
float32 det = a11 * a22 - a12 * a21;
|
||||
if (det != 0.0f)
|
||||
{
|
||||
det = 1.0f / det;
|
||||
}
|
||||
b2Vec2 x;
|
||||
x.x = det * (a22 * b.x - a12 * b.y);
|
||||
x.y = det * (a11 * b.y - a21 * b.x);
|
||||
return x;
|
||||
}
|
||||
|
||||
b2Vec2 ex, ey;
|
||||
};
|
||||
|
||||
/// A 3-by-3 matrix. Stored in column-major order.
|
||||
struct b2Mat33
|
||||
{
|
||||
/// The default constructor does nothing (for performance).
|
||||
b2Mat33() {}
|
||||
|
||||
/// Construct this matrix using columns.
|
||||
b2Mat33(const b2Vec3& c1, const b2Vec3& c2, const b2Vec3& c3)
|
||||
{
|
||||
ex = c1;
|
||||
ey = c2;
|
||||
ez = c3;
|
||||
}
|
||||
|
||||
/// Set this matrix to all zeros.
|
||||
void SetZero()
|
||||
{
|
||||
ex.SetZero();
|
||||
ey.SetZero();
|
||||
ez.SetZero();
|
||||
}
|
||||
|
||||
/// Solve A * x = b, where b is a column vector. This is more efficient
|
||||
/// than computing the inverse in one-shot cases.
|
||||
b2Vec3 Solve33(const b2Vec3& b) const;
|
||||
|
||||
/// Solve A * x = b, where b is a column vector. This is more efficient
|
||||
/// than computing the inverse in one-shot cases. Solve only the upper
|
||||
/// 2-by-2 matrix equation.
|
||||
b2Vec2 Solve22(const b2Vec2& b) const;
|
||||
|
||||
/// Get the inverse of this matrix as a 2-by-2.
|
||||
/// Returns the zero matrix if singular.
|
||||
void GetInverse22(b2Mat33* M) const;
|
||||
|
||||
/// Get the symmetric inverse of this matrix as a 3-by-3.
|
||||
/// Returns the zero matrix if singular.
|
||||
void GetSymInverse33(b2Mat33* M) const;
|
||||
|
||||
b2Vec3 ex, ey, ez;
|
||||
};
|
||||
|
||||
/// Rotation
|
||||
struct b2Rot
|
||||
{
|
||||
b2Rot() : s(0.f), c(0.f) {}
|
||||
|
||||
/// Initialize from an angle in radians
|
||||
explicit b2Rot(float32 angle)
|
||||
{
|
||||
/// TODO_ERIN optimize
|
||||
s = sinf(angle);
|
||||
c = cosf(angle);
|
||||
}
|
||||
|
||||
/// Set using an angle in radians.
|
||||
void Set(float32 angle)
|
||||
{
|
||||
/// TODO_ERIN optimize
|
||||
s = sinf(angle);
|
||||
c = cosf(angle);
|
||||
}
|
||||
|
||||
/// Set to the identity rotation
|
||||
void SetIdentity()
|
||||
{
|
||||
s = 0.0f;
|
||||
c = 1.0f;
|
||||
}
|
||||
|
||||
/// Get the angle in radians
|
||||
float32 GetAngle() const
|
||||
{
|
||||
return b2Atan2(s, c);
|
||||
}
|
||||
|
||||
/// Get the x-axis
|
||||
b2Vec2 GetXAxis() const
|
||||
{
|
||||
return b2Vec2(c, s);
|
||||
}
|
||||
|
||||
/// Get the u-axis
|
||||
b2Vec2 GetYAxis() const
|
||||
{
|
||||
return b2Vec2(-s, c);
|
||||
}
|
||||
|
||||
/// Sine and cosine
|
||||
float32 s, c;
|
||||
};
|
||||
|
||||
/// A transform contains translation and rotation. It is used to represent
|
||||
/// the position and orientation of rigid frames.
|
||||
struct b2Transform
|
||||
{
|
||||
/// The default constructor does nothing.
|
||||
b2Transform() {}
|
||||
|
||||
/// Initialize using a position vector and a rotation.
|
||||
b2Transform(const b2Vec2& position, const b2Rot& rotation) : p(position), q(rotation) {}
|
||||
|
||||
/// Set this to the identity transform.
|
||||
void SetIdentity()
|
||||
{
|
||||
p.SetZero();
|
||||
q.SetIdentity();
|
||||
}
|
||||
|
||||
/// Set this based on the position and angle.
|
||||
void Set(const b2Vec2& position, float32 angle)
|
||||
{
|
||||
p = position;
|
||||
q.Set(angle);
|
||||
}
|
||||
|
||||
b2Vec2 p;
|
||||
b2Rot q;
|
||||
};
|
||||
|
||||
/// This describes the motion of a body/shape for TOI computation.
|
||||
/// Shapes are defined with respect to the body origin, which may
|
||||
/// no coincide with the center of mass. However, to support dynamics
|
||||
/// we must interpolate the center of mass position.
|
||||
struct b2Sweep
|
||||
{
|
||||
/// Get the interpolated transform at a specific time.
|
||||
/// @param beta is a factor in [0,1], where 0 indicates alpha0.
|
||||
void GetTransform(b2Transform* xfb, float32 beta) const;
|
||||
|
||||
/// Advance the sweep forward, yielding a new initial state.
|
||||
/// @param alpha the new initial time.
|
||||
void Advance(float32 alpha);
|
||||
|
||||
/// Normalize the angles.
|
||||
void Normalize();
|
||||
|
||||
b2Vec2 localCenter; ///< local center of mass position
|
||||
b2Vec2 c0, c; ///< center world positions
|
||||
float32 a0, a; ///< world angles
|
||||
|
||||
/// Fraction of the current time step in the range [0,1]
|
||||
/// c0 and a0 are the positions at alpha0.
|
||||
float32 alpha0;
|
||||
};
|
||||
|
||||
/// Useful constant
|
||||
extern const b2Vec2 b2Vec2_zero;
|
||||
|
||||
/// Perform the dot product on two vectors.
|
||||
inline float32 b2Dot(const b2Vec2& a, const b2Vec2& b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y;
|
||||
}
|
||||
|
||||
/// Perform the cross product on two vectors. In 2D this produces a scalar.
|
||||
inline float32 b2Cross(const b2Vec2& a, const b2Vec2& b)
|
||||
{
|
||||
return a.x * b.y - a.y * b.x;
|
||||
}
|
||||
|
||||
/// Perform the cross product on a vector and a scalar. In 2D this produces
|
||||
/// a vector.
|
||||
inline b2Vec2 b2Cross(const b2Vec2& a, float32 s)
|
||||
{
|
||||
return b2Vec2(s * a.y, -s * a.x);
|
||||
}
|
||||
|
||||
/// Perform the cross product on a scalar and a vector. In 2D this produces
|
||||
/// a vector.
|
||||
inline b2Vec2 b2Cross(float32 s, const b2Vec2& a)
|
||||
{
|
||||
return b2Vec2(-s * a.y, s * a.x);
|
||||
}
|
||||
|
||||
/// Multiply a matrix times a vector. If a rotation matrix is provided,
|
||||
/// then this transforms the vector from one frame to another.
|
||||
inline b2Vec2 b2Mul(const b2Mat22& A, const b2Vec2& v)
|
||||
{
|
||||
return b2Vec2(A.ex.x * v.x + A.ey.x * v.y, A.ex.y * v.x + A.ey.y * v.y);
|
||||
}
|
||||
|
||||
/// Multiply a matrix transpose times a vector. If a rotation matrix is provided,
|
||||
/// then this transforms the vector from one frame to another (inverse transform).
|
||||
inline b2Vec2 b2MulT(const b2Mat22& A, const b2Vec2& v)
|
||||
{
|
||||
return b2Vec2(b2Dot(v, A.ex), b2Dot(v, A.ey));
|
||||
}
|
||||
|
||||
/// Add two vectors component-wise.
|
||||
inline b2Vec2 operator + (const b2Vec2& a, const b2Vec2& b)
|
||||
{
|
||||
return b2Vec2(a.x + b.x, a.y + b.y);
|
||||
}
|
||||
|
||||
/// Subtract two vectors component-wise.
|
||||
inline b2Vec2 operator - (const b2Vec2& a, const b2Vec2& b)
|
||||
{
|
||||
return b2Vec2(a.x - b.x, a.y - b.y);
|
||||
}
|
||||
|
||||
inline b2Vec2 operator * (float32 s, const b2Vec2& a)
|
||||
{
|
||||
return b2Vec2(s * a.x, s * a.y);
|
||||
}
|
||||
|
||||
inline bool operator == (const b2Vec2& a, const b2Vec2& b)
|
||||
{
|
||||
return a.x == b.x && a.y == b.y;
|
||||
}
|
||||
|
||||
inline bool operator != (const b2Vec2& a, const b2Vec2& b)
|
||||
{
|
||||
return a.x != b.x || a.y != b.y;
|
||||
}
|
||||
|
||||
inline float32 b2Distance(const b2Vec2& a, const b2Vec2& b)
|
||||
{
|
||||
b2Vec2 c = a - b;
|
||||
return c.Length();
|
||||
}
|
||||
|
||||
inline float32 b2DistanceSquared(const b2Vec2& a, const b2Vec2& b)
|
||||
{
|
||||
b2Vec2 c = a - b;
|
||||
return b2Dot(c, c);
|
||||
}
|
||||
|
||||
inline b2Vec3 operator * (float32 s, const b2Vec3& a)
|
||||
{
|
||||
return b2Vec3(s * a.x, s * a.y, s * a.z);
|
||||
}
|
||||
|
||||
/// Add two vectors component-wise.
|
||||
inline b2Vec3 operator + (const b2Vec3& a, const b2Vec3& b)
|
||||
{
|
||||
return b2Vec3(a.x + b.x, a.y + b.y, a.z + b.z);
|
||||
}
|
||||
|
||||
/// Subtract two vectors component-wise.
|
||||
inline b2Vec3 operator - (const b2Vec3& a, const b2Vec3& b)
|
||||
{
|
||||
return b2Vec3(a.x - b.x, a.y - b.y, a.z - b.z);
|
||||
}
|
||||
|
||||
/// Perform the dot product on two vectors.
|
||||
inline float32 b2Dot(const b2Vec3& a, const b2Vec3& b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
/// Perform the cross product on two vectors.
|
||||
inline b2Vec3 b2Cross(const b2Vec3& a, const b2Vec3& b)
|
||||
{
|
||||
return b2Vec3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
|
||||
}
|
||||
|
||||
inline b2Mat22 operator + (const b2Mat22& A, const b2Mat22& B)
|
||||
{
|
||||
return b2Mat22(A.ex + B.ex, A.ey + B.ey);
|
||||
}
|
||||
|
||||
// A * B
|
||||
inline b2Mat22 b2Mul(const b2Mat22& A, const b2Mat22& B)
|
||||
{
|
||||
return b2Mat22(b2Mul(A, B.ex), b2Mul(A, B.ey));
|
||||
}
|
||||
|
||||
// A^T * B
|
||||
inline b2Mat22 b2MulT(const b2Mat22& A, const b2Mat22& B)
|
||||
{
|
||||
b2Vec2 c1(b2Dot(A.ex, B.ex), b2Dot(A.ey, B.ex));
|
||||
b2Vec2 c2(b2Dot(A.ex, B.ey), b2Dot(A.ey, B.ey));
|
||||
return b2Mat22(c1, c2);
|
||||
}
|
||||
|
||||
/// Multiply a matrix times a vector.
|
||||
inline b2Vec3 b2Mul(const b2Mat33& A, const b2Vec3& v)
|
||||
{
|
||||
return v.x * A.ex + v.y * A.ey + v.z * A.ez;
|
||||
}
|
||||
|
||||
/// Multiply a matrix times a vector.
|
||||
inline b2Vec2 b2Mul22(const b2Mat33& A, const b2Vec2& v)
|
||||
{
|
||||
return b2Vec2(A.ex.x * v.x + A.ey.x * v.y, A.ex.y * v.x + A.ey.y * v.y);
|
||||
}
|
||||
|
||||
/// Multiply two rotations: q * r
|
||||
inline b2Rot b2Mul(const b2Rot& q, const b2Rot& r)
|
||||
{
|
||||
// [qc -qs] * [rc -rs] = [qc*rc-qs*rs -qc*rs-qs*rc]
|
||||
// [qs qc] [rs rc] [qs*rc+qc*rs -qs*rs+qc*rc]
|
||||
// s = qs * rc + qc * rs
|
||||
// c = qc * rc - qs * rs
|
||||
b2Rot qr;
|
||||
qr.s = q.s * r.c + q.c * r.s;
|
||||
qr.c = q.c * r.c - q.s * r.s;
|
||||
return qr;
|
||||
}
|
||||
|
||||
/// Transpose multiply two rotations: qT * r
|
||||
inline b2Rot b2MulT(const b2Rot& q, const b2Rot& r)
|
||||
{
|
||||
// [ qc qs] * [rc -rs] = [qc*rc+qs*rs -qc*rs+qs*rc]
|
||||
// [-qs qc] [rs rc] [-qs*rc+qc*rs qs*rs+qc*rc]
|
||||
// s = qc * rs - qs * rc
|
||||
// c = qc * rc + qs * rs
|
||||
b2Rot qr;
|
||||
qr.s = q.c * r.s - q.s * r.c;
|
||||
qr.c = q.c * r.c + q.s * r.s;
|
||||
return qr;
|
||||
}
|
||||
|
||||
/// Rotate a vector
|
||||
inline b2Vec2 b2Mul(const b2Rot& q, const b2Vec2& v)
|
||||
{
|
||||
return b2Vec2(q.c * v.x - q.s * v.y, q.s * v.x + q.c * v.y);
|
||||
}
|
||||
|
||||
/// Inverse rotate a vector
|
||||
inline b2Vec2 b2MulT(const b2Rot& q, const b2Vec2& v)
|
||||
{
|
||||
return b2Vec2(q.c * v.x + q.s * v.y, -q.s * v.x + q.c * v.y);
|
||||
}
|
||||
|
||||
inline b2Vec2 b2Mul(const b2Transform& T, const b2Vec2& v)
|
||||
{
|
||||
float32 x = (T.q.c * v.x - T.q.s * v.y) + T.p.x;
|
||||
float32 y = (T.q.s * v.x + T.q.c * v.y) + T.p.y;
|
||||
|
||||
return b2Vec2(x, y);
|
||||
}
|
||||
|
||||
inline b2Vec2 b2MulT(const b2Transform& T, const b2Vec2& v)
|
||||
{
|
||||
float32 px = v.x - T.p.x;
|
||||
float32 py = v.y - T.p.y;
|
||||
float32 x = (T.q.c * px + T.q.s * py);
|
||||
float32 y = (-T.q.s * px + T.q.c * py);
|
||||
|
||||
return b2Vec2(x, y);
|
||||
}
|
||||
|
||||
// v2 = A.q.Rot(B.q.Rot(v1) + B.p) + A.p
|
||||
// = (A.q * B.q).Rot(v1) + A.q.Rot(B.p) + A.p
|
||||
inline b2Transform b2Mul(const b2Transform& A, const b2Transform& B)
|
||||
{
|
||||
b2Transform C;
|
||||
C.q = b2Mul(A.q, B.q);
|
||||
C.p = b2Mul(A.q, B.p) + A.p;
|
||||
return C;
|
||||
}
|
||||
|
||||
// v2 = A.q' * (B.q * v1 + B.p - A.p)
|
||||
// = A.q' * B.q * v1 + A.q' * (B.p - A.p)
|
||||
inline b2Transform b2MulT(const b2Transform& A, const b2Transform& B)
|
||||
{
|
||||
b2Transform C;
|
||||
C.q = b2MulT(A.q, B.q);
|
||||
C.p = b2MulT(A.q, B.p - A.p);
|
||||
return C;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T b2Abs(T a)
|
||||
{
|
||||
return a > T(0) ? a : -a;
|
||||
}
|
||||
|
||||
inline b2Vec2 b2Abs(const b2Vec2& a)
|
||||
{
|
||||
return b2Vec2(b2Abs(a.x), b2Abs(a.y));
|
||||
}
|
||||
|
||||
inline b2Mat22 b2Abs(const b2Mat22& A)
|
||||
{
|
||||
return b2Mat22(b2Abs(A.ex), b2Abs(A.ey));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T b2Min(T a, T b)
|
||||
{
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
inline b2Vec2 b2Min(const b2Vec2& a, const b2Vec2& b)
|
||||
{
|
||||
return b2Vec2(b2Min(a.x, b.x), b2Min(a.y, b.y));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T b2Max(T a, T b)
|
||||
{
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
inline b2Vec2 b2Max(const b2Vec2& a, const b2Vec2& b)
|
||||
{
|
||||
return b2Vec2(b2Max(a.x, b.x), b2Max(a.y, b.y));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T b2Clamp(T a, T low, T high)
|
||||
{
|
||||
return b2Max(low, b2Min(a, high));
|
||||
}
|
||||
|
||||
inline b2Vec2 b2Clamp(const b2Vec2& a, const b2Vec2& low, const b2Vec2& high)
|
||||
{
|
||||
return b2Max(low, b2Min(a, high));
|
||||
}
|
||||
|
||||
template<typename T> inline void b2Swap(T& a, T& b)
|
||||
{
|
||||
T tmp = a;
|
||||
a = b;
|
||||
b = tmp;
|
||||
}
|
||||
|
||||
/// "Next Largest Power of 2
|
||||
/// Given a binary integer value x, the next largest power of 2 can be computed by a SWAR algorithm
|
||||
/// that recursively "folds" the upper bits into the lower bits. This process yields a bit vector with
|
||||
/// the same most significant 1 as x, but all 1's below it. Adding 1 to that value yields the next
|
||||
/// largest power of 2. For a 32-bit value:"
|
||||
inline uint32 b2NextPowerOfTwo(uint32 x)
|
||||
{
|
||||
x |= (x >> 1);
|
||||
x |= (x >> 2);
|
||||
x |= (x >> 4);
|
||||
x |= (x >> 8);
|
||||
x |= (x >> 16);
|
||||
return x + 1;
|
||||
}
|
||||
|
||||
inline bool b2IsPowerOfTwo(uint32 x)
|
||||
{
|
||||
bool result = x > 0 && (x & (x - 1)) == 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
inline void b2Sweep::GetTransform(b2Transform* xf, float32 beta) const
|
||||
{
|
||||
xf->p = (1.0f - beta) * c0 + beta * c;
|
||||
float32 angle = (1.0f - beta) * a0 + beta * a;
|
||||
xf->q.Set(angle);
|
||||
|
||||
// Shift to origin
|
||||
xf->p -= b2Mul(xf->q, localCenter);
|
||||
}
|
||||
|
||||
inline void b2Sweep::Advance(float32 alpha)
|
||||
{
|
||||
b2Assert(alpha0 < 1.0f);
|
||||
float32 beta = (alpha - alpha0) / (1.0f - alpha0);
|
||||
c0 += beta * (c - c0);
|
||||
a0 += beta * (a - a0);
|
||||
alpha0 = alpha;
|
||||
}
|
||||
|
||||
/// Normalize an angle in radians to be between -pi and pi
|
||||
inline void b2Sweep::Normalize()
|
||||
{
|
||||
float32 twoPi = 2.0f * b2_pi;
|
||||
float32 d = twoPi * floorf(a0 / twoPi);
|
||||
a0 -= d;
|
||||
a -= d;
|
||||
}
|
||||
|
||||
#endif
|
||||
155
inc/Box2D/Common/b2Settings.h
Normal file
155
inc/Box2D/Common/b2Settings.h
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_SETTINGS_H
|
||||
#define B2_SETTINGS_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <assert.h>
|
||||
#include <float.h>
|
||||
|
||||
#if !defined(NDEBUG)
|
||||
#define b2DEBUG
|
||||
#endif
|
||||
|
||||
#define B2_NOT_USED(x) ((void)(x))
|
||||
#define b2Assert(A) assert(A)
|
||||
|
||||
typedef signed char int8;
|
||||
typedef signed short int16;
|
||||
typedef signed int int32;
|
||||
typedef unsigned char uint8;
|
||||
typedef unsigned short uint16;
|
||||
typedef unsigned int uint32;
|
||||
typedef float float32;
|
||||
typedef double float64;
|
||||
|
||||
#define b2_maxFloat FLT_MAX
|
||||
#define b2_epsilon FLT_EPSILON
|
||||
#define b2_pi 3.14159265359f
|
||||
|
||||
/// @file
|
||||
/// Global tuning constants based on meters-kilograms-seconds (MKS) units.
|
||||
///
|
||||
|
||||
// Collision
|
||||
|
||||
/// The maximum number of contact points between two convex shapes. Do
|
||||
/// not change this value.
|
||||
#define b2_maxManifoldPoints 2
|
||||
|
||||
/// The maximum number of vertices on a convex polygon. You cannot increase
|
||||
/// this too much because b2BlockAllocator has a maximum object size.
|
||||
#define b2_maxPolygonVertices 8
|
||||
|
||||
/// This is used to fatten AABBs in the dynamic tree. This allows proxies
|
||||
/// to move by a small amount without triggering a tree adjustment.
|
||||
/// This is in meters.
|
||||
#define b2_aabbExtension 0.1f
|
||||
|
||||
/// This is used to fatten AABBs in the dynamic tree. This is used to predict
|
||||
/// the future position based on the current displacement.
|
||||
/// This is a dimensionless multiplier.
|
||||
#define b2_aabbMultiplier 2.0f
|
||||
|
||||
/// A small length used as a collision and constraint tolerance. Usually it is
|
||||
/// chosen to be numerically significant, but visually insignificant.
|
||||
#define b2_linearSlop 0.005f
|
||||
|
||||
/// A small angle used as a collision and constraint tolerance. Usually it is
|
||||
/// chosen to be numerically significant, but visually insignificant.
|
||||
#define b2_angularSlop (2.0f / 180.0f * b2_pi)
|
||||
|
||||
/// The radius of the polygon/edge shape skin. This should not be modified. Making
|
||||
/// this smaller means polygons will have an insufficient buffer for continuous collision.
|
||||
/// Making it larger may create artifacts for vertex collision.
|
||||
#define b2_polygonRadius (2.0f * b2_linearSlop)
|
||||
|
||||
/// Maximum number of sub-steps per contact in continuous physics simulation.
|
||||
#define b2_maxSubSteps 8
|
||||
|
||||
|
||||
// Dynamics
|
||||
|
||||
/// Maximum number of contacts to be handled to solve a TOI impact.
|
||||
#define b2_maxTOIContacts 32
|
||||
|
||||
/// A velocity threshold for elastic collisions. Any collision with a relative linear
|
||||
/// velocity below this threshold will be treated as inelastic.
|
||||
#define b2_velocityThreshold 1.0f
|
||||
|
||||
/// The maximum linear position correction used when solving constraints. This helps to
|
||||
/// prevent overshoot.
|
||||
#define b2_maxLinearCorrection 0.2f
|
||||
|
||||
/// The maximum angular position correction used when solving constraints. This helps to
|
||||
/// prevent overshoot.
|
||||
#define b2_maxAngularCorrection (8.0f / 180.0f * b2_pi)
|
||||
|
||||
/// The maximum linear velocity of a body. This limit is very large and is used
|
||||
/// to prevent numerical problems. You shouldn't need to adjust this.
|
||||
#define b2_maxTranslation 2.0f
|
||||
#define b2_maxTranslationSquared (b2_maxTranslation * b2_maxTranslation)
|
||||
|
||||
/// The maximum angular velocity of a body. This limit is very large and is used
|
||||
/// to prevent numerical problems. You shouldn't need to adjust this.
|
||||
#define b2_maxRotation (0.5f * b2_pi)
|
||||
#define b2_maxRotationSquared (b2_maxRotation * b2_maxRotation)
|
||||
|
||||
/// This scale factor controls how fast overlap is resolved. Ideally this would be 1 so
|
||||
/// that overlap is removed in one time step. However using values close to 1 often lead
|
||||
/// to overshoot.
|
||||
#define b2_baumgarte 0.2f
|
||||
#define b2_toiBaugarte 0.75f
|
||||
|
||||
|
||||
// Sleep
|
||||
|
||||
/// The time that a body must be still before it will go to sleep.
|
||||
#define b2_timeToSleep 0.5f
|
||||
|
||||
/// A body cannot sleep if its linear velocity is above this tolerance.
|
||||
#define b2_linearSleepTolerance 0.01f
|
||||
|
||||
/// A body cannot sleep if its angular velocity is above this tolerance.
|
||||
#define b2_angularSleepTolerance (2.0f / 180.0f * b2_pi)
|
||||
|
||||
// Memory Allocation
|
||||
|
||||
/// Implement this function to use your own memory allocator.
|
||||
void* b2Alloc(int32 size);
|
||||
|
||||
/// If you implement b2Alloc, you should also implement this function.
|
||||
void b2Free(void* mem);
|
||||
|
||||
/// Logging function.
|
||||
void b2Log(const char* string, ...);
|
||||
|
||||
/// Version numbering scheme.
|
||||
/// See http://en.wikipedia.org/wiki/Software_versioning
|
||||
struct b2Version
|
||||
{
|
||||
int32 major; ///< significant changes
|
||||
int32 minor; ///< incremental changes
|
||||
int32 revision; ///< bug fixes
|
||||
};
|
||||
|
||||
/// Current version.
|
||||
extern b2Version b2_version;
|
||||
|
||||
#endif
|
||||
60
inc/Box2D/Common/b2StackAllocator.h
Normal file
60
inc/Box2D/Common/b2StackAllocator.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_STACK_ALLOCATOR_H
|
||||
#define B2_STACK_ALLOCATOR_H
|
||||
|
||||
#include <Box2D/Common/b2Settings.h>
|
||||
|
||||
const int32 b2_stackSize = 100 * 1024; // 100k
|
||||
const int32 b2_maxStackEntries = 32;
|
||||
|
||||
struct b2StackEntry
|
||||
{
|
||||
char* data;
|
||||
int32 size;
|
||||
bool usedMalloc;
|
||||
};
|
||||
|
||||
// This is a stack allocator used for fast per step allocations.
|
||||
// You must nest allocate/free pairs. The code will assert
|
||||
// if you try to interleave multiple allocate/free pairs.
|
||||
class b2StackAllocator
|
||||
{
|
||||
public:
|
||||
b2StackAllocator();
|
||||
~b2StackAllocator();
|
||||
|
||||
void* Allocate(int32 size);
|
||||
void Free(void* p);
|
||||
|
||||
int32 GetMaxAllocation() const;
|
||||
|
||||
private:
|
||||
|
||||
char m_data[b2_stackSize];
|
||||
int32 m_index;
|
||||
|
||||
int32 m_allocation;
|
||||
int32 m_maxAllocation;
|
||||
|
||||
b2StackEntry m_entries[b2_maxStackEntries];
|
||||
int32 m_entryCount;
|
||||
};
|
||||
|
||||
#endif
|
||||
50
inc/Box2D/Common/b2Timer.h
Normal file
50
inc/Box2D/Common/b2Timer.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 2011 Erin Catto http://box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_TIMER_H
|
||||
#define B2_TIMER_H
|
||||
|
||||
#include <Box2D/Common/b2Settings.h>
|
||||
|
||||
/// Timer for profiling. This has platform specific code and may
|
||||
/// not work on every platform.
|
||||
class b2Timer
|
||||
{
|
||||
public:
|
||||
|
||||
/// Constructor
|
||||
b2Timer();
|
||||
|
||||
/// Reset the timer.
|
||||
void Reset();
|
||||
|
||||
/// Get the time since construction or the last reset.
|
||||
float32 GetMilliseconds() const;
|
||||
|
||||
private:
|
||||
|
||||
#if defined(_WIN32)
|
||||
float64 m_start;
|
||||
static float64 s_invFrequency;
|
||||
#elif defined(__linux__) || defined (__APPLE__)
|
||||
unsigned long m_start_sec;
|
||||
unsigned long m_start_usec;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
39
inc/Box2D/Dynamics/Contacts/b2ChainAndCircleContact.h
Normal file
39
inc/Box2D/Dynamics/Contacts/b2ChainAndCircleContact.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_CHAIN_AND_CIRCLE_CONTACT_H
|
||||
#define B2_CHAIN_AND_CIRCLE_CONTACT_H
|
||||
|
||||
#include <Box2D/Dynamics/Contacts/b2Contact.h>
|
||||
|
||||
class b2BlockAllocator;
|
||||
|
||||
class b2ChainAndCircleContact : public b2Contact
|
||||
{
|
||||
public:
|
||||
static b2Contact* Create( b2Fixture* fixtureA, int32 indexA,
|
||||
b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator);
|
||||
static void Destroy(b2Contact* contact, b2BlockAllocator* allocator);
|
||||
|
||||
b2ChainAndCircleContact(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB);
|
||||
~b2ChainAndCircleContact() {}
|
||||
|
||||
void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB);
|
||||
};
|
||||
|
||||
#endif
|
||||
39
inc/Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.h
Normal file
39
inc/Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_CHAIN_AND_POLYGON_CONTACT_H
|
||||
#define B2_CHAIN_AND_POLYGON_CONTACT_H
|
||||
|
||||
#include <Box2D/Dynamics/Contacts/b2Contact.h>
|
||||
|
||||
class b2BlockAllocator;
|
||||
|
||||
class b2ChainAndPolygonContact : public b2Contact
|
||||
{
|
||||
public:
|
||||
static b2Contact* Create( b2Fixture* fixtureA, int32 indexA,
|
||||
b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator);
|
||||
static void Destroy(b2Contact* contact, b2BlockAllocator* allocator);
|
||||
|
||||
b2ChainAndPolygonContact(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB);
|
||||
~b2ChainAndPolygonContact() {}
|
||||
|
||||
void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB);
|
||||
};
|
||||
|
||||
#endif
|
||||
39
inc/Box2D/Dynamics/Contacts/b2CircleContact.h
Normal file
39
inc/Box2D/Dynamics/Contacts/b2CircleContact.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_CIRCLE_CONTACT_H
|
||||
#define B2_CIRCLE_CONTACT_H
|
||||
|
||||
#include <Box2D/Dynamics/Contacts/b2Contact.h>
|
||||
|
||||
class b2BlockAllocator;
|
||||
|
||||
class b2CircleContact : public b2Contact
|
||||
{
|
||||
public:
|
||||
static b2Contact* Create( b2Fixture* fixtureA, int32 indexA,
|
||||
b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator);
|
||||
static void Destroy(b2Contact* contact, b2BlockAllocator* allocator);
|
||||
|
||||
b2CircleContact(b2Fixture* fixtureA, b2Fixture* fixtureB);
|
||||
~b2CircleContact() {}
|
||||
|
||||
void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB);
|
||||
};
|
||||
|
||||
#endif
|
||||
349
inc/Box2D/Dynamics/Contacts/b2Contact.h
Normal file
349
inc/Box2D/Dynamics/Contacts/b2Contact.h
Normal file
@@ -0,0 +1,349 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_CONTACT_H
|
||||
#define B2_CONTACT_H
|
||||
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
#include <Box2D/Collision/b2Collision.h>
|
||||
#include <Box2D/Collision/Shapes/b2Shape.h>
|
||||
#include <Box2D/Dynamics/b2Fixture.h>
|
||||
|
||||
class b2Body;
|
||||
class b2Contact;
|
||||
class b2Fixture;
|
||||
class b2World;
|
||||
class b2BlockAllocator;
|
||||
class b2StackAllocator;
|
||||
class b2ContactListener;
|
||||
|
||||
/// Friction mixing law. The idea is to allow either fixture to drive the restitution to zero.
|
||||
/// For example, anything slides on ice.
|
||||
inline float32 b2MixFriction(float32 friction1, float32 friction2)
|
||||
{
|
||||
return b2Sqrt(friction1 * friction2);
|
||||
}
|
||||
|
||||
/// Restitution mixing law. The idea is allow for anything to bounce off an inelastic surface.
|
||||
/// For example, a superball bounces on anything.
|
||||
inline float32 b2MixRestitution(float32 restitution1, float32 restitution2)
|
||||
{
|
||||
return restitution1 > restitution2 ? restitution1 : restitution2;
|
||||
}
|
||||
|
||||
typedef b2Contact* b2ContactCreateFcn( b2Fixture* fixtureA, int32 indexA,
|
||||
b2Fixture* fixtureB, int32 indexB,
|
||||
b2BlockAllocator* allocator);
|
||||
typedef void b2ContactDestroyFcn(b2Contact* contact, b2BlockAllocator* allocator);
|
||||
|
||||
struct b2ContactRegister
|
||||
{
|
||||
b2ContactCreateFcn* createFcn;
|
||||
b2ContactDestroyFcn* destroyFcn;
|
||||
bool primary;
|
||||
};
|
||||
|
||||
/// A contact edge is used to connect bodies and contacts together
|
||||
/// in a contact graph where each body is a node and each contact
|
||||
/// is an edge. A contact edge belongs to a doubly linked list
|
||||
/// maintained in each attached body. Each contact has two contact
|
||||
/// nodes, one for each attached body.
|
||||
struct b2ContactEdge
|
||||
{
|
||||
b2Body* other; ///< provides quick access to the other body attached.
|
||||
b2Contact* contact; ///< the contact
|
||||
b2ContactEdge* prev; ///< the previous contact edge in the body's contact list
|
||||
b2ContactEdge* next; ///< the next contact edge in the body's contact list
|
||||
};
|
||||
|
||||
/// The class manages contact between two shapes. A contact exists for each overlapping
|
||||
/// AABB in the broad-phase (except if filtered). Therefore a contact object may exist
|
||||
/// that has no contact points.
|
||||
class b2Contact
|
||||
{
|
||||
public:
|
||||
|
||||
/// Get the contact manifold. Do not modify the manifold unless you understand the
|
||||
/// internals of Box2D.
|
||||
b2Manifold* GetManifold();
|
||||
const b2Manifold* GetManifold() const;
|
||||
|
||||
/// Get the world manifold.
|
||||
void GetWorldManifold(b2WorldManifold* worldManifold) const;
|
||||
|
||||
/// Is this contact touching?
|
||||
bool IsTouching() const;
|
||||
|
||||
/// Enable/disable this contact. This can be used inside the pre-solve
|
||||
/// contact listener. The contact is only disabled for the current
|
||||
/// time step (or sub-step in continuous collisions).
|
||||
void SetEnabled(bool flag);
|
||||
|
||||
/// Has this contact been disabled?
|
||||
bool IsEnabled() const;
|
||||
|
||||
/// Get the next contact in the world's contact list.
|
||||
b2Contact* GetNext();
|
||||
const b2Contact* GetNext() const;
|
||||
|
||||
/// Get fixture A in this contact.
|
||||
b2Fixture* GetFixtureA();
|
||||
const b2Fixture* GetFixtureA() const;
|
||||
|
||||
/// Get the child primitive index for fixture A.
|
||||
int32 GetChildIndexA() const;
|
||||
|
||||
/// Get fixture B in this contact.
|
||||
b2Fixture* GetFixtureB();
|
||||
const b2Fixture* GetFixtureB() const;
|
||||
|
||||
/// Get the child primitive index for fixture B.
|
||||
int32 GetChildIndexB() const;
|
||||
|
||||
/// Override the default friction mixture. You can call this in b2ContactListener::PreSolve.
|
||||
/// This value persists until set or reset.
|
||||
void SetFriction(float32 friction);
|
||||
|
||||
/// Get the friction.
|
||||
float32 GetFriction() const;
|
||||
|
||||
/// Reset the friction mixture to the default value.
|
||||
void ResetFriction();
|
||||
|
||||
/// Override the default restitution mixture. You can call this in b2ContactListener::PreSolve.
|
||||
/// The value persists until you set or reset.
|
||||
void SetRestitution(float32 restitution);
|
||||
|
||||
/// Get the restitution.
|
||||
float32 GetRestitution() const;
|
||||
|
||||
/// Reset the restitution to the default value.
|
||||
void ResetRestitution();
|
||||
|
||||
/// Set the desired tangent speed for a conveyor belt behavior. In meters per second.
|
||||
void SetTangentSpeed(float32 speed);
|
||||
|
||||
/// Get the desired tangent speed. In meters per second.
|
||||
float32 GetTangentSpeed() const;
|
||||
|
||||
/// Evaluate this contact with your own manifold and transforms.
|
||||
virtual void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) = 0;
|
||||
|
||||
protected:
|
||||
friend class b2ContactManager;
|
||||
friend class b2World;
|
||||
friend class b2ContactSolver;
|
||||
friend class b2Body;
|
||||
friend class b2Fixture;
|
||||
|
||||
// Flags stored in m_flags
|
||||
enum
|
||||
{
|
||||
// Used when crawling contact graph when forming islands.
|
||||
e_islandFlag = 0x0001,
|
||||
|
||||
// Set when the shapes are touching.
|
||||
e_touchingFlag = 0x0002,
|
||||
|
||||
// This contact can be disabled (by user)
|
||||
e_enabledFlag = 0x0004,
|
||||
|
||||
// This contact needs filtering because a fixture filter was changed.
|
||||
e_filterFlag = 0x0008,
|
||||
|
||||
// This bullet contact had a TOI event
|
||||
e_bulletHitFlag = 0x0010,
|
||||
|
||||
// This contact has a valid TOI in m_toi
|
||||
e_toiFlag = 0x0020
|
||||
};
|
||||
|
||||
/// Flag this contact for filtering. Filtering will occur the next time step.
|
||||
void FlagForFiltering();
|
||||
|
||||
static void AddType(b2ContactCreateFcn* createFcn, b2ContactDestroyFcn* destroyFcn,
|
||||
b2Shape::Type typeA, b2Shape::Type typeB);
|
||||
static void InitializeRegisters();
|
||||
static b2Contact* Create(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator);
|
||||
static void Destroy(b2Contact* contact, b2Shape::Type typeA, b2Shape::Type typeB, b2BlockAllocator* allocator);
|
||||
static void Destroy(b2Contact* contact, b2BlockAllocator* allocator);
|
||||
|
||||
b2Contact() : m_fixtureA(NULL), m_fixtureB(NULL) {}
|
||||
b2Contact(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB);
|
||||
virtual ~b2Contact() {}
|
||||
|
||||
void Update(b2ContactListener* listener);
|
||||
|
||||
static b2ContactRegister s_registers[b2Shape::e_typeCount][b2Shape::e_typeCount];
|
||||
static bool s_initialized;
|
||||
|
||||
uint32 m_flags;
|
||||
|
||||
// World pool and list pointers.
|
||||
b2Contact* m_prev;
|
||||
b2Contact* m_next;
|
||||
|
||||
// Nodes for connecting bodies.
|
||||
b2ContactEdge m_nodeA;
|
||||
b2ContactEdge m_nodeB;
|
||||
|
||||
b2Fixture* m_fixtureA;
|
||||
b2Fixture* m_fixtureB;
|
||||
|
||||
int32 m_indexA;
|
||||
int32 m_indexB;
|
||||
|
||||
b2Manifold m_manifold;
|
||||
|
||||
int32 m_toiCount;
|
||||
float32 m_toi;
|
||||
|
||||
float32 m_friction;
|
||||
float32 m_restitution;
|
||||
|
||||
float32 m_tangentSpeed;
|
||||
};
|
||||
|
||||
inline b2Manifold* b2Contact::GetManifold()
|
||||
{
|
||||
return &m_manifold;
|
||||
}
|
||||
|
||||
inline const b2Manifold* b2Contact::GetManifold() const
|
||||
{
|
||||
return &m_manifold;
|
||||
}
|
||||
|
||||
inline void b2Contact::GetWorldManifold(b2WorldManifold* worldManifold) const
|
||||
{
|
||||
const b2Body* bodyA = m_fixtureA->GetBody();
|
||||
const b2Body* bodyB = m_fixtureB->GetBody();
|
||||
const b2Shape* shapeA = m_fixtureA->GetShape();
|
||||
const b2Shape* shapeB = m_fixtureB->GetShape();
|
||||
|
||||
worldManifold->Initialize(&m_manifold, bodyA->GetTransform(), shapeA->m_radius, bodyB->GetTransform(), shapeB->m_radius);
|
||||
}
|
||||
|
||||
inline void b2Contact::SetEnabled(bool flag)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
m_flags |= e_enabledFlag;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_flags &= ~e_enabledFlag;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool b2Contact::IsEnabled() const
|
||||
{
|
||||
return (m_flags & e_enabledFlag) == e_enabledFlag;
|
||||
}
|
||||
|
||||
inline bool b2Contact::IsTouching() const
|
||||
{
|
||||
return (m_flags & e_touchingFlag) == e_touchingFlag;
|
||||
}
|
||||
|
||||
inline b2Contact* b2Contact::GetNext()
|
||||
{
|
||||
return m_next;
|
||||
}
|
||||
|
||||
inline const b2Contact* b2Contact::GetNext() const
|
||||
{
|
||||
return m_next;
|
||||
}
|
||||
|
||||
inline b2Fixture* b2Contact::GetFixtureA()
|
||||
{
|
||||
return m_fixtureA;
|
||||
}
|
||||
|
||||
inline const b2Fixture* b2Contact::GetFixtureA() const
|
||||
{
|
||||
return m_fixtureA;
|
||||
}
|
||||
|
||||
inline b2Fixture* b2Contact::GetFixtureB()
|
||||
{
|
||||
return m_fixtureB;
|
||||
}
|
||||
|
||||
inline int32 b2Contact::GetChildIndexA() const
|
||||
{
|
||||
return m_indexA;
|
||||
}
|
||||
|
||||
inline const b2Fixture* b2Contact::GetFixtureB() const
|
||||
{
|
||||
return m_fixtureB;
|
||||
}
|
||||
|
||||
inline int32 b2Contact::GetChildIndexB() const
|
||||
{
|
||||
return m_indexB;
|
||||
}
|
||||
|
||||
inline void b2Contact::FlagForFiltering()
|
||||
{
|
||||
m_flags |= e_filterFlag;
|
||||
}
|
||||
|
||||
inline void b2Contact::SetFriction(float32 friction)
|
||||
{
|
||||
m_friction = friction;
|
||||
}
|
||||
|
||||
inline float32 b2Contact::GetFriction() const
|
||||
{
|
||||
return m_friction;
|
||||
}
|
||||
|
||||
inline void b2Contact::ResetFriction()
|
||||
{
|
||||
m_friction = b2MixFriction(m_fixtureA->m_friction, m_fixtureB->m_friction);
|
||||
}
|
||||
|
||||
inline void b2Contact::SetRestitution(float32 restitution)
|
||||
{
|
||||
m_restitution = restitution;
|
||||
}
|
||||
|
||||
inline float32 b2Contact::GetRestitution() const
|
||||
{
|
||||
return m_restitution;
|
||||
}
|
||||
|
||||
inline void b2Contact::ResetRestitution()
|
||||
{
|
||||
m_restitution = b2MixRestitution(m_fixtureA->m_restitution, m_fixtureB->m_restitution);
|
||||
}
|
||||
|
||||
inline void b2Contact::SetTangentSpeed(float32 speed)
|
||||
{
|
||||
m_tangentSpeed = speed;
|
||||
}
|
||||
|
||||
inline float32 b2Contact::GetTangentSpeed() const
|
||||
{
|
||||
return m_tangentSpeed;
|
||||
}
|
||||
|
||||
#endif
|
||||
95
inc/Box2D/Dynamics/Contacts/b2ContactSolver.h
Normal file
95
inc/Box2D/Dynamics/Contacts/b2ContactSolver.h
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_CONTACT_SOLVER_H
|
||||
#define B2_CONTACT_SOLVER_H
|
||||
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
#include <Box2D/Collision/b2Collision.h>
|
||||
#include <Box2D/Dynamics/b2TimeStep.h>
|
||||
|
||||
class b2Contact;
|
||||
class b2Body;
|
||||
class b2StackAllocator;
|
||||
struct b2ContactPositionConstraint;
|
||||
|
||||
struct b2VelocityConstraintPoint
|
||||
{
|
||||
b2Vec2 rA;
|
||||
b2Vec2 rB;
|
||||
float32 normalImpulse;
|
||||
float32 tangentImpulse;
|
||||
float32 normalMass;
|
||||
float32 tangentMass;
|
||||
float32 velocityBias;
|
||||
};
|
||||
|
||||
struct b2ContactVelocityConstraint
|
||||
{
|
||||
b2VelocityConstraintPoint points[b2_maxManifoldPoints];
|
||||
b2Vec2 normal;
|
||||
b2Mat22 normalMass;
|
||||
b2Mat22 K;
|
||||
int32 indexA;
|
||||
int32 indexB;
|
||||
float32 invMassA, invMassB;
|
||||
float32 invIA, invIB;
|
||||
float32 friction;
|
||||
float32 restitution;
|
||||
float32 tangentSpeed;
|
||||
int32 pointCount;
|
||||
int32 contactIndex;
|
||||
};
|
||||
|
||||
struct b2ContactSolverDef
|
||||
{
|
||||
b2TimeStep step;
|
||||
b2Contact** contacts;
|
||||
int32 count;
|
||||
b2Position* positions;
|
||||
b2Velocity* velocities;
|
||||
b2StackAllocator* allocator;
|
||||
};
|
||||
|
||||
class b2ContactSolver
|
||||
{
|
||||
public:
|
||||
b2ContactSolver(b2ContactSolverDef* def);
|
||||
~b2ContactSolver();
|
||||
|
||||
void InitializeVelocityConstraints();
|
||||
|
||||
void WarmStart();
|
||||
void SolveVelocityConstraints();
|
||||
void StoreImpulses();
|
||||
|
||||
bool SolvePositionConstraints();
|
||||
bool SolveTOIPositionConstraints(int32 toiIndexA, int32 toiIndexB);
|
||||
|
||||
b2TimeStep m_step;
|
||||
b2Position* m_positions;
|
||||
b2Velocity* m_velocities;
|
||||
b2StackAllocator* m_allocator;
|
||||
b2ContactPositionConstraint* m_positionConstraints;
|
||||
b2ContactVelocityConstraint* m_velocityConstraints;
|
||||
b2Contact** m_contacts;
|
||||
int m_count;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
39
inc/Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.h
Normal file
39
inc/Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_EDGE_AND_CIRCLE_CONTACT_H
|
||||
#define B2_EDGE_AND_CIRCLE_CONTACT_H
|
||||
|
||||
#include <Box2D/Dynamics/Contacts/b2Contact.h>
|
||||
|
||||
class b2BlockAllocator;
|
||||
|
||||
class b2EdgeAndCircleContact : public b2Contact
|
||||
{
|
||||
public:
|
||||
static b2Contact* Create( b2Fixture* fixtureA, int32 indexA,
|
||||
b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator);
|
||||
static void Destroy(b2Contact* contact, b2BlockAllocator* allocator);
|
||||
|
||||
b2EdgeAndCircleContact(b2Fixture* fixtureA, b2Fixture* fixtureB);
|
||||
~b2EdgeAndCircleContact() {}
|
||||
|
||||
void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB);
|
||||
};
|
||||
|
||||
#endif
|
||||
39
inc/Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.h
Normal file
39
inc/Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_EDGE_AND_POLYGON_CONTACT_H
|
||||
#define B2_EDGE_AND_POLYGON_CONTACT_H
|
||||
|
||||
#include <Box2D/Dynamics/Contacts/b2Contact.h>
|
||||
|
||||
class b2BlockAllocator;
|
||||
|
||||
class b2EdgeAndPolygonContact : public b2Contact
|
||||
{
|
||||
public:
|
||||
static b2Contact* Create( b2Fixture* fixtureA, int32 indexA,
|
||||
b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator);
|
||||
static void Destroy(b2Contact* contact, b2BlockAllocator* allocator);
|
||||
|
||||
b2EdgeAndPolygonContact(b2Fixture* fixtureA, b2Fixture* fixtureB);
|
||||
~b2EdgeAndPolygonContact() {}
|
||||
|
||||
void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB);
|
||||
};
|
||||
|
||||
#endif
|
||||
38
inc/Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.h
Normal file
38
inc/Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_POLYGON_AND_CIRCLE_CONTACT_H
|
||||
#define B2_POLYGON_AND_CIRCLE_CONTACT_H
|
||||
|
||||
#include <Box2D/Dynamics/Contacts/b2Contact.h>
|
||||
|
||||
class b2BlockAllocator;
|
||||
|
||||
class b2PolygonAndCircleContact : public b2Contact
|
||||
{
|
||||
public:
|
||||
static b2Contact* Create(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator);
|
||||
static void Destroy(b2Contact* contact, b2BlockAllocator* allocator);
|
||||
|
||||
b2PolygonAndCircleContact(b2Fixture* fixtureA, b2Fixture* fixtureB);
|
||||
~b2PolygonAndCircleContact() {}
|
||||
|
||||
void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB);
|
||||
};
|
||||
|
||||
#endif
|
||||
39
inc/Box2D/Dynamics/Contacts/b2PolygonContact.h
Normal file
39
inc/Box2D/Dynamics/Contacts/b2PolygonContact.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_POLYGON_CONTACT_H
|
||||
#define B2_POLYGON_CONTACT_H
|
||||
|
||||
#include <Box2D/Dynamics/Contacts/b2Contact.h>
|
||||
|
||||
class b2BlockAllocator;
|
||||
|
||||
class b2PolygonContact : public b2Contact
|
||||
{
|
||||
public:
|
||||
static b2Contact* Create( b2Fixture* fixtureA, int32 indexA,
|
||||
b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator);
|
||||
static void Destroy(b2Contact* contact, b2BlockAllocator* allocator);
|
||||
|
||||
b2PolygonContact(b2Fixture* fixtureA, b2Fixture* fixtureB);
|
||||
~b2PolygonContact() {}
|
||||
|
||||
void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB);
|
||||
};
|
||||
|
||||
#endif
|
||||
169
inc/Box2D/Dynamics/Joints/b2DistanceJoint.h
Normal file
169
inc/Box2D/Dynamics/Joints/b2DistanceJoint.h
Normal file
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_DISTANCE_JOINT_H
|
||||
#define B2_DISTANCE_JOINT_H
|
||||
|
||||
#include <Box2D/Dynamics/Joints/b2Joint.h>
|
||||
|
||||
/// Distance joint definition. This requires defining an
|
||||
/// anchor point on both bodies and the non-zero length of the
|
||||
/// distance joint. The definition uses local anchor points
|
||||
/// so that the initial configuration can violate the constraint
|
||||
/// slightly. This helps when saving and loading a game.
|
||||
/// @warning Do not use a zero or short length.
|
||||
struct b2DistanceJointDef : public b2JointDef
|
||||
{
|
||||
b2DistanceJointDef()
|
||||
{
|
||||
type = e_distanceJoint;
|
||||
localAnchorA.Set(0.0f, 0.0f);
|
||||
localAnchorB.Set(0.0f, 0.0f);
|
||||
length = 1.0f;
|
||||
frequencyHz = 0.0f;
|
||||
dampingRatio = 0.0f;
|
||||
}
|
||||
|
||||
/// Initialize the bodies, anchors, and length using the world
|
||||
/// anchors.
|
||||
void Initialize(b2Body* bodyA, b2Body* bodyB,
|
||||
const b2Vec2& anchorA, const b2Vec2& anchorB);
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
b2Vec2 localAnchorA;
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
b2Vec2 localAnchorB;
|
||||
|
||||
/// The natural length between the anchor points.
|
||||
float32 length;
|
||||
|
||||
/// The mass-spring-damper frequency in Hertz. A value of 0
|
||||
/// disables softness.
|
||||
float32 frequencyHz;
|
||||
|
||||
/// The damping ratio. 0 = no damping, 1 = critical damping.
|
||||
float32 dampingRatio;
|
||||
};
|
||||
|
||||
/// A distance joint constrains two points on two bodies
|
||||
/// to remain at a fixed distance from each other. You can view
|
||||
/// this as a massless, rigid rod.
|
||||
class b2DistanceJoint : public b2Joint
|
||||
{
|
||||
public:
|
||||
|
||||
b2Vec2 GetAnchorA() const;
|
||||
b2Vec2 GetAnchorB() const;
|
||||
|
||||
/// Get the reaction force given the inverse time step.
|
||||
/// Unit is N.
|
||||
b2Vec2 GetReactionForce(float32 inv_dt) const;
|
||||
|
||||
/// Get the reaction torque given the inverse time step.
|
||||
/// Unit is N*m. This is always zero for a distance joint.
|
||||
float32 GetReactionTorque(float32 inv_dt) const;
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
|
||||
|
||||
/// Set/get the natural length.
|
||||
/// Manipulating the length can lead to non-physical behavior when the frequency is zero.
|
||||
void SetLength(float32 length);
|
||||
float32 GetLength() const;
|
||||
|
||||
/// Set/get frequency in Hz.
|
||||
void SetFrequency(float32 hz);
|
||||
float32 GetFrequency() const;
|
||||
|
||||
/// Set/get damping ratio.
|
||||
void SetDampingRatio(float32 ratio);
|
||||
float32 GetDampingRatio() const;
|
||||
|
||||
/// Dump joint to dmLog
|
||||
void Dump();
|
||||
|
||||
protected:
|
||||
|
||||
friend class b2Joint;
|
||||
b2DistanceJoint(const b2DistanceJointDef* data);
|
||||
|
||||
void InitVelocityConstraints(const b2SolverData& data);
|
||||
void SolveVelocityConstraints(const b2SolverData& data);
|
||||
bool SolvePositionConstraints(const b2SolverData& data);
|
||||
|
||||
float32 m_frequencyHz;
|
||||
float32 m_dampingRatio;
|
||||
float32 m_bias;
|
||||
|
||||
// Solver shared
|
||||
b2Vec2 m_localAnchorA;
|
||||
b2Vec2 m_localAnchorB;
|
||||
float32 m_gamma;
|
||||
float32 m_impulse;
|
||||
float32 m_length;
|
||||
|
||||
// Solver temp
|
||||
int32 m_indexA;
|
||||
int32 m_indexB;
|
||||
b2Vec2 m_u;
|
||||
b2Vec2 m_rA;
|
||||
b2Vec2 m_rB;
|
||||
b2Vec2 m_localCenterA;
|
||||
b2Vec2 m_localCenterB;
|
||||
float32 m_invMassA;
|
||||
float32 m_invMassB;
|
||||
float32 m_invIA;
|
||||
float32 m_invIB;
|
||||
float32 m_mass;
|
||||
};
|
||||
|
||||
inline void b2DistanceJoint::SetLength(float32 length)
|
||||
{
|
||||
m_length = length;
|
||||
}
|
||||
|
||||
inline float32 b2DistanceJoint::GetLength() const
|
||||
{
|
||||
return m_length;
|
||||
}
|
||||
|
||||
inline void b2DistanceJoint::SetFrequency(float32 hz)
|
||||
{
|
||||
m_frequencyHz = hz;
|
||||
}
|
||||
|
||||
inline float32 b2DistanceJoint::GetFrequency() const
|
||||
{
|
||||
return m_frequencyHz;
|
||||
}
|
||||
|
||||
inline void b2DistanceJoint::SetDampingRatio(float32 ratio)
|
||||
{
|
||||
m_dampingRatio = ratio;
|
||||
}
|
||||
|
||||
inline float32 b2DistanceJoint::GetDampingRatio() const
|
||||
{
|
||||
return m_dampingRatio;
|
||||
}
|
||||
|
||||
#endif
|
||||
119
inc/Box2D/Dynamics/Joints/b2FrictionJoint.h
Normal file
119
inc/Box2D/Dynamics/Joints/b2FrictionJoint.h
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_FRICTION_JOINT_H
|
||||
#define B2_FRICTION_JOINT_H
|
||||
|
||||
#include <Box2D/Dynamics/Joints/b2Joint.h>
|
||||
|
||||
/// Friction joint definition.
|
||||
struct b2FrictionJointDef : public b2JointDef
|
||||
{
|
||||
b2FrictionJointDef()
|
||||
{
|
||||
type = e_frictionJoint;
|
||||
localAnchorA.SetZero();
|
||||
localAnchorB.SetZero();
|
||||
maxForce = 0.0f;
|
||||
maxTorque = 0.0f;
|
||||
}
|
||||
|
||||
/// Initialize the bodies, anchors, axis, and reference angle using the world
|
||||
/// anchor and world axis.
|
||||
void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor);
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
b2Vec2 localAnchorA;
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
b2Vec2 localAnchorB;
|
||||
|
||||
/// The maximum friction force in N.
|
||||
float32 maxForce;
|
||||
|
||||
/// The maximum friction torque in N-m.
|
||||
float32 maxTorque;
|
||||
};
|
||||
|
||||
/// Friction joint. This is used for top-down friction.
|
||||
/// It provides 2D translational friction and angular friction.
|
||||
class b2FrictionJoint : public b2Joint
|
||||
{
|
||||
public:
|
||||
b2Vec2 GetAnchorA() const;
|
||||
b2Vec2 GetAnchorB() const;
|
||||
|
||||
b2Vec2 GetReactionForce(float32 inv_dt) const;
|
||||
float32 GetReactionTorque(float32 inv_dt) const;
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
|
||||
|
||||
/// Set the maximum friction force in N.
|
||||
void SetMaxForce(float32 force);
|
||||
|
||||
/// Get the maximum friction force in N.
|
||||
float32 GetMaxForce() const;
|
||||
|
||||
/// Set the maximum friction torque in N*m.
|
||||
void SetMaxTorque(float32 torque);
|
||||
|
||||
/// Get the maximum friction torque in N*m.
|
||||
float32 GetMaxTorque() const;
|
||||
|
||||
/// Dump joint to dmLog
|
||||
void Dump();
|
||||
|
||||
protected:
|
||||
|
||||
friend class b2Joint;
|
||||
|
||||
b2FrictionJoint(const b2FrictionJointDef* def);
|
||||
|
||||
void InitVelocityConstraints(const b2SolverData& data);
|
||||
void SolveVelocityConstraints(const b2SolverData& data);
|
||||
bool SolvePositionConstraints(const b2SolverData& data);
|
||||
|
||||
b2Vec2 m_localAnchorA;
|
||||
b2Vec2 m_localAnchorB;
|
||||
|
||||
// Solver shared
|
||||
b2Vec2 m_linearImpulse;
|
||||
float32 m_angularImpulse;
|
||||
float32 m_maxForce;
|
||||
float32 m_maxTorque;
|
||||
|
||||
// Solver temp
|
||||
int32 m_indexA;
|
||||
int32 m_indexB;
|
||||
b2Vec2 m_rA;
|
||||
b2Vec2 m_rB;
|
||||
b2Vec2 m_localCenterA;
|
||||
b2Vec2 m_localCenterB;
|
||||
float32 m_invMassA;
|
||||
float32 m_invMassB;
|
||||
float32 m_invIA;
|
||||
float32 m_invIB;
|
||||
b2Mat22 m_linearMass;
|
||||
float32 m_angularMass;
|
||||
};
|
||||
|
||||
#endif
|
||||
125
inc/Box2D/Dynamics/Joints/b2GearJoint.h
Normal file
125
inc/Box2D/Dynamics/Joints/b2GearJoint.h
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_GEAR_JOINT_H
|
||||
#define B2_GEAR_JOINT_H
|
||||
|
||||
#include <Box2D/Dynamics/Joints/b2Joint.h>
|
||||
|
||||
/// Gear joint definition. This definition requires two existing
|
||||
/// revolute or prismatic joints (any combination will work).
|
||||
struct b2GearJointDef : public b2JointDef
|
||||
{
|
||||
b2GearJointDef()
|
||||
{
|
||||
type = e_gearJoint;
|
||||
joint1 = NULL;
|
||||
joint2 = NULL;
|
||||
ratio = 1.0f;
|
||||
}
|
||||
|
||||
/// The first revolute/prismatic joint attached to the gear joint.
|
||||
b2Joint* joint1;
|
||||
|
||||
/// The second revolute/prismatic joint attached to the gear joint.
|
||||
b2Joint* joint2;
|
||||
|
||||
/// The gear ratio.
|
||||
/// @see b2GearJoint for explanation.
|
||||
float32 ratio;
|
||||
};
|
||||
|
||||
/// A gear joint is used to connect two joints together. Either joint
|
||||
/// can be a revolute or prismatic joint. You specify a gear ratio
|
||||
/// to bind the motions together:
|
||||
/// coordinate1 + ratio * coordinate2 = constant
|
||||
/// The ratio can be negative or positive. If one joint is a revolute joint
|
||||
/// and the other joint is a prismatic joint, then the ratio will have units
|
||||
/// of length or units of 1/length.
|
||||
/// @warning You have to manually destroy the gear joint if joint1 or joint2
|
||||
/// is destroyed.
|
||||
class b2GearJoint : public b2Joint
|
||||
{
|
||||
public:
|
||||
b2Vec2 GetAnchorA() const;
|
||||
b2Vec2 GetAnchorB() const;
|
||||
|
||||
b2Vec2 GetReactionForce(float32 inv_dt) const;
|
||||
float32 GetReactionTorque(float32 inv_dt) const;
|
||||
|
||||
/// Get the first joint.
|
||||
b2Joint* GetJoint1() { return m_joint1; }
|
||||
|
||||
/// Get the second joint.
|
||||
b2Joint* GetJoint2() { return m_joint2; }
|
||||
|
||||
/// Set/Get the gear ratio.
|
||||
void SetRatio(float32 ratio);
|
||||
float32 GetRatio() const;
|
||||
|
||||
/// Dump joint to dmLog
|
||||
void Dump();
|
||||
|
||||
protected:
|
||||
|
||||
friend class b2Joint;
|
||||
b2GearJoint(const b2GearJointDef* data);
|
||||
|
||||
void InitVelocityConstraints(const b2SolverData& data);
|
||||
void SolveVelocityConstraints(const b2SolverData& data);
|
||||
bool SolvePositionConstraints(const b2SolverData& data);
|
||||
|
||||
b2Joint* m_joint1;
|
||||
b2Joint* m_joint2;
|
||||
|
||||
b2JointType m_typeA;
|
||||
b2JointType m_typeB;
|
||||
|
||||
// Body A is connected to body C
|
||||
// Body B is connected to body D
|
||||
b2Body* m_bodyC;
|
||||
b2Body* m_bodyD;
|
||||
|
||||
// Solver shared
|
||||
b2Vec2 m_localAnchorA;
|
||||
b2Vec2 m_localAnchorB;
|
||||
b2Vec2 m_localAnchorC;
|
||||
b2Vec2 m_localAnchorD;
|
||||
|
||||
b2Vec2 m_localAxisC;
|
||||
b2Vec2 m_localAxisD;
|
||||
|
||||
float32 m_referenceAngleA;
|
||||
float32 m_referenceAngleB;
|
||||
|
||||
float32 m_constant;
|
||||
float32 m_ratio;
|
||||
|
||||
float32 m_impulse;
|
||||
|
||||
// Solver temp
|
||||
int32 m_indexA, m_indexB, m_indexC, m_indexD;
|
||||
b2Vec2 m_lcA, m_lcB, m_lcC, m_lcD;
|
||||
float32 m_mA, m_mB, m_mC, m_mD;
|
||||
float32 m_iA, m_iB, m_iC, m_iD;
|
||||
b2Vec2 m_JvAC, m_JvBD;
|
||||
float32 m_JwA, m_JwB, m_JwC, m_JwD;
|
||||
float32 m_mass;
|
||||
};
|
||||
|
||||
#endif
|
||||
226
inc/Box2D/Dynamics/Joints/b2Joint.h
Normal file
226
inc/Box2D/Dynamics/Joints/b2Joint.h
Normal file
@@ -0,0 +1,226 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_JOINT_H
|
||||
#define B2_JOINT_H
|
||||
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
|
||||
class b2Body;
|
||||
class b2Joint;
|
||||
struct b2SolverData;
|
||||
class b2BlockAllocator;
|
||||
|
||||
enum b2JointType
|
||||
{
|
||||
e_unknownJoint,
|
||||
e_revoluteJoint,
|
||||
e_prismaticJoint,
|
||||
e_distanceJoint,
|
||||
e_pulleyJoint,
|
||||
e_mouseJoint,
|
||||
e_gearJoint,
|
||||
e_wheelJoint,
|
||||
e_weldJoint,
|
||||
e_frictionJoint,
|
||||
e_ropeJoint,
|
||||
e_motorJoint
|
||||
};
|
||||
|
||||
enum b2LimitState
|
||||
{
|
||||
e_inactiveLimit,
|
||||
e_atLowerLimit,
|
||||
e_atUpperLimit,
|
||||
e_equalLimits
|
||||
};
|
||||
|
||||
struct b2Jacobian
|
||||
{
|
||||
b2Vec2 linear;
|
||||
float32 angularA;
|
||||
float32 angularB;
|
||||
};
|
||||
|
||||
/// A joint edge is used to connect bodies and joints together
|
||||
/// in a joint graph where each body is a node and each joint
|
||||
/// is an edge. A joint edge belongs to a doubly linked list
|
||||
/// maintained in each attached body. Each joint has two joint
|
||||
/// nodes, one for each attached body.
|
||||
struct b2JointEdge
|
||||
{
|
||||
b2Body* other; ///< provides quick access to the other body attached.
|
||||
b2Joint* joint; ///< the joint
|
||||
b2JointEdge* prev; ///< the previous joint edge in the body's joint list
|
||||
b2JointEdge* next; ///< the next joint edge in the body's joint list
|
||||
};
|
||||
|
||||
/// Joint definitions are used to construct joints.
|
||||
struct b2JointDef
|
||||
{
|
||||
b2JointDef()
|
||||
{
|
||||
type = e_unknownJoint;
|
||||
userData = NULL;
|
||||
bodyA = NULL;
|
||||
bodyB = NULL;
|
||||
collideConnected = false;
|
||||
}
|
||||
|
||||
/// The joint type is set automatically for concrete joint types.
|
||||
b2JointType type;
|
||||
|
||||
/// Use this to attach application specific data to your joints.
|
||||
void* userData;
|
||||
|
||||
/// The first attached body.
|
||||
b2Body* bodyA;
|
||||
|
||||
/// The second attached body.
|
||||
b2Body* bodyB;
|
||||
|
||||
/// Set this flag to true if the attached bodies should collide.
|
||||
bool collideConnected;
|
||||
};
|
||||
|
||||
/// The base joint class. Joints are used to constraint two bodies together in
|
||||
/// various fashions. Some joints also feature limits and motors.
|
||||
class b2Joint
|
||||
{
|
||||
public:
|
||||
|
||||
/// Get the type of the concrete joint.
|
||||
b2JointType GetType() const;
|
||||
|
||||
/// Get the first body attached to this joint.
|
||||
b2Body* GetBodyA();
|
||||
|
||||
/// Get the second body attached to this joint.
|
||||
b2Body* GetBodyB();
|
||||
|
||||
/// Get the anchor point on bodyA in world coordinates.
|
||||
virtual b2Vec2 GetAnchorA() const = 0;
|
||||
|
||||
/// Get the anchor point on bodyB in world coordinates.
|
||||
virtual b2Vec2 GetAnchorB() const = 0;
|
||||
|
||||
/// Get the reaction force on bodyB at the joint anchor in Newtons.
|
||||
virtual b2Vec2 GetReactionForce(float32 inv_dt) const = 0;
|
||||
|
||||
/// Get the reaction torque on bodyB in N*m.
|
||||
virtual float32 GetReactionTorque(float32 inv_dt) const = 0;
|
||||
|
||||
/// Get the next joint the world joint list.
|
||||
b2Joint* GetNext();
|
||||
const b2Joint* GetNext() const;
|
||||
|
||||
/// Get the user data pointer.
|
||||
void* GetUserData() const;
|
||||
|
||||
/// Set the user data pointer.
|
||||
void SetUserData(void* data);
|
||||
|
||||
/// Short-cut function to determine if either body is inactive.
|
||||
bool IsActive() const;
|
||||
|
||||
/// Get collide connected.
|
||||
/// Note: modifying the collide connect flag won't work correctly because
|
||||
/// the flag is only checked when fixture AABBs begin to overlap.
|
||||
bool GetCollideConnected() const;
|
||||
|
||||
/// Dump this joint to the log file.
|
||||
virtual void Dump() { b2Log("// Dump is not supported for this joint type.\n"); }
|
||||
|
||||
/// Shift the origin for any points stored in world coordinates.
|
||||
virtual void ShiftOrigin(const b2Vec2& newOrigin) { B2_NOT_USED(newOrigin); }
|
||||
|
||||
protected:
|
||||
friend class b2World;
|
||||
friend class b2Body;
|
||||
friend class b2Island;
|
||||
friend class b2GearJoint;
|
||||
|
||||
static b2Joint* Create(const b2JointDef* def, b2BlockAllocator* allocator);
|
||||
static void Destroy(b2Joint* joint, b2BlockAllocator* allocator);
|
||||
|
||||
b2Joint(const b2JointDef* def);
|
||||
virtual ~b2Joint() {}
|
||||
|
||||
virtual void InitVelocityConstraints(const b2SolverData& data) = 0;
|
||||
virtual void SolveVelocityConstraints(const b2SolverData& data) = 0;
|
||||
|
||||
// This returns true if the position errors are within tolerance.
|
||||
virtual bool SolvePositionConstraints(const b2SolverData& data) = 0;
|
||||
|
||||
b2JointType m_type;
|
||||
b2Joint* m_prev;
|
||||
b2Joint* m_next;
|
||||
b2JointEdge m_edgeA;
|
||||
b2JointEdge m_edgeB;
|
||||
b2Body* m_bodyA;
|
||||
b2Body* m_bodyB;
|
||||
|
||||
int32 m_index;
|
||||
|
||||
bool m_islandFlag;
|
||||
bool m_collideConnected;
|
||||
|
||||
void* m_userData;
|
||||
};
|
||||
|
||||
inline b2JointType b2Joint::GetType() const
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
inline b2Body* b2Joint::GetBodyA()
|
||||
{
|
||||
return m_bodyA;
|
||||
}
|
||||
|
||||
inline b2Body* b2Joint::GetBodyB()
|
||||
{
|
||||
return m_bodyB;
|
||||
}
|
||||
|
||||
inline b2Joint* b2Joint::GetNext()
|
||||
{
|
||||
return m_next;
|
||||
}
|
||||
|
||||
inline const b2Joint* b2Joint::GetNext() const
|
||||
{
|
||||
return m_next;
|
||||
}
|
||||
|
||||
inline void* b2Joint::GetUserData() const
|
||||
{
|
||||
return m_userData;
|
||||
}
|
||||
|
||||
inline void b2Joint::SetUserData(void* data)
|
||||
{
|
||||
m_userData = data;
|
||||
}
|
||||
|
||||
inline bool b2Joint::GetCollideConnected() const
|
||||
{
|
||||
return m_collideConnected;
|
||||
}
|
||||
|
||||
#endif
|
||||
133
inc/Box2D/Dynamics/Joints/b2MotorJoint.h
Normal file
133
inc/Box2D/Dynamics/Joints/b2MotorJoint.h
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2012 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_MOTOR_JOINT_H
|
||||
#define B2_MOTOR_JOINT_H
|
||||
|
||||
#include <Box2D/Dynamics/Joints/b2Joint.h>
|
||||
|
||||
/// Motor joint definition.
|
||||
struct b2MotorJointDef : public b2JointDef
|
||||
{
|
||||
b2MotorJointDef()
|
||||
{
|
||||
type = e_motorJoint;
|
||||
linearOffset.SetZero();
|
||||
angularOffset = 0.0f;
|
||||
maxForce = 1.0f;
|
||||
maxTorque = 1.0f;
|
||||
correctionFactor = 0.3f;
|
||||
}
|
||||
|
||||
/// Initialize the bodies and offsets using the current transforms.
|
||||
void Initialize(b2Body* bodyA, b2Body* bodyB);
|
||||
|
||||
/// Position of bodyB minus the position of bodyA, in bodyA's frame, in meters.
|
||||
b2Vec2 linearOffset;
|
||||
|
||||
/// The bodyB angle minus bodyA angle in radians.
|
||||
float32 angularOffset;
|
||||
|
||||
/// The maximum motor force in N.
|
||||
float32 maxForce;
|
||||
|
||||
/// The maximum motor torque in N-m.
|
||||
float32 maxTorque;
|
||||
|
||||
/// Position correction factor in the range [0,1].
|
||||
float32 correctionFactor;
|
||||
};
|
||||
|
||||
/// A motor joint is used to control the relative motion
|
||||
/// between two bodies. A typical usage is to control the movement
|
||||
/// of a dynamic body with respect to the ground.
|
||||
class b2MotorJoint : public b2Joint
|
||||
{
|
||||
public:
|
||||
b2Vec2 GetAnchorA() const;
|
||||
b2Vec2 GetAnchorB() const;
|
||||
|
||||
b2Vec2 GetReactionForce(float32 inv_dt) const;
|
||||
float32 GetReactionTorque(float32 inv_dt) const;
|
||||
|
||||
/// Set/get the target linear offset, in frame A, in meters.
|
||||
void SetLinearOffset(const b2Vec2& linearOffset);
|
||||
const b2Vec2& GetLinearOffset() const;
|
||||
|
||||
/// Set/get the target angular offset, in radians.
|
||||
void SetAngularOffset(float32 angularOffset);
|
||||
float32 GetAngularOffset() const;
|
||||
|
||||
/// Set the maximum friction force in N.
|
||||
void SetMaxForce(float32 force);
|
||||
|
||||
/// Get the maximum friction force in N.
|
||||
float32 GetMaxForce() const;
|
||||
|
||||
/// Set the maximum friction torque in N*m.
|
||||
void SetMaxTorque(float32 torque);
|
||||
|
||||
/// Get the maximum friction torque in N*m.
|
||||
float32 GetMaxTorque() const;
|
||||
|
||||
/// Set the position correction factor in the range [0,1].
|
||||
void SetCorrectionFactor(float32 factor);
|
||||
|
||||
/// Get the position correction factor in the range [0,1].
|
||||
float32 GetCorrectionFactor() const;
|
||||
|
||||
/// Dump to b2Log
|
||||
void Dump();
|
||||
|
||||
protected:
|
||||
|
||||
friend class b2Joint;
|
||||
|
||||
b2MotorJoint(const b2MotorJointDef* def);
|
||||
|
||||
void InitVelocityConstraints(const b2SolverData& data);
|
||||
void SolveVelocityConstraints(const b2SolverData& data);
|
||||
bool SolvePositionConstraints(const b2SolverData& data);
|
||||
|
||||
// Solver shared
|
||||
b2Vec2 m_linearOffset;
|
||||
float32 m_angularOffset;
|
||||
b2Vec2 m_linearImpulse;
|
||||
float32 m_angularImpulse;
|
||||
float32 m_maxForce;
|
||||
float32 m_maxTorque;
|
||||
float32 m_correctionFactor;
|
||||
|
||||
// Solver temp
|
||||
int32 m_indexA;
|
||||
int32 m_indexB;
|
||||
b2Vec2 m_rA;
|
||||
b2Vec2 m_rB;
|
||||
b2Vec2 m_localCenterA;
|
||||
b2Vec2 m_localCenterB;
|
||||
b2Vec2 m_linearError;
|
||||
float32 m_angularError;
|
||||
float32 m_invMassA;
|
||||
float32 m_invMassB;
|
||||
float32 m_invIA;
|
||||
float32 m_invIB;
|
||||
b2Mat22 m_linearMass;
|
||||
float32 m_angularMass;
|
||||
};
|
||||
|
||||
#endif
|
||||
129
inc/Box2D/Dynamics/Joints/b2MouseJoint.h
Normal file
129
inc/Box2D/Dynamics/Joints/b2MouseJoint.h
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_MOUSE_JOINT_H
|
||||
#define B2_MOUSE_JOINT_H
|
||||
|
||||
#include <Box2D/Dynamics/Joints/b2Joint.h>
|
||||
|
||||
/// Mouse joint definition. This requires a world target point,
|
||||
/// tuning parameters, and the time step.
|
||||
struct b2MouseJointDef : public b2JointDef
|
||||
{
|
||||
b2MouseJointDef()
|
||||
{
|
||||
type = e_mouseJoint;
|
||||
target.Set(0.0f, 0.0f);
|
||||
maxForce = 0.0f;
|
||||
frequencyHz = 5.0f;
|
||||
dampingRatio = 0.7f;
|
||||
}
|
||||
|
||||
/// The initial world target point. This is assumed
|
||||
/// to coincide with the body anchor initially.
|
||||
b2Vec2 target;
|
||||
|
||||
/// The maximum constraint force that can be exerted
|
||||
/// to move the candidate body. Usually you will express
|
||||
/// as some multiple of the weight (multiplier * mass * gravity).
|
||||
float32 maxForce;
|
||||
|
||||
/// The response speed.
|
||||
float32 frequencyHz;
|
||||
|
||||
/// The damping ratio. 0 = no damping, 1 = critical damping.
|
||||
float32 dampingRatio;
|
||||
};
|
||||
|
||||
/// A mouse joint is used to make a point on a body track a
|
||||
/// specified world point. This a soft constraint with a maximum
|
||||
/// force. This allows the constraint to stretch and without
|
||||
/// applying huge forces.
|
||||
/// NOTE: this joint is not documented in the manual because it was
|
||||
/// developed to be used in the testbed. If you want to learn how to
|
||||
/// use the mouse joint, look at the testbed.
|
||||
class b2MouseJoint : public b2Joint
|
||||
{
|
||||
public:
|
||||
|
||||
/// Implements b2Joint.
|
||||
b2Vec2 GetAnchorA() const;
|
||||
|
||||
/// Implements b2Joint.
|
||||
b2Vec2 GetAnchorB() const;
|
||||
|
||||
/// Implements b2Joint.
|
||||
b2Vec2 GetReactionForce(float32 inv_dt) const;
|
||||
|
||||
/// Implements b2Joint.
|
||||
float32 GetReactionTorque(float32 inv_dt) const;
|
||||
|
||||
/// Use this to update the target point.
|
||||
void SetTarget(const b2Vec2& target);
|
||||
const b2Vec2& GetTarget() const;
|
||||
|
||||
/// Set/get the maximum force in Newtons.
|
||||
void SetMaxForce(float32 force);
|
||||
float32 GetMaxForce() const;
|
||||
|
||||
/// Set/get the frequency in Hertz.
|
||||
void SetFrequency(float32 hz);
|
||||
float32 GetFrequency() const;
|
||||
|
||||
/// Set/get the damping ratio (dimensionless).
|
||||
void SetDampingRatio(float32 ratio);
|
||||
float32 GetDampingRatio() const;
|
||||
|
||||
/// The mouse joint does not support dumping.
|
||||
void Dump() { b2Log("Mouse joint dumping is not supported.\n"); }
|
||||
|
||||
/// Implement b2Joint::ShiftOrigin
|
||||
void ShiftOrigin(const b2Vec2& newOrigin);
|
||||
|
||||
protected:
|
||||
friend class b2Joint;
|
||||
|
||||
b2MouseJoint(const b2MouseJointDef* def);
|
||||
|
||||
void InitVelocityConstraints(const b2SolverData& data);
|
||||
void SolveVelocityConstraints(const b2SolverData& data);
|
||||
bool SolvePositionConstraints(const b2SolverData& data);
|
||||
|
||||
b2Vec2 m_localAnchorB;
|
||||
b2Vec2 m_targetA;
|
||||
float32 m_frequencyHz;
|
||||
float32 m_dampingRatio;
|
||||
float32 m_beta;
|
||||
|
||||
// Solver shared
|
||||
b2Vec2 m_impulse;
|
||||
float32 m_maxForce;
|
||||
float32 m_gamma;
|
||||
|
||||
// Solver temp
|
||||
int32 m_indexA;
|
||||
int32 m_indexB;
|
||||
b2Vec2 m_rB;
|
||||
b2Vec2 m_localCenterB;
|
||||
float32 m_invMassB;
|
||||
float32 m_invIB;
|
||||
b2Mat22 m_mass;
|
||||
b2Vec2 m_C;
|
||||
};
|
||||
|
||||
#endif
|
||||
196
inc/Box2D/Dynamics/Joints/b2PrismaticJoint.h
Normal file
196
inc/Box2D/Dynamics/Joints/b2PrismaticJoint.h
Normal file
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_PRISMATIC_JOINT_H
|
||||
#define B2_PRISMATIC_JOINT_H
|
||||
|
||||
#include <Box2D/Dynamics/Joints/b2Joint.h>
|
||||
|
||||
/// Prismatic joint definition. This requires defining a line of
|
||||
/// motion using an axis and an anchor point. The definition uses local
|
||||
/// anchor points and a local axis so that the initial configuration
|
||||
/// can violate the constraint slightly. The joint translation is zero
|
||||
/// when the local anchor points coincide in world space. Using local
|
||||
/// anchors and a local axis helps when saving and loading a game.
|
||||
struct b2PrismaticJointDef : public b2JointDef
|
||||
{
|
||||
b2PrismaticJointDef()
|
||||
{
|
||||
type = e_prismaticJoint;
|
||||
localAnchorA.SetZero();
|
||||
localAnchorB.SetZero();
|
||||
localAxisA.Set(1.0f, 0.0f);
|
||||
referenceAngle = 0.0f;
|
||||
enableLimit = false;
|
||||
lowerTranslation = 0.0f;
|
||||
upperTranslation = 0.0f;
|
||||
enableMotor = false;
|
||||
maxMotorForce = 0.0f;
|
||||
motorSpeed = 0.0f;
|
||||
}
|
||||
|
||||
/// Initialize the bodies, anchors, axis, and reference angle using the world
|
||||
/// anchor and unit world axis.
|
||||
void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor, const b2Vec2& axis);
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
b2Vec2 localAnchorA;
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
b2Vec2 localAnchorB;
|
||||
|
||||
/// The local translation unit axis in bodyA.
|
||||
b2Vec2 localAxisA;
|
||||
|
||||
/// The constrained angle between the bodies: bodyB_angle - bodyA_angle.
|
||||
float32 referenceAngle;
|
||||
|
||||
/// Enable/disable the joint limit.
|
||||
bool enableLimit;
|
||||
|
||||
/// The lower translation limit, usually in meters.
|
||||
float32 lowerTranslation;
|
||||
|
||||
/// The upper translation limit, usually in meters.
|
||||
float32 upperTranslation;
|
||||
|
||||
/// Enable/disable the joint motor.
|
||||
bool enableMotor;
|
||||
|
||||
/// The maximum motor torque, usually in N-m.
|
||||
float32 maxMotorForce;
|
||||
|
||||
/// The desired motor speed in radians per second.
|
||||
float32 motorSpeed;
|
||||
};
|
||||
|
||||
/// A prismatic joint. This joint provides one degree of freedom: translation
|
||||
/// along an axis fixed in bodyA. Relative rotation is prevented. You can
|
||||
/// use a joint limit to restrict the range of motion and a joint motor to
|
||||
/// drive the motion or to model joint friction.
|
||||
class b2PrismaticJoint : public b2Joint
|
||||
{
|
||||
public:
|
||||
b2Vec2 GetAnchorA() const;
|
||||
b2Vec2 GetAnchorB() const;
|
||||
|
||||
b2Vec2 GetReactionForce(float32 inv_dt) const;
|
||||
float32 GetReactionTorque(float32 inv_dt) const;
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
|
||||
|
||||
/// The local joint axis relative to bodyA.
|
||||
const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; }
|
||||
|
||||
/// Get the reference angle.
|
||||
float32 GetReferenceAngle() const { return m_referenceAngle; }
|
||||
|
||||
/// Get the current joint translation, usually in meters.
|
||||
float32 GetJointTranslation() const;
|
||||
|
||||
/// Get the current joint translation speed, usually in meters per second.
|
||||
float32 GetJointSpeed() const;
|
||||
|
||||
/// Is the joint limit enabled?
|
||||
bool IsLimitEnabled() const;
|
||||
|
||||
/// Enable/disable the joint limit.
|
||||
void EnableLimit(bool flag);
|
||||
|
||||
/// Get the lower joint limit, usually in meters.
|
||||
float32 GetLowerLimit() const;
|
||||
|
||||
/// Get the upper joint limit, usually in meters.
|
||||
float32 GetUpperLimit() const;
|
||||
|
||||
/// Set the joint limits, usually in meters.
|
||||
void SetLimits(float32 lower, float32 upper);
|
||||
|
||||
/// Is the joint motor enabled?
|
||||
bool IsMotorEnabled() const;
|
||||
|
||||
/// Enable/disable the joint motor.
|
||||
void EnableMotor(bool flag);
|
||||
|
||||
/// Set the motor speed, usually in meters per second.
|
||||
void SetMotorSpeed(float32 speed);
|
||||
|
||||
/// Get the motor speed, usually in meters per second.
|
||||
float32 GetMotorSpeed() const;
|
||||
|
||||
/// Set the maximum motor force, usually in N.
|
||||
void SetMaxMotorForce(float32 force);
|
||||
float32 GetMaxMotorForce() const { return m_maxMotorForce; }
|
||||
|
||||
/// Get the current motor force given the inverse time step, usually in N.
|
||||
float32 GetMotorForce(float32 inv_dt) const;
|
||||
|
||||
/// Dump to b2Log
|
||||
void Dump();
|
||||
|
||||
protected:
|
||||
friend class b2Joint;
|
||||
friend class b2GearJoint;
|
||||
b2PrismaticJoint(const b2PrismaticJointDef* def);
|
||||
|
||||
void InitVelocityConstraints(const b2SolverData& data);
|
||||
void SolveVelocityConstraints(const b2SolverData& data);
|
||||
bool SolvePositionConstraints(const b2SolverData& data);
|
||||
|
||||
// Solver shared
|
||||
b2Vec2 m_localAnchorA;
|
||||
b2Vec2 m_localAnchorB;
|
||||
b2Vec2 m_localXAxisA;
|
||||
b2Vec2 m_localYAxisA;
|
||||
float32 m_referenceAngle;
|
||||
b2Vec3 m_impulse;
|
||||
float32 m_motorImpulse;
|
||||
float32 m_lowerTranslation;
|
||||
float32 m_upperTranslation;
|
||||
float32 m_maxMotorForce;
|
||||
float32 m_motorSpeed;
|
||||
bool m_enableLimit;
|
||||
bool m_enableMotor;
|
||||
b2LimitState m_limitState;
|
||||
|
||||
// Solver temp
|
||||
int32 m_indexA;
|
||||
int32 m_indexB;
|
||||
b2Vec2 m_localCenterA;
|
||||
b2Vec2 m_localCenterB;
|
||||
float32 m_invMassA;
|
||||
float32 m_invMassB;
|
||||
float32 m_invIA;
|
||||
float32 m_invIB;
|
||||
b2Vec2 m_axis, m_perp;
|
||||
float32 m_s1, m_s2;
|
||||
float32 m_a1, m_a2;
|
||||
b2Mat33 m_K;
|
||||
float32 m_motorMass;
|
||||
};
|
||||
|
||||
inline float32 b2PrismaticJoint::GetMotorSpeed() const
|
||||
{
|
||||
return m_motorSpeed;
|
||||
}
|
||||
|
||||
#endif
|
||||
152
inc/Box2D/Dynamics/Joints/b2PulleyJoint.h
Normal file
152
inc/Box2D/Dynamics/Joints/b2PulleyJoint.h
Normal file
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_PULLEY_JOINT_H
|
||||
#define B2_PULLEY_JOINT_H
|
||||
|
||||
#include <Box2D/Dynamics/Joints/b2Joint.h>
|
||||
|
||||
const float32 b2_minPulleyLength = 2.0f;
|
||||
|
||||
/// Pulley joint definition. This requires two ground anchors,
|
||||
/// two dynamic body anchor points, and a pulley ratio.
|
||||
struct b2PulleyJointDef : public b2JointDef
|
||||
{
|
||||
b2PulleyJointDef()
|
||||
{
|
||||
type = e_pulleyJoint;
|
||||
groundAnchorA.Set(-1.0f, 1.0f);
|
||||
groundAnchorB.Set(1.0f, 1.0f);
|
||||
localAnchorA.Set(-1.0f, 0.0f);
|
||||
localAnchorB.Set(1.0f, 0.0f);
|
||||
lengthA = 0.0f;
|
||||
lengthB = 0.0f;
|
||||
ratio = 1.0f;
|
||||
collideConnected = true;
|
||||
}
|
||||
|
||||
/// Initialize the bodies, anchors, lengths, max lengths, and ratio using the world anchors.
|
||||
void Initialize(b2Body* bodyA, b2Body* bodyB,
|
||||
const b2Vec2& groundAnchorA, const b2Vec2& groundAnchorB,
|
||||
const b2Vec2& anchorA, const b2Vec2& anchorB,
|
||||
float32 ratio);
|
||||
|
||||
/// The first ground anchor in world coordinates. This point never moves.
|
||||
b2Vec2 groundAnchorA;
|
||||
|
||||
/// The second ground anchor in world coordinates. This point never moves.
|
||||
b2Vec2 groundAnchorB;
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
b2Vec2 localAnchorA;
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
b2Vec2 localAnchorB;
|
||||
|
||||
/// The a reference length for the segment attached to bodyA.
|
||||
float32 lengthA;
|
||||
|
||||
/// The a reference length for the segment attached to bodyB.
|
||||
float32 lengthB;
|
||||
|
||||
/// The pulley ratio, used to simulate a block-and-tackle.
|
||||
float32 ratio;
|
||||
};
|
||||
|
||||
/// The pulley joint is connected to two bodies and two fixed ground points.
|
||||
/// The pulley supports a ratio such that:
|
||||
/// length1 + ratio * length2 <= constant
|
||||
/// Yes, the force transmitted is scaled by the ratio.
|
||||
/// Warning: the pulley joint can get a bit squirrelly by itself. They often
|
||||
/// work better when combined with prismatic joints. You should also cover the
|
||||
/// the anchor points with static shapes to prevent one side from going to
|
||||
/// zero length.
|
||||
class b2PulleyJoint : public b2Joint
|
||||
{
|
||||
public:
|
||||
b2Vec2 GetAnchorA() const;
|
||||
b2Vec2 GetAnchorB() const;
|
||||
|
||||
b2Vec2 GetReactionForce(float32 inv_dt) const;
|
||||
float32 GetReactionTorque(float32 inv_dt) const;
|
||||
|
||||
/// Get the first ground anchor.
|
||||
b2Vec2 GetGroundAnchorA() const;
|
||||
|
||||
/// Get the second ground anchor.
|
||||
b2Vec2 GetGroundAnchorB() const;
|
||||
|
||||
/// Get the current length of the segment attached to bodyA.
|
||||
float32 GetLengthA() const;
|
||||
|
||||
/// Get the current length of the segment attached to bodyB.
|
||||
float32 GetLengthB() const;
|
||||
|
||||
/// Get the pulley ratio.
|
||||
float32 GetRatio() const;
|
||||
|
||||
/// Get the current length of the segment attached to bodyA.
|
||||
float32 GetCurrentLengthA() const;
|
||||
|
||||
/// Get the current length of the segment attached to bodyB.
|
||||
float32 GetCurrentLengthB() const;
|
||||
|
||||
/// Dump joint to dmLog
|
||||
void Dump();
|
||||
|
||||
/// Implement b2Joint::ShiftOrigin
|
||||
void ShiftOrigin(const b2Vec2& newOrigin);
|
||||
|
||||
protected:
|
||||
|
||||
friend class b2Joint;
|
||||
b2PulleyJoint(const b2PulleyJointDef* data);
|
||||
|
||||
void InitVelocityConstraints(const b2SolverData& data);
|
||||
void SolveVelocityConstraints(const b2SolverData& data);
|
||||
bool SolvePositionConstraints(const b2SolverData& data);
|
||||
|
||||
b2Vec2 m_groundAnchorA;
|
||||
b2Vec2 m_groundAnchorB;
|
||||
float32 m_lengthA;
|
||||
float32 m_lengthB;
|
||||
|
||||
// Solver shared
|
||||
b2Vec2 m_localAnchorA;
|
||||
b2Vec2 m_localAnchorB;
|
||||
float32 m_constant;
|
||||
float32 m_ratio;
|
||||
float32 m_impulse;
|
||||
|
||||
// Solver temp
|
||||
int32 m_indexA;
|
||||
int32 m_indexB;
|
||||
b2Vec2 m_uA;
|
||||
b2Vec2 m_uB;
|
||||
b2Vec2 m_rA;
|
||||
b2Vec2 m_rB;
|
||||
b2Vec2 m_localCenterA;
|
||||
b2Vec2 m_localCenterB;
|
||||
float32 m_invMassA;
|
||||
float32 m_invMassB;
|
||||
float32 m_invIA;
|
||||
float32 m_invIB;
|
||||
float32 m_mass;
|
||||
};
|
||||
|
||||
#endif
|
||||
204
inc/Box2D/Dynamics/Joints/b2RevoluteJoint.h
Normal file
204
inc/Box2D/Dynamics/Joints/b2RevoluteJoint.h
Normal file
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_REVOLUTE_JOINT_H
|
||||
#define B2_REVOLUTE_JOINT_H
|
||||
|
||||
#include <Box2D/Dynamics/Joints/b2Joint.h>
|
||||
|
||||
/// Revolute joint definition. This requires defining an
|
||||
/// anchor point where the bodies are joined. The definition
|
||||
/// uses local anchor points so that the initial configuration
|
||||
/// can violate the constraint slightly. You also need to
|
||||
/// specify the initial relative angle for joint limits. This
|
||||
/// helps when saving and loading a game.
|
||||
/// The local anchor points are measured from the body's origin
|
||||
/// rather than the center of mass because:
|
||||
/// 1. you might not know where the center of mass will be.
|
||||
/// 2. if you add/remove shapes from a body and recompute the mass,
|
||||
/// the joints will be broken.
|
||||
struct b2RevoluteJointDef : public b2JointDef
|
||||
{
|
||||
b2RevoluteJointDef()
|
||||
{
|
||||
type = e_revoluteJoint;
|
||||
localAnchorA.Set(0.0f, 0.0f);
|
||||
localAnchorB.Set(0.0f, 0.0f);
|
||||
referenceAngle = 0.0f;
|
||||
lowerAngle = 0.0f;
|
||||
upperAngle = 0.0f;
|
||||
maxMotorTorque = 0.0f;
|
||||
motorSpeed = 0.0f;
|
||||
enableLimit = false;
|
||||
enableMotor = false;
|
||||
}
|
||||
|
||||
/// Initialize the bodies, anchors, and reference angle using a world
|
||||
/// anchor point.
|
||||
void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor);
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
b2Vec2 localAnchorA;
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
b2Vec2 localAnchorB;
|
||||
|
||||
/// The bodyB angle minus bodyA angle in the reference state (radians).
|
||||
float32 referenceAngle;
|
||||
|
||||
/// A flag to enable joint limits.
|
||||
bool enableLimit;
|
||||
|
||||
/// The lower angle for the joint limit (radians).
|
||||
float32 lowerAngle;
|
||||
|
||||
/// The upper angle for the joint limit (radians).
|
||||
float32 upperAngle;
|
||||
|
||||
/// A flag to enable the joint motor.
|
||||
bool enableMotor;
|
||||
|
||||
/// The desired motor speed. Usually in radians per second.
|
||||
float32 motorSpeed;
|
||||
|
||||
/// The maximum motor torque used to achieve the desired motor speed.
|
||||
/// Usually in N-m.
|
||||
float32 maxMotorTorque;
|
||||
};
|
||||
|
||||
/// A revolute joint constrains two bodies to share a common point while they
|
||||
/// are free to rotate about the point. The relative rotation about the shared
|
||||
/// point is the joint angle. You can limit the relative rotation with
|
||||
/// a joint limit that specifies a lower and upper angle. You can use a motor
|
||||
/// to drive the relative rotation about the shared point. A maximum motor torque
|
||||
/// is provided so that infinite forces are not generated.
|
||||
class b2RevoluteJoint : public b2Joint
|
||||
{
|
||||
public:
|
||||
b2Vec2 GetAnchorA() const;
|
||||
b2Vec2 GetAnchorB() const;
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
|
||||
|
||||
/// Get the reference angle.
|
||||
float32 GetReferenceAngle() const { return m_referenceAngle; }
|
||||
|
||||
/// Get the current joint angle in radians.
|
||||
float32 GetJointAngle() const;
|
||||
|
||||
/// Get the current joint angle speed in radians per second.
|
||||
float32 GetJointSpeed() const;
|
||||
|
||||
/// Is the joint limit enabled?
|
||||
bool IsLimitEnabled() const;
|
||||
|
||||
/// Enable/disable the joint limit.
|
||||
void EnableLimit(bool flag);
|
||||
|
||||
/// Get the lower joint limit in radians.
|
||||
float32 GetLowerLimit() const;
|
||||
|
||||
/// Get the upper joint limit in radians.
|
||||
float32 GetUpperLimit() const;
|
||||
|
||||
/// Set the joint limits in radians.
|
||||
void SetLimits(float32 lower, float32 upper);
|
||||
|
||||
/// Is the joint motor enabled?
|
||||
bool IsMotorEnabled() const;
|
||||
|
||||
/// Enable/disable the joint motor.
|
||||
void EnableMotor(bool flag);
|
||||
|
||||
/// Set the motor speed in radians per second.
|
||||
void SetMotorSpeed(float32 speed);
|
||||
|
||||
/// Get the motor speed in radians per second.
|
||||
float32 GetMotorSpeed() const;
|
||||
|
||||
/// Set the maximum motor torque, usually in N-m.
|
||||
void SetMaxMotorTorque(float32 torque);
|
||||
float32 GetMaxMotorTorque() const { return m_maxMotorTorque; }
|
||||
|
||||
/// Get the reaction force given the inverse time step.
|
||||
/// Unit is N.
|
||||
b2Vec2 GetReactionForce(float32 inv_dt) const;
|
||||
|
||||
/// Get the reaction torque due to the joint limit given the inverse time step.
|
||||
/// Unit is N*m.
|
||||
float32 GetReactionTorque(float32 inv_dt) const;
|
||||
|
||||
/// Get the current motor torque given the inverse time step.
|
||||
/// Unit is N*m.
|
||||
float32 GetMotorTorque(float32 inv_dt) const;
|
||||
|
||||
/// Dump to b2Log.
|
||||
void Dump();
|
||||
|
||||
protected:
|
||||
|
||||
friend class b2Joint;
|
||||
friend class b2GearJoint;
|
||||
|
||||
b2RevoluteJoint(const b2RevoluteJointDef* def);
|
||||
|
||||
void InitVelocityConstraints(const b2SolverData& data);
|
||||
void SolveVelocityConstraints(const b2SolverData& data);
|
||||
bool SolvePositionConstraints(const b2SolverData& data);
|
||||
|
||||
// Solver shared
|
||||
b2Vec2 m_localAnchorA;
|
||||
b2Vec2 m_localAnchorB;
|
||||
b2Vec3 m_impulse;
|
||||
float32 m_motorImpulse;
|
||||
|
||||
bool m_enableMotor;
|
||||
float32 m_maxMotorTorque;
|
||||
float32 m_motorSpeed;
|
||||
|
||||
bool m_enableLimit;
|
||||
float32 m_referenceAngle;
|
||||
float32 m_lowerAngle;
|
||||
float32 m_upperAngle;
|
||||
|
||||
// Solver temp
|
||||
int32 m_indexA;
|
||||
int32 m_indexB;
|
||||
b2Vec2 m_rA;
|
||||
b2Vec2 m_rB;
|
||||
b2Vec2 m_localCenterA;
|
||||
b2Vec2 m_localCenterB;
|
||||
float32 m_invMassA;
|
||||
float32 m_invMassB;
|
||||
float32 m_invIA;
|
||||
float32 m_invIB;
|
||||
b2Mat33 m_mass; // effective mass for point-to-point constraint.
|
||||
float32 m_motorMass; // effective mass for motor/limit angular constraint.
|
||||
b2LimitState m_limitState;
|
||||
};
|
||||
|
||||
inline float32 b2RevoluteJoint::GetMotorSpeed() const
|
||||
{
|
||||
return m_motorSpeed;
|
||||
}
|
||||
|
||||
#endif
|
||||
114
inc/Box2D/Dynamics/Joints/b2RopeJoint.h
Normal file
114
inc/Box2D/Dynamics/Joints/b2RopeJoint.h
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_ROPE_JOINT_H
|
||||
#define B2_ROPE_JOINT_H
|
||||
|
||||
#include <Box2D/Dynamics/Joints/b2Joint.h>
|
||||
|
||||
/// Rope joint definition. This requires two body anchor points and
|
||||
/// a maximum lengths.
|
||||
/// Note: by default the connected objects will not collide.
|
||||
/// see collideConnected in b2JointDef.
|
||||
struct b2RopeJointDef : public b2JointDef
|
||||
{
|
||||
b2RopeJointDef()
|
||||
{
|
||||
type = e_ropeJoint;
|
||||
localAnchorA.Set(-1.0f, 0.0f);
|
||||
localAnchorB.Set(1.0f, 0.0f);
|
||||
maxLength = 0.0f;
|
||||
}
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
b2Vec2 localAnchorA;
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
b2Vec2 localAnchorB;
|
||||
|
||||
/// The maximum length of the rope.
|
||||
/// Warning: this must be larger than b2_linearSlop or
|
||||
/// the joint will have no effect.
|
||||
float32 maxLength;
|
||||
};
|
||||
|
||||
/// A rope joint enforces a maximum distance between two points
|
||||
/// on two bodies. It has no other effect.
|
||||
/// Warning: if you attempt to change the maximum length during
|
||||
/// the simulation you will get some non-physical behavior.
|
||||
/// A model that would allow you to dynamically modify the length
|
||||
/// would have some sponginess, so I chose not to implement it
|
||||
/// that way. See b2DistanceJoint if you want to dynamically
|
||||
/// control length.
|
||||
class b2RopeJoint : public b2Joint
|
||||
{
|
||||
public:
|
||||
b2Vec2 GetAnchorA() const;
|
||||
b2Vec2 GetAnchorB() const;
|
||||
|
||||
b2Vec2 GetReactionForce(float32 inv_dt) const;
|
||||
float32 GetReactionTorque(float32 inv_dt) const;
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
|
||||
|
||||
/// Set/Get the maximum length of the rope.
|
||||
void SetMaxLength(float32 length) { m_maxLength = length; }
|
||||
float32 GetMaxLength() const;
|
||||
|
||||
b2LimitState GetLimitState() const;
|
||||
|
||||
/// Dump joint to dmLog
|
||||
void Dump();
|
||||
|
||||
protected:
|
||||
|
||||
friend class b2Joint;
|
||||
b2RopeJoint(const b2RopeJointDef* data);
|
||||
|
||||
void InitVelocityConstraints(const b2SolverData& data);
|
||||
void SolveVelocityConstraints(const b2SolverData& data);
|
||||
bool SolvePositionConstraints(const b2SolverData& data);
|
||||
|
||||
// Solver shared
|
||||
b2Vec2 m_localAnchorA;
|
||||
b2Vec2 m_localAnchorB;
|
||||
float32 m_maxLength;
|
||||
float32 m_length;
|
||||
float32 m_impulse;
|
||||
|
||||
// Solver temp
|
||||
int32 m_indexA;
|
||||
int32 m_indexB;
|
||||
b2Vec2 m_u;
|
||||
b2Vec2 m_rA;
|
||||
b2Vec2 m_rB;
|
||||
b2Vec2 m_localCenterA;
|
||||
b2Vec2 m_localCenterB;
|
||||
float32 m_invMassA;
|
||||
float32 m_invMassB;
|
||||
float32 m_invIA;
|
||||
float32 m_invIB;
|
||||
float32 m_mass;
|
||||
b2LimitState m_state;
|
||||
};
|
||||
|
||||
#endif
|
||||
126
inc/Box2D/Dynamics/Joints/b2WeldJoint.h
Normal file
126
inc/Box2D/Dynamics/Joints/b2WeldJoint.h
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_WELD_JOINT_H
|
||||
#define B2_WELD_JOINT_H
|
||||
|
||||
#include <Box2D/Dynamics/Joints/b2Joint.h>
|
||||
|
||||
/// Weld joint definition. You need to specify local anchor points
|
||||
/// where they are attached and the relative body angle. The position
|
||||
/// of the anchor points is important for computing the reaction torque.
|
||||
struct b2WeldJointDef : public b2JointDef
|
||||
{
|
||||
b2WeldJointDef()
|
||||
{
|
||||
type = e_weldJoint;
|
||||
localAnchorA.Set(0.0f, 0.0f);
|
||||
localAnchorB.Set(0.0f, 0.0f);
|
||||
referenceAngle = 0.0f;
|
||||
frequencyHz = 0.0f;
|
||||
dampingRatio = 0.0f;
|
||||
}
|
||||
|
||||
/// Initialize the bodies, anchors, and reference angle using a world
|
||||
/// anchor point.
|
||||
void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor);
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
b2Vec2 localAnchorA;
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
b2Vec2 localAnchorB;
|
||||
|
||||
/// The bodyB angle minus bodyA angle in the reference state (radians).
|
||||
float32 referenceAngle;
|
||||
|
||||
/// The mass-spring-damper frequency in Hertz. Rotation only.
|
||||
/// Disable softness with a value of 0.
|
||||
float32 frequencyHz;
|
||||
|
||||
/// The damping ratio. 0 = no damping, 1 = critical damping.
|
||||
float32 dampingRatio;
|
||||
};
|
||||
|
||||
/// A weld joint essentially glues two bodies together. A weld joint may
|
||||
/// distort somewhat because the island constraint solver is approximate.
|
||||
class b2WeldJoint : public b2Joint
|
||||
{
|
||||
public:
|
||||
b2Vec2 GetAnchorA() const;
|
||||
b2Vec2 GetAnchorB() const;
|
||||
|
||||
b2Vec2 GetReactionForce(float32 inv_dt) const;
|
||||
float32 GetReactionTorque(float32 inv_dt) const;
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
|
||||
|
||||
/// Get the reference angle.
|
||||
float32 GetReferenceAngle() const { return m_referenceAngle; }
|
||||
|
||||
/// Set/get frequency in Hz.
|
||||
void SetFrequency(float32 hz) { m_frequencyHz = hz; }
|
||||
float32 GetFrequency() const { return m_frequencyHz; }
|
||||
|
||||
/// Set/get damping ratio.
|
||||
void SetDampingRatio(float32 ratio) { m_dampingRatio = ratio; }
|
||||
float32 GetDampingRatio() const { return m_dampingRatio; }
|
||||
|
||||
/// Dump to b2Log
|
||||
void Dump();
|
||||
|
||||
protected:
|
||||
|
||||
friend class b2Joint;
|
||||
|
||||
b2WeldJoint(const b2WeldJointDef* def);
|
||||
|
||||
void InitVelocityConstraints(const b2SolverData& data);
|
||||
void SolveVelocityConstraints(const b2SolverData& data);
|
||||
bool SolvePositionConstraints(const b2SolverData& data);
|
||||
|
||||
float32 m_frequencyHz;
|
||||
float32 m_dampingRatio;
|
||||
float32 m_bias;
|
||||
|
||||
// Solver shared
|
||||
b2Vec2 m_localAnchorA;
|
||||
b2Vec2 m_localAnchorB;
|
||||
float32 m_referenceAngle;
|
||||
float32 m_gamma;
|
||||
b2Vec3 m_impulse;
|
||||
|
||||
// Solver temp
|
||||
int32 m_indexA;
|
||||
int32 m_indexB;
|
||||
b2Vec2 m_rA;
|
||||
b2Vec2 m_rB;
|
||||
b2Vec2 m_localCenterA;
|
||||
b2Vec2 m_localCenterB;
|
||||
float32 m_invMassA;
|
||||
float32 m_invMassB;
|
||||
float32 m_invIA;
|
||||
float32 m_invIB;
|
||||
b2Mat33 m_mass;
|
||||
};
|
||||
|
||||
#endif
|
||||
216
inc/Box2D/Dynamics/Joints/b2WheelJoint.h
Normal file
216
inc/Box2D/Dynamics/Joints/b2WheelJoint.h
Normal file
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_WHEEL_JOINT_H
|
||||
#define B2_WHEEL_JOINT_H
|
||||
|
||||
#include <Box2D/Dynamics/Joints/b2Joint.h>
|
||||
|
||||
/// Wheel joint definition. This requires defining a line of
|
||||
/// motion using an axis and an anchor point. The definition uses local
|
||||
/// anchor points and a local axis so that the initial configuration
|
||||
/// can violate the constraint slightly. The joint translation is zero
|
||||
/// when the local anchor points coincide in world space. Using local
|
||||
/// anchors and a local axis helps when saving and loading a game.
|
||||
struct b2WheelJointDef : public b2JointDef
|
||||
{
|
||||
b2WheelJointDef()
|
||||
{
|
||||
type = e_wheelJoint;
|
||||
localAnchorA.SetZero();
|
||||
localAnchorB.SetZero();
|
||||
localAxisA.Set(1.0f, 0.0f);
|
||||
enableMotor = false;
|
||||
maxMotorTorque = 0.0f;
|
||||
motorSpeed = 0.0f;
|
||||
frequencyHz = 2.0f;
|
||||
dampingRatio = 0.7f;
|
||||
}
|
||||
|
||||
/// Initialize the bodies, anchors, axis, and reference angle using the world
|
||||
/// anchor and world axis.
|
||||
void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor, const b2Vec2& axis);
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
b2Vec2 localAnchorA;
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
b2Vec2 localAnchorB;
|
||||
|
||||
/// The local translation axis in bodyA.
|
||||
b2Vec2 localAxisA;
|
||||
|
||||
/// Enable/disable the joint motor.
|
||||
bool enableMotor;
|
||||
|
||||
/// The maximum motor torque, usually in N-m.
|
||||
float32 maxMotorTorque;
|
||||
|
||||
/// The desired motor speed in radians per second.
|
||||
float32 motorSpeed;
|
||||
|
||||
/// Suspension frequency, zero indicates no suspension
|
||||
float32 frequencyHz;
|
||||
|
||||
/// Suspension damping ratio, one indicates critical damping
|
||||
float32 dampingRatio;
|
||||
};
|
||||
|
||||
/// A wheel joint. This joint provides two degrees of freedom: translation
|
||||
/// along an axis fixed in bodyA and rotation in the plane. In other words, it is a point to
|
||||
/// line constraint with a rotational motor and a linear spring/damper.
|
||||
/// This joint is designed for vehicle suspensions.
|
||||
class b2WheelJoint : public b2Joint
|
||||
{
|
||||
public:
|
||||
b2Vec2 GetAnchorA() const;
|
||||
b2Vec2 GetAnchorB() const;
|
||||
|
||||
b2Vec2 GetReactionForce(float32 inv_dt) const;
|
||||
float32 GetReactionTorque(float32 inv_dt) const;
|
||||
|
||||
/// The local anchor point relative to bodyA's origin.
|
||||
const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
|
||||
|
||||
/// The local anchor point relative to bodyB's origin.
|
||||
const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
|
||||
|
||||
/// The local joint axis relative to bodyA.
|
||||
const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; }
|
||||
|
||||
/// Get the current joint translation, usually in meters.
|
||||
float32 GetJointTranslation() const;
|
||||
|
||||
/// Get the current joint linear speed, usually in meters per second.
|
||||
float32 GetJointLinearSpeed() const;
|
||||
|
||||
/// Get the current joint angle in radians.
|
||||
float32 GetJointAngle() const;
|
||||
|
||||
/// Get the current joint angular speed in radians per second.
|
||||
float32 GetJointAngularSpeed() const;
|
||||
|
||||
/// Is the joint motor enabled?
|
||||
bool IsMotorEnabled() const;
|
||||
|
||||
/// Enable/disable the joint motor.
|
||||
void EnableMotor(bool flag);
|
||||
|
||||
/// Set the motor speed, usually in radians per second.
|
||||
void SetMotorSpeed(float32 speed);
|
||||
|
||||
/// Get the motor speed, usually in radians per second.
|
||||
float32 GetMotorSpeed() const;
|
||||
|
||||
/// Set/Get the maximum motor force, usually in N-m.
|
||||
void SetMaxMotorTorque(float32 torque);
|
||||
float32 GetMaxMotorTorque() const;
|
||||
|
||||
/// Get the current motor torque given the inverse time step, usually in N-m.
|
||||
float32 GetMotorTorque(float32 inv_dt) const;
|
||||
|
||||
/// Set/Get the spring frequency in hertz. Setting the frequency to zero disables the spring.
|
||||
void SetSpringFrequencyHz(float32 hz);
|
||||
float32 GetSpringFrequencyHz() const;
|
||||
|
||||
/// Set/Get the spring damping ratio
|
||||
void SetSpringDampingRatio(float32 ratio);
|
||||
float32 GetSpringDampingRatio() const;
|
||||
|
||||
/// Dump to b2Log
|
||||
void Dump();
|
||||
|
||||
protected:
|
||||
|
||||
friend class b2Joint;
|
||||
b2WheelJoint(const b2WheelJointDef* def);
|
||||
|
||||
void InitVelocityConstraints(const b2SolverData& data);
|
||||
void SolveVelocityConstraints(const b2SolverData& data);
|
||||
bool SolvePositionConstraints(const b2SolverData& data);
|
||||
|
||||
float32 m_frequencyHz;
|
||||
float32 m_dampingRatio;
|
||||
|
||||
// Solver shared
|
||||
b2Vec2 m_localAnchorA;
|
||||
b2Vec2 m_localAnchorB;
|
||||
b2Vec2 m_localXAxisA;
|
||||
b2Vec2 m_localYAxisA;
|
||||
|
||||
float32 m_impulse;
|
||||
float32 m_motorImpulse;
|
||||
float32 m_springImpulse;
|
||||
|
||||
float32 m_maxMotorTorque;
|
||||
float32 m_motorSpeed;
|
||||
bool m_enableMotor;
|
||||
|
||||
// Solver temp
|
||||
int32 m_indexA;
|
||||
int32 m_indexB;
|
||||
b2Vec2 m_localCenterA;
|
||||
b2Vec2 m_localCenterB;
|
||||
float32 m_invMassA;
|
||||
float32 m_invMassB;
|
||||
float32 m_invIA;
|
||||
float32 m_invIB;
|
||||
|
||||
b2Vec2 m_ax, m_ay;
|
||||
float32 m_sAx, m_sBx;
|
||||
float32 m_sAy, m_sBy;
|
||||
|
||||
float32 m_mass;
|
||||
float32 m_motorMass;
|
||||
float32 m_springMass;
|
||||
|
||||
float32 m_bias;
|
||||
float32 m_gamma;
|
||||
};
|
||||
|
||||
inline float32 b2WheelJoint::GetMotorSpeed() const
|
||||
{
|
||||
return m_motorSpeed;
|
||||
}
|
||||
|
||||
inline float32 b2WheelJoint::GetMaxMotorTorque() const
|
||||
{
|
||||
return m_maxMotorTorque;
|
||||
}
|
||||
|
||||
inline void b2WheelJoint::SetSpringFrequencyHz(float32 hz)
|
||||
{
|
||||
m_frequencyHz = hz;
|
||||
}
|
||||
|
||||
inline float32 b2WheelJoint::GetSpringFrequencyHz() const
|
||||
{
|
||||
return m_frequencyHz;
|
||||
}
|
||||
|
||||
inline void b2WheelJoint::SetSpringDampingRatio(float32 ratio)
|
||||
{
|
||||
m_dampingRatio = ratio;
|
||||
}
|
||||
|
||||
inline float32 b2WheelJoint::GetSpringDampingRatio() const
|
||||
{
|
||||
return m_dampingRatio;
|
||||
}
|
||||
|
||||
#endif
|
||||
883
inc/Box2D/Dynamics/b2Body.h
Normal file
883
inc/Box2D/Dynamics/b2Body.h
Normal file
@@ -0,0 +1,883 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_BODY_H
|
||||
#define B2_BODY_H
|
||||
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
#include <Box2D/Collision/Shapes/b2Shape.h>
|
||||
#include <memory>
|
||||
|
||||
class b2Fixture;
|
||||
class b2Joint;
|
||||
class b2Contact;
|
||||
class b2Controller;
|
||||
class b2World;
|
||||
struct b2FixtureDef;
|
||||
struct b2JointEdge;
|
||||
struct b2ContactEdge;
|
||||
|
||||
/// The body type.
|
||||
/// static: zero mass, zero velocity, may be manually moved
|
||||
/// kinematic: zero mass, non-zero velocity set by user, moved by solver
|
||||
/// dynamic: positive mass, non-zero velocity determined by forces, moved by solver
|
||||
enum b2BodyType
|
||||
{
|
||||
b2_staticBody = 0,
|
||||
b2_kinematicBody,
|
||||
b2_dynamicBody
|
||||
|
||||
// TODO_ERIN
|
||||
//b2_bulletBody,
|
||||
};
|
||||
|
||||
/// A body definition holds all the data needed to construct a rigid body.
|
||||
/// You can safely re-use body definitions. Shapes are added to a body after construction.
|
||||
struct b2BodyDef
|
||||
{
|
||||
/// This constructor sets the body definition default values.
|
||||
b2BodyDef()
|
||||
{
|
||||
userData = NULL;
|
||||
position.Set(0.0f, 0.0f);
|
||||
angle = 0.0f;
|
||||
linearVelocity.Set(0.0f, 0.0f);
|
||||
angularVelocity = 0.0f;
|
||||
linearDamping = 0.0f;
|
||||
angularDamping = 0.0f;
|
||||
allowSleep = true;
|
||||
awake = true;
|
||||
fixedRotation = false;
|
||||
bullet = false;
|
||||
type = b2_staticBody;
|
||||
active = true;
|
||||
gravityScale = 1.0f;
|
||||
}
|
||||
|
||||
/// The body type: static, kinematic, or dynamic.
|
||||
/// Note: if a dynamic body would have zero mass, the mass is set to one.
|
||||
b2BodyType type;
|
||||
|
||||
/// The world position of the body. Avoid creating bodies at the origin
|
||||
/// since this can lead to many overlapping shapes.
|
||||
b2Vec2 position;
|
||||
|
||||
/// The world angle of the body in radians.
|
||||
float32 angle;
|
||||
|
||||
/// The linear velocity of the body's origin in world co-ordinates.
|
||||
b2Vec2 linearVelocity;
|
||||
|
||||
/// The angular velocity of the body.
|
||||
float32 angularVelocity;
|
||||
|
||||
/// Linear damping is use to reduce the linear velocity. The damping parameter
|
||||
/// can be larger than 1.0f but the damping effect becomes sensitive to the
|
||||
/// time step when the damping parameter is large.
|
||||
float32 linearDamping;
|
||||
|
||||
/// Angular damping is use to reduce the angular velocity. The damping parameter
|
||||
/// can be larger than 1.0f but the damping effect becomes sensitive to the
|
||||
/// time step when the damping parameter is large.
|
||||
float32 angularDamping;
|
||||
|
||||
/// Set this flag to false if this body should never fall asleep. Note that
|
||||
/// this increases CPU usage.
|
||||
bool allowSleep;
|
||||
|
||||
/// Is this body initially awake or sleeping?
|
||||
bool awake;
|
||||
|
||||
/// Should this body be prevented from rotating? Useful for characters.
|
||||
bool fixedRotation;
|
||||
|
||||
/// Is this a fast moving body that should be prevented from tunneling through
|
||||
/// other moving bodies? Note that all bodies are prevented from tunneling through
|
||||
/// kinematic and static bodies. This setting is only considered on dynamic bodies.
|
||||
/// @warning You should use this flag sparingly since it increases processing time.
|
||||
bool bullet;
|
||||
|
||||
/// Does this body start out active?
|
||||
bool active;
|
||||
|
||||
/// Use this to store application specific body data.
|
||||
void* userData;
|
||||
|
||||
/// Scale the gravity applied to this body.
|
||||
float32 gravityScale;
|
||||
};
|
||||
|
||||
/// A rigid body. These are created via b2World::CreateBody.
|
||||
class b2Body
|
||||
{
|
||||
public:
|
||||
/// Creates a fixture and attach it to this body. Use this function if you need
|
||||
/// to set some fixture parameters, like friction. Otherwise you can create the
|
||||
/// fixture directly from a shape.
|
||||
/// If the density is non-zero, this function automatically updates the mass of the body.
|
||||
/// Contacts are not created until the next time step.
|
||||
/// @param def the fixture definition.
|
||||
/// @warning This function is locked during callbacks.
|
||||
b2Fixture* CreateFixture(const b2FixtureDef* def);
|
||||
|
||||
/// Creates a fixture from a shape and attach it to this body.
|
||||
/// This is a convenience function. Use b2FixtureDef if you need to set parameters
|
||||
/// like friction, restitution, user data, or filtering.
|
||||
/// If the density is non-zero, this function automatically updates the mass of the body.
|
||||
/// @param shape the shape to be cloned.
|
||||
/// @param density the shape density (set to zero for static bodies).
|
||||
/// @warning This function is locked during callbacks.
|
||||
b2Fixture* CreateFixture(const b2Shape* shape, float32 density);
|
||||
|
||||
/// Destroy a fixture. This removes the fixture from the broad-phase and
|
||||
/// destroys all contacts associated with this fixture. This will
|
||||
/// automatically adjust the mass of the body if the body is dynamic and the
|
||||
/// fixture has positive density.
|
||||
/// All fixtures attached to a body are implicitly destroyed when the body is destroyed.
|
||||
/// @param fixture the fixture to be removed.
|
||||
/// @warning This function is locked during callbacks.
|
||||
void DestroyFixture(b2Fixture* fixture);
|
||||
|
||||
/// Set the position of the body's origin and rotation.
|
||||
/// Manipulating a body's transform may cause non-physical behavior.
|
||||
/// Note: contacts are updated on the next call to b2World::Step.
|
||||
/// @param position the world position of the body's local origin.
|
||||
/// @param angle the world rotation in radians.
|
||||
void SetTransform(const b2Vec2& position, float32 angle);
|
||||
|
||||
/// Get the body transform for the body's origin.
|
||||
/// @return the world transform of the body's origin.
|
||||
const b2Transform& GetTransform() const;
|
||||
|
||||
/// Get the world body origin position.
|
||||
/// @return the world position of the body's origin.
|
||||
const b2Vec2& GetPosition() const;
|
||||
|
||||
/// Get the angle in radians.
|
||||
/// @return the current world rotation angle in radians.
|
||||
float32 GetAngle() const;
|
||||
|
||||
/// Get the world position of the center of mass.
|
||||
const b2Vec2& GetWorldCenter() const;
|
||||
|
||||
/// Get the local position of the center of mass.
|
||||
const b2Vec2& GetLocalCenter() const;
|
||||
|
||||
/// Set the linear velocity of the center of mass.
|
||||
/// @param v the new linear velocity of the center of mass.
|
||||
void SetLinearVelocity(const b2Vec2& v);
|
||||
|
||||
/// Get the linear velocity of the center of mass.
|
||||
/// @return the linear velocity of the center of mass.
|
||||
const b2Vec2& GetLinearVelocity() const;
|
||||
|
||||
/// Set the angular velocity.
|
||||
/// @param omega the new angular velocity in radians/second.
|
||||
void SetAngularVelocity(float32 omega);
|
||||
|
||||
/// Get the angular velocity.
|
||||
/// @return the angular velocity in radians/second.
|
||||
float32 GetAngularVelocity() const;
|
||||
|
||||
/// Apply a force at a world point. If the force is not
|
||||
/// applied at the center of mass, it will generate a torque and
|
||||
/// affect the angular velocity. This wakes up the body.
|
||||
/// @param force the world force vector, usually in Newtons (N).
|
||||
/// @param point the world position of the point of application.
|
||||
/// @param wake also wake up the body
|
||||
void ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake);
|
||||
|
||||
/// Apply a force to the center of mass. This wakes up the body.
|
||||
/// @param force the world force vector, usually in Newtons (N).
|
||||
/// @param wake also wake up the body
|
||||
void ApplyForceToCenter(const b2Vec2& force, bool wake);
|
||||
|
||||
/// Apply a torque. This affects the angular velocity
|
||||
/// without affecting the linear velocity of the center of mass.
|
||||
/// @param torque about the z-axis (out of the screen), usually in N-m.
|
||||
/// @param wake also wake up the body
|
||||
void ApplyTorque(float32 torque, bool wake);
|
||||
|
||||
/// Apply an impulse at a point. This immediately modifies the velocity.
|
||||
/// It also modifies the angular velocity if the point of application
|
||||
/// is not at the center of mass. This wakes up the body.
|
||||
/// @param impulse the world impulse vector, usually in N-seconds or kg-m/s.
|
||||
/// @param point the world position of the point of application.
|
||||
/// @param wake also wake up the body
|
||||
void ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake);
|
||||
|
||||
/// Apply an impulse to the center of mass. This immediately modifies the velocity.
|
||||
/// @param impulse the world impulse vector, usually in N-seconds or kg-m/s.
|
||||
/// @param wake also wake up the body
|
||||
void ApplyLinearImpulseToCenter(const b2Vec2& impulse, bool wake);
|
||||
|
||||
/// Apply an angular impulse.
|
||||
/// @param impulse the angular impulse in units of kg*m*m/s
|
||||
/// @param wake also wake up the body
|
||||
void ApplyAngularImpulse(float32 impulse, bool wake);
|
||||
|
||||
/// Get the total mass of the body.
|
||||
/// @return the mass, usually in kilograms (kg).
|
||||
float32 GetMass() const;
|
||||
|
||||
/// Get the rotational inertia of the body about the local origin.
|
||||
/// @return the rotational inertia, usually in kg-m^2.
|
||||
float32 GetInertia() const;
|
||||
|
||||
/// Get the mass data of the body.
|
||||
/// @return a struct containing the mass, inertia and center of the body.
|
||||
void GetMassData(b2MassData* data) const;
|
||||
|
||||
/// Set the mass properties to override the mass properties of the fixtures.
|
||||
/// Note that this changes the center of mass position.
|
||||
/// Note that creating or destroying fixtures can also alter the mass.
|
||||
/// This function has no effect if the body isn't dynamic.
|
||||
/// @param massData the mass properties.
|
||||
void SetMassData(const b2MassData* data);
|
||||
|
||||
/// This resets the mass properties to the sum of the mass properties of the fixtures.
|
||||
/// This normally does not need to be called unless you called SetMassData to override
|
||||
/// the mass and you later want to reset the mass.
|
||||
void ResetMassData();
|
||||
|
||||
/// Get the world coordinates of a point given the local coordinates.
|
||||
/// @param localPoint a point on the body measured relative the the body's origin.
|
||||
/// @return the same point expressed in world coordinates.
|
||||
b2Vec2 GetWorldPoint(const b2Vec2& localPoint) const;
|
||||
|
||||
/// Get the world coordinates of a vector given the local coordinates.
|
||||
/// @param localVector a vector fixed in the body.
|
||||
/// @return the same vector expressed in world coordinates.
|
||||
b2Vec2 GetWorldVector(const b2Vec2& localVector) const;
|
||||
|
||||
/// Gets a local point relative to the body's origin given a world point.
|
||||
/// @param a point in world coordinates.
|
||||
/// @return the corresponding local point relative to the body's origin.
|
||||
b2Vec2 GetLocalPoint(const b2Vec2& worldPoint) const;
|
||||
|
||||
/// Gets a local vector given a world vector.
|
||||
/// @param a vector in world coordinates.
|
||||
/// @return the corresponding local vector.
|
||||
b2Vec2 GetLocalVector(const b2Vec2& worldVector) const;
|
||||
|
||||
/// Get the world linear velocity of a world point attached to this body.
|
||||
/// @param a point in world coordinates.
|
||||
/// @return the world velocity of a point.
|
||||
b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const;
|
||||
|
||||
/// Get the world velocity of a local point.
|
||||
/// @param a point in local coordinates.
|
||||
/// @return the world velocity of a point.
|
||||
b2Vec2 GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const;
|
||||
|
||||
/// Get the linear damping of the body.
|
||||
float32 GetLinearDamping() const;
|
||||
|
||||
/// Set the linear damping of the body.
|
||||
void SetLinearDamping(float32 linearDamping);
|
||||
|
||||
/// Get the angular damping of the body.
|
||||
float32 GetAngularDamping() const;
|
||||
|
||||
/// Set the angular damping of the body.
|
||||
void SetAngularDamping(float32 angularDamping);
|
||||
|
||||
/// Get the gravity scale of the body.
|
||||
float32 GetGravityScale() const;
|
||||
|
||||
/// Set the gravity scale of the body.
|
||||
void SetGravityScale(float32 scale);
|
||||
|
||||
/// Set the type of this body. This may alter the mass and velocity.
|
||||
void SetType(b2BodyType type);
|
||||
|
||||
/// Get the type of this body.
|
||||
b2BodyType GetType() const;
|
||||
|
||||
/// Should this body be treated like a bullet for continuous collision detection?
|
||||
void SetBullet(bool flag);
|
||||
|
||||
/// Is this body treated like a bullet for continuous collision detection?
|
||||
bool IsBullet() const;
|
||||
|
||||
/// You can disable sleeping on this body. If you disable sleeping, the
|
||||
/// body will be woken.
|
||||
void SetSleepingAllowed(bool flag);
|
||||
|
||||
/// Is this body allowed to sleep
|
||||
bool IsSleepingAllowed() const;
|
||||
|
||||
/// Set the sleep state of the body. A sleeping body has very
|
||||
/// low CPU cost.
|
||||
/// @param flag set to true to wake the body, false to put it to sleep.
|
||||
void SetAwake(bool flag);
|
||||
|
||||
/// Get the sleeping state of this body.
|
||||
/// @return true if the body is awake.
|
||||
bool IsAwake() const;
|
||||
|
||||
/// Set the active state of the body. An inactive body is not
|
||||
/// simulated and cannot be collided with or woken up.
|
||||
/// If you pass a flag of true, all fixtures will be added to the
|
||||
/// broad-phase.
|
||||
/// If you pass a flag of false, all fixtures will be removed from
|
||||
/// the broad-phase and all contacts will be destroyed.
|
||||
/// Fixtures and joints are otherwise unaffected. You may continue
|
||||
/// to create/destroy fixtures and joints on inactive bodies.
|
||||
/// Fixtures on an inactive body are implicitly inactive and will
|
||||
/// not participate in collisions, ray-casts, or queries.
|
||||
/// Joints connected to an inactive body are implicitly inactive.
|
||||
/// An inactive body is still owned by a b2World object and remains
|
||||
/// in the body list.
|
||||
void SetActive(bool flag);
|
||||
|
||||
/// Get the active state of the body.
|
||||
bool IsActive() const;
|
||||
|
||||
/// Set this body to have fixed rotation. This causes the mass
|
||||
/// to be reset.
|
||||
void SetFixedRotation(bool flag);
|
||||
|
||||
/// Does this body have fixed rotation?
|
||||
bool IsFixedRotation() const;
|
||||
|
||||
/// Get the list of all fixtures attached to this body.
|
||||
b2Fixture* GetFixtureList();
|
||||
const b2Fixture* GetFixtureList() const;
|
||||
|
||||
/// Get the list of all joints attached to this body.
|
||||
b2JointEdge* GetJointList();
|
||||
const b2JointEdge* GetJointList() const;
|
||||
|
||||
/// Get the list of all contacts attached to this body.
|
||||
/// @warning this list changes during the time step and you may
|
||||
/// miss some collisions if you don't use b2ContactListener.
|
||||
b2ContactEdge* GetContactList();
|
||||
const b2ContactEdge* GetContactList() const;
|
||||
|
||||
/// Get the next body in the world's body list.
|
||||
b2Body* GetNext();
|
||||
const b2Body* GetNext() const;
|
||||
|
||||
/// Get the user data pointer that was provided in the body definition.
|
||||
void* GetUserData() const;
|
||||
|
||||
/// Set the user data. Use this to store your application specific data.
|
||||
void SetUserData(void* data);
|
||||
|
||||
/// Get the parent world of this body.
|
||||
b2World* GetWorld();
|
||||
const b2World* GetWorld() const;
|
||||
|
||||
/// Dump this body to a log file
|
||||
void Dump();
|
||||
|
||||
private:
|
||||
|
||||
friend class b2World;
|
||||
friend class b2Island;
|
||||
friend class b2ContactManager;
|
||||
friend class b2ContactSolver;
|
||||
friend class b2Contact;
|
||||
|
||||
friend class b2DistanceJoint;
|
||||
friend class b2FrictionJoint;
|
||||
friend class b2GearJoint;
|
||||
friend class b2MotorJoint;
|
||||
friend class b2MouseJoint;
|
||||
friend class b2PrismaticJoint;
|
||||
friend class b2PulleyJoint;
|
||||
friend class b2RevoluteJoint;
|
||||
friend class b2RopeJoint;
|
||||
friend class b2WeldJoint;
|
||||
friend class b2WheelJoint;
|
||||
|
||||
// m_flags
|
||||
enum
|
||||
{
|
||||
e_islandFlag = 0x0001,
|
||||
e_awakeFlag = 0x0002,
|
||||
e_autoSleepFlag = 0x0004,
|
||||
e_bulletFlag = 0x0008,
|
||||
e_fixedRotationFlag = 0x0010,
|
||||
e_activeFlag = 0x0020,
|
||||
e_toiFlag = 0x0040
|
||||
};
|
||||
|
||||
b2Body(const b2BodyDef* bd, b2World* world);
|
||||
~b2Body();
|
||||
|
||||
void SynchronizeFixtures();
|
||||
void SynchronizeTransform();
|
||||
|
||||
// This is used to prevent connected bodies from colliding.
|
||||
// It may lie, depending on the collideConnected flag.
|
||||
bool ShouldCollide(const b2Body* other) const;
|
||||
|
||||
void Advance(float32 t);
|
||||
|
||||
b2BodyType m_type;
|
||||
|
||||
uint16 m_flags;
|
||||
|
||||
int32 m_islandIndex;
|
||||
|
||||
b2Transform m_xf; // the body origin transform
|
||||
b2Sweep m_sweep; // the swept motion for CCD
|
||||
|
||||
b2Vec2 m_linearVelocity;
|
||||
float32 m_angularVelocity;
|
||||
|
||||
b2Vec2 m_force;
|
||||
float32 m_torque;
|
||||
|
||||
b2World* m_world;
|
||||
b2Body* m_prev;
|
||||
b2Body* m_next;
|
||||
|
||||
b2Fixture* m_fixtureList;
|
||||
int32 m_fixtureCount;
|
||||
|
||||
b2JointEdge* m_jointList;
|
||||
b2ContactEdge* m_contactList;
|
||||
|
||||
float32 m_mass, m_invMass;
|
||||
|
||||
// Rotational inertia about the center of mass.
|
||||
float32 m_I, m_invI;
|
||||
|
||||
float32 m_linearDamping;
|
||||
float32 m_angularDamping;
|
||||
float32 m_gravityScale;
|
||||
|
||||
float32 m_sleepTime;
|
||||
|
||||
void* m_userData;
|
||||
};
|
||||
|
||||
inline b2BodyType b2Body::GetType() const
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
inline const b2Transform& b2Body::GetTransform() const
|
||||
{
|
||||
return m_xf;
|
||||
}
|
||||
|
||||
inline const b2Vec2& b2Body::GetPosition() const
|
||||
{
|
||||
return m_xf.p;
|
||||
}
|
||||
|
||||
inline float32 b2Body::GetAngle() const
|
||||
{
|
||||
return m_sweep.a;
|
||||
}
|
||||
|
||||
inline const b2Vec2& b2Body::GetWorldCenter() const
|
||||
{
|
||||
return m_sweep.c;
|
||||
}
|
||||
|
||||
inline const b2Vec2& b2Body::GetLocalCenter() const
|
||||
{
|
||||
return m_sweep.localCenter;
|
||||
}
|
||||
|
||||
inline void b2Body::SetLinearVelocity(const b2Vec2& v)
|
||||
{
|
||||
if (m_type == b2_staticBody)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (b2Dot(v,v) > 0.0f)
|
||||
{
|
||||
SetAwake(true);
|
||||
}
|
||||
|
||||
m_linearVelocity = v;
|
||||
}
|
||||
|
||||
inline const b2Vec2& b2Body::GetLinearVelocity() const
|
||||
{
|
||||
return m_linearVelocity;
|
||||
}
|
||||
|
||||
inline void b2Body::SetAngularVelocity(float32 w)
|
||||
{
|
||||
if (m_type == b2_staticBody)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (w * w > 0.0f)
|
||||
{
|
||||
SetAwake(true);
|
||||
}
|
||||
|
||||
m_angularVelocity = w;
|
||||
}
|
||||
|
||||
inline float32 b2Body::GetAngularVelocity() const
|
||||
{
|
||||
return m_angularVelocity;
|
||||
}
|
||||
|
||||
inline float32 b2Body::GetMass() const
|
||||
{
|
||||
return m_mass;
|
||||
}
|
||||
|
||||
inline float32 b2Body::GetInertia() const
|
||||
{
|
||||
return m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
|
||||
}
|
||||
|
||||
inline void b2Body::GetMassData(b2MassData* data) const
|
||||
{
|
||||
data->mass = m_mass;
|
||||
data->I = m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
|
||||
data->center = m_sweep.localCenter;
|
||||
}
|
||||
|
||||
inline b2Vec2 b2Body::GetWorldPoint(const b2Vec2& localPoint) const
|
||||
{
|
||||
return b2Mul(m_xf, localPoint);
|
||||
}
|
||||
|
||||
inline b2Vec2 b2Body::GetWorldVector(const b2Vec2& localVector) const
|
||||
{
|
||||
return b2Mul(m_xf.q, localVector);
|
||||
}
|
||||
|
||||
inline b2Vec2 b2Body::GetLocalPoint(const b2Vec2& worldPoint) const
|
||||
{
|
||||
return b2MulT(m_xf, worldPoint);
|
||||
}
|
||||
|
||||
inline b2Vec2 b2Body::GetLocalVector(const b2Vec2& worldVector) const
|
||||
{
|
||||
return b2MulT(m_xf.q, worldVector);
|
||||
}
|
||||
|
||||
inline b2Vec2 b2Body::GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const
|
||||
{
|
||||
return m_linearVelocity + b2Cross(m_angularVelocity, worldPoint - m_sweep.c);
|
||||
}
|
||||
|
||||
inline b2Vec2 b2Body::GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const
|
||||
{
|
||||
return GetLinearVelocityFromWorldPoint(GetWorldPoint(localPoint));
|
||||
}
|
||||
|
||||
inline float32 b2Body::GetLinearDamping() const
|
||||
{
|
||||
return m_linearDamping;
|
||||
}
|
||||
|
||||
inline void b2Body::SetLinearDamping(float32 linearDamping)
|
||||
{
|
||||
m_linearDamping = linearDamping;
|
||||
}
|
||||
|
||||
inline float32 b2Body::GetAngularDamping() const
|
||||
{
|
||||
return m_angularDamping;
|
||||
}
|
||||
|
||||
inline void b2Body::SetAngularDamping(float32 angularDamping)
|
||||
{
|
||||
m_angularDamping = angularDamping;
|
||||
}
|
||||
|
||||
inline float32 b2Body::GetGravityScale() const
|
||||
{
|
||||
return m_gravityScale;
|
||||
}
|
||||
|
||||
inline void b2Body::SetGravityScale(float32 scale)
|
||||
{
|
||||
m_gravityScale = scale;
|
||||
}
|
||||
|
||||
inline void b2Body::SetBullet(bool flag)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
m_flags |= e_bulletFlag;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_flags &= ~e_bulletFlag;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool b2Body::IsBullet() const
|
||||
{
|
||||
return (m_flags & e_bulletFlag) == e_bulletFlag;
|
||||
}
|
||||
|
||||
inline void b2Body::SetAwake(bool flag)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
if ((m_flags & e_awakeFlag) == 0)
|
||||
{
|
||||
m_flags |= e_awakeFlag;
|
||||
m_sleepTime = 0.0f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_flags &= ~e_awakeFlag;
|
||||
m_sleepTime = 0.0f;
|
||||
m_linearVelocity.SetZero();
|
||||
m_angularVelocity = 0.0f;
|
||||
m_force.SetZero();
|
||||
m_torque = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool b2Body::IsAwake() const
|
||||
{
|
||||
return (m_flags & e_awakeFlag) == e_awakeFlag;
|
||||
}
|
||||
|
||||
inline bool b2Body::IsActive() const
|
||||
{
|
||||
return (m_flags & e_activeFlag) == e_activeFlag;
|
||||
}
|
||||
|
||||
inline bool b2Body::IsFixedRotation() const
|
||||
{
|
||||
return (m_flags & e_fixedRotationFlag) == e_fixedRotationFlag;
|
||||
}
|
||||
|
||||
inline void b2Body::SetSleepingAllowed(bool flag)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
m_flags |= e_autoSleepFlag;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_flags &= ~e_autoSleepFlag;
|
||||
SetAwake(true);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool b2Body::IsSleepingAllowed() const
|
||||
{
|
||||
return (m_flags & e_autoSleepFlag) == e_autoSleepFlag;
|
||||
}
|
||||
|
||||
inline b2Fixture* b2Body::GetFixtureList()
|
||||
{
|
||||
return m_fixtureList;
|
||||
}
|
||||
|
||||
inline const b2Fixture* b2Body::GetFixtureList() const
|
||||
{
|
||||
return m_fixtureList;
|
||||
}
|
||||
|
||||
inline b2JointEdge* b2Body::GetJointList()
|
||||
{
|
||||
return m_jointList;
|
||||
}
|
||||
|
||||
inline const b2JointEdge* b2Body::GetJointList() const
|
||||
{
|
||||
return m_jointList;
|
||||
}
|
||||
|
||||
inline b2ContactEdge* b2Body::GetContactList()
|
||||
{
|
||||
return m_contactList;
|
||||
}
|
||||
|
||||
inline const b2ContactEdge* b2Body::GetContactList() const
|
||||
{
|
||||
return m_contactList;
|
||||
}
|
||||
|
||||
inline b2Body* b2Body::GetNext()
|
||||
{
|
||||
return m_next;
|
||||
}
|
||||
|
||||
inline const b2Body* b2Body::GetNext() const
|
||||
{
|
||||
return m_next;
|
||||
}
|
||||
|
||||
inline void b2Body::SetUserData(void* data)
|
||||
{
|
||||
m_userData = data;
|
||||
}
|
||||
|
||||
inline void* b2Body::GetUserData() const
|
||||
{
|
||||
return m_userData;
|
||||
}
|
||||
|
||||
inline void b2Body::ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake)
|
||||
{
|
||||
if (m_type != b2_dynamicBody)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (wake && (m_flags & e_awakeFlag) == 0)
|
||||
{
|
||||
SetAwake(true);
|
||||
}
|
||||
|
||||
// Don't accumulate a force if the body is sleeping.
|
||||
if (m_flags & e_awakeFlag)
|
||||
{
|
||||
m_force += force;
|
||||
m_torque += b2Cross(point - m_sweep.c, force);
|
||||
}
|
||||
}
|
||||
|
||||
inline void b2Body::ApplyForceToCenter(const b2Vec2& force, bool wake)
|
||||
{
|
||||
if (m_type != b2_dynamicBody)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (wake && (m_flags & e_awakeFlag) == 0)
|
||||
{
|
||||
SetAwake(true);
|
||||
}
|
||||
|
||||
// Don't accumulate a force if the body is sleeping
|
||||
if (m_flags & e_awakeFlag)
|
||||
{
|
||||
m_force += force;
|
||||
}
|
||||
}
|
||||
|
||||
inline void b2Body::ApplyTorque(float32 torque, bool wake)
|
||||
{
|
||||
if (m_type != b2_dynamicBody)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (wake && (m_flags & e_awakeFlag) == 0)
|
||||
{
|
||||
SetAwake(true);
|
||||
}
|
||||
|
||||
// Don't accumulate a force if the body is sleeping
|
||||
if (m_flags & e_awakeFlag)
|
||||
{
|
||||
m_torque += torque;
|
||||
}
|
||||
}
|
||||
|
||||
inline void b2Body::ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake)
|
||||
{
|
||||
if (m_type != b2_dynamicBody)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (wake && (m_flags & e_awakeFlag) == 0)
|
||||
{
|
||||
SetAwake(true);
|
||||
}
|
||||
|
||||
// Don't accumulate velocity if the body is sleeping
|
||||
if (m_flags & e_awakeFlag)
|
||||
{
|
||||
m_linearVelocity += m_invMass * impulse;
|
||||
m_angularVelocity += m_invI * b2Cross(point - m_sweep.c, impulse);
|
||||
}
|
||||
}
|
||||
|
||||
inline void b2Body::ApplyLinearImpulseToCenter(const b2Vec2& impulse, bool wake)
|
||||
{
|
||||
if (m_type != b2_dynamicBody)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (wake && (m_flags & e_awakeFlag) == 0)
|
||||
{
|
||||
SetAwake(true);
|
||||
}
|
||||
|
||||
// Don't accumulate velocity if the body is sleeping
|
||||
if (m_flags & e_awakeFlag)
|
||||
{
|
||||
m_linearVelocity += m_invMass * impulse;
|
||||
}
|
||||
}
|
||||
|
||||
inline void b2Body::ApplyAngularImpulse(float32 impulse, bool wake)
|
||||
{
|
||||
if (m_type != b2_dynamicBody)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (wake && (m_flags & e_awakeFlag) == 0)
|
||||
{
|
||||
SetAwake(true);
|
||||
}
|
||||
|
||||
// Don't accumulate velocity if the body is sleeping
|
||||
if (m_flags & e_awakeFlag)
|
||||
{
|
||||
m_angularVelocity += m_invI * impulse;
|
||||
}
|
||||
}
|
||||
|
||||
inline void b2Body::SynchronizeTransform()
|
||||
{
|
||||
m_xf.q.Set(m_sweep.a);
|
||||
m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
|
||||
}
|
||||
|
||||
inline void b2Body::Advance(float32 alpha)
|
||||
{
|
||||
// Advance to the new safe time. This doesn't sync the broad-phase.
|
||||
m_sweep.Advance(alpha);
|
||||
m_sweep.c = m_sweep.c0;
|
||||
m_sweep.a = m_sweep.a0;
|
||||
m_xf.q.Set(m_sweep.a);
|
||||
m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
|
||||
}
|
||||
|
||||
inline b2World* b2Body::GetWorld()
|
||||
{
|
||||
return m_world;
|
||||
}
|
||||
|
||||
inline const b2World* b2Body::GetWorld() const
|
||||
{
|
||||
return m_world;
|
||||
}
|
||||
|
||||
#endif
|
||||
52
inc/Box2D/Dynamics/b2ContactManager.h
Normal file
52
inc/Box2D/Dynamics/b2ContactManager.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_CONTACT_MANAGER_H
|
||||
#define B2_CONTACT_MANAGER_H
|
||||
|
||||
#include <Box2D/Collision/b2BroadPhase.h>
|
||||
|
||||
class b2Contact;
|
||||
class b2ContactFilter;
|
||||
class b2ContactListener;
|
||||
class b2BlockAllocator;
|
||||
|
||||
// Delegate of b2World.
|
||||
class b2ContactManager
|
||||
{
|
||||
public:
|
||||
b2ContactManager();
|
||||
|
||||
// Broad-phase callback.
|
||||
void AddPair(void* proxyUserDataA, void* proxyUserDataB);
|
||||
|
||||
void FindNewContacts();
|
||||
|
||||
void Destroy(b2Contact* c);
|
||||
|
||||
void Collide();
|
||||
|
||||
b2BroadPhase m_broadPhase;
|
||||
b2Contact* m_contactList;
|
||||
int32 m_contactCount;
|
||||
b2ContactFilter* m_contactFilter;
|
||||
b2ContactListener* m_contactListener;
|
||||
b2BlockAllocator* m_allocator;
|
||||
};
|
||||
|
||||
#endif
|
||||
345
inc/Box2D/Dynamics/b2Fixture.h
Normal file
345
inc/Box2D/Dynamics/b2Fixture.h
Normal file
@@ -0,0 +1,345 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_FIXTURE_H
|
||||
#define B2_FIXTURE_H
|
||||
|
||||
#include <Box2D/Dynamics/b2Body.h>
|
||||
#include <Box2D/Collision/b2Collision.h>
|
||||
#include <Box2D/Collision/Shapes/b2Shape.h>
|
||||
|
||||
class b2BlockAllocator;
|
||||
class b2Body;
|
||||
class b2BroadPhase;
|
||||
class b2Fixture;
|
||||
|
||||
/// This holds contact filtering data.
|
||||
struct b2Filter
|
||||
{
|
||||
b2Filter()
|
||||
{
|
||||
categoryBits = 0x0001;
|
||||
maskBits = 0xFFFF;
|
||||
groupIndex = 0;
|
||||
}
|
||||
|
||||
/// The collision category bits. Normally you would just set one bit.
|
||||
uint16 categoryBits;
|
||||
|
||||
/// The collision mask bits. This states the categories that this
|
||||
/// shape would accept for collision.
|
||||
uint16 maskBits;
|
||||
|
||||
/// Collision groups allow a certain group of objects to never collide (negative)
|
||||
/// or always collide (positive). Zero means no collision group. Non-zero group
|
||||
/// filtering always wins against the mask bits.
|
||||
int16 groupIndex;
|
||||
};
|
||||
|
||||
/// A fixture definition is used to create a fixture. This class defines an
|
||||
/// abstract fixture definition. You can reuse fixture definitions safely.
|
||||
struct b2FixtureDef
|
||||
{
|
||||
/// The constructor sets the default fixture definition values.
|
||||
b2FixtureDef()
|
||||
{
|
||||
shape = NULL;
|
||||
userData = NULL;
|
||||
friction = 0.2f;
|
||||
restitution = 0.0f;
|
||||
density = 0.0f;
|
||||
isSensor = false;
|
||||
}
|
||||
|
||||
/// The shape, this must be set. The shape will be cloned, so you
|
||||
/// can create the shape on the stack.
|
||||
const b2Shape* shape;
|
||||
|
||||
/// Use this to store application specific fixture data.
|
||||
void* userData;
|
||||
|
||||
/// The friction coefficient, usually in the range [0,1].
|
||||
float32 friction;
|
||||
|
||||
/// The restitution (elasticity) usually in the range [0,1].
|
||||
float32 restitution;
|
||||
|
||||
/// The density, usually in kg/m^2.
|
||||
float32 density;
|
||||
|
||||
/// A sensor shape collects contact information but never generates a collision
|
||||
/// response.
|
||||
bool isSensor;
|
||||
|
||||
/// Contact filtering data.
|
||||
b2Filter filter;
|
||||
};
|
||||
|
||||
/// This proxy is used internally to connect fixtures to the broad-phase.
|
||||
struct b2FixtureProxy
|
||||
{
|
||||
b2AABB aabb;
|
||||
b2Fixture* fixture;
|
||||
int32 childIndex;
|
||||
int32 proxyId;
|
||||
};
|
||||
|
||||
/// A fixture is used to attach a shape to a body for collision detection. A fixture
|
||||
/// inherits its transform from its parent. Fixtures hold additional non-geometric data
|
||||
/// such as friction, collision filters, etc.
|
||||
/// Fixtures are created via b2Body::CreateFixture.
|
||||
/// @warning you cannot reuse fixtures.
|
||||
class b2Fixture
|
||||
{
|
||||
public:
|
||||
/// Get the type of the child shape. You can use this to down cast to the concrete shape.
|
||||
/// @return the shape type.
|
||||
b2Shape::Type GetType() const;
|
||||
|
||||
/// Get the child shape. You can modify the child shape, however you should not change the
|
||||
/// number of vertices because this will crash some collision caching mechanisms.
|
||||
/// Manipulating the shape may lead to non-physical behavior.
|
||||
b2Shape* GetShape();
|
||||
const b2Shape* GetShape() const;
|
||||
|
||||
/// Set if this fixture is a sensor.
|
||||
void SetSensor(bool sensor);
|
||||
|
||||
/// Is this fixture a sensor (non-solid)?
|
||||
/// @return the true if the shape is a sensor.
|
||||
bool IsSensor() const;
|
||||
|
||||
/// Set the contact filtering data. This will not update contacts until the next time
|
||||
/// step when either parent body is active and awake.
|
||||
/// This automatically calls Refilter.
|
||||
void SetFilterData(const b2Filter& filter);
|
||||
|
||||
/// Get the contact filtering data.
|
||||
const b2Filter& GetFilterData() const;
|
||||
|
||||
/// Call this if you want to establish collision that was previously disabled by b2ContactFilter::ShouldCollide.
|
||||
void Refilter();
|
||||
|
||||
/// Get the parent body of this fixture. This is NULL if the fixture is not attached.
|
||||
/// @return the parent body.
|
||||
b2Body* GetBody();
|
||||
const b2Body* GetBody() const;
|
||||
|
||||
/// Get the next fixture in the parent body's fixture list.
|
||||
/// @return the next shape.
|
||||
b2Fixture* GetNext();
|
||||
const b2Fixture* GetNext() const;
|
||||
|
||||
/// Get the user data that was assigned in the fixture definition. Use this to
|
||||
/// store your application specific data.
|
||||
void* GetUserData() const;
|
||||
|
||||
/// Set the user data. Use this to store your application specific data.
|
||||
void SetUserData(void* data);
|
||||
|
||||
/// Test a point for containment in this fixture.
|
||||
/// @param p a point in world coordinates.
|
||||
bool TestPoint(const b2Vec2& p) const;
|
||||
|
||||
/// Cast a ray against this shape.
|
||||
/// @param output the ray-cast results.
|
||||
/// @param input the ray-cast input parameters.
|
||||
bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const;
|
||||
|
||||
/// Get the mass data for this fixture. The mass data is based on the density and
|
||||
/// the shape. The rotational inertia is about the shape's origin. This operation
|
||||
/// may be expensive.
|
||||
void GetMassData(b2MassData* massData) const;
|
||||
|
||||
/// Set the density of this fixture. This will _not_ automatically adjust the mass
|
||||
/// of the body. You must call b2Body::ResetMassData to update the body's mass.
|
||||
void SetDensity(float32 density);
|
||||
|
||||
/// Get the density of this fixture.
|
||||
float32 GetDensity() const;
|
||||
|
||||
/// Get the coefficient of friction.
|
||||
float32 GetFriction() const;
|
||||
|
||||
/// Set the coefficient of friction. This will _not_ change the friction of
|
||||
/// existing contacts.
|
||||
void SetFriction(float32 friction);
|
||||
|
||||
/// Get the coefficient of restitution.
|
||||
float32 GetRestitution() const;
|
||||
|
||||
/// Set the coefficient of restitution. This will _not_ change the restitution of
|
||||
/// existing contacts.
|
||||
void SetRestitution(float32 restitution);
|
||||
|
||||
/// Get the fixture's AABB. This AABB may be enlarge and/or stale.
|
||||
/// If you need a more accurate AABB, compute it using the shape and
|
||||
/// the body transform.
|
||||
const b2AABB& GetAABB(int32 childIndex) const;
|
||||
|
||||
/// Dump this fixture to the log file.
|
||||
void Dump(int32 bodyIndex);
|
||||
|
||||
protected:
|
||||
|
||||
friend class b2Body;
|
||||
friend class b2World;
|
||||
friend class b2Contact;
|
||||
friend class b2ContactManager;
|
||||
|
||||
b2Fixture();
|
||||
|
||||
// We need separation create/destroy functions from the constructor/destructor because
|
||||
// the destructor cannot access the allocator (no destructor arguments allowed by C++).
|
||||
void Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def);
|
||||
void Destroy(b2BlockAllocator* allocator);
|
||||
|
||||
// These support body activation/deactivation.
|
||||
void CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf);
|
||||
void DestroyProxies(b2BroadPhase* broadPhase);
|
||||
|
||||
void Synchronize(b2BroadPhase* broadPhase, const b2Transform& xf1, const b2Transform& xf2);
|
||||
|
||||
float32 m_density;
|
||||
|
||||
b2Fixture* m_next;
|
||||
b2Body* m_body;
|
||||
|
||||
b2Shape* m_shape;
|
||||
|
||||
float32 m_friction;
|
||||
float32 m_restitution;
|
||||
|
||||
b2FixtureProxy* m_proxies;
|
||||
int32 m_proxyCount;
|
||||
|
||||
b2Filter m_filter;
|
||||
|
||||
bool m_isSensor;
|
||||
|
||||
void* m_userData;
|
||||
};
|
||||
|
||||
inline b2Shape::Type b2Fixture::GetType() const
|
||||
{
|
||||
return m_shape->GetType();
|
||||
}
|
||||
|
||||
inline b2Shape* b2Fixture::GetShape()
|
||||
{
|
||||
return m_shape;
|
||||
}
|
||||
|
||||
inline const b2Shape* b2Fixture::GetShape() const
|
||||
{
|
||||
return m_shape;
|
||||
}
|
||||
|
||||
inline bool b2Fixture::IsSensor() const
|
||||
{
|
||||
return m_isSensor;
|
||||
}
|
||||
|
||||
inline const b2Filter& b2Fixture::GetFilterData() const
|
||||
{
|
||||
return m_filter;
|
||||
}
|
||||
|
||||
inline void* b2Fixture::GetUserData() const
|
||||
{
|
||||
return m_userData;
|
||||
}
|
||||
|
||||
inline void b2Fixture::SetUserData(void* data)
|
||||
{
|
||||
m_userData = data;
|
||||
}
|
||||
|
||||
inline b2Body* b2Fixture::GetBody()
|
||||
{
|
||||
return m_body;
|
||||
}
|
||||
|
||||
inline const b2Body* b2Fixture::GetBody() const
|
||||
{
|
||||
return m_body;
|
||||
}
|
||||
|
||||
inline b2Fixture* b2Fixture::GetNext()
|
||||
{
|
||||
return m_next;
|
||||
}
|
||||
|
||||
inline const b2Fixture* b2Fixture::GetNext() const
|
||||
{
|
||||
return m_next;
|
||||
}
|
||||
|
||||
inline void b2Fixture::SetDensity(float32 density)
|
||||
{
|
||||
b2Assert(b2IsValid(density) && density >= 0.0f);
|
||||
m_density = density;
|
||||
}
|
||||
|
||||
inline float32 b2Fixture::GetDensity() const
|
||||
{
|
||||
return m_density;
|
||||
}
|
||||
|
||||
inline float32 b2Fixture::GetFriction() const
|
||||
{
|
||||
return m_friction;
|
||||
}
|
||||
|
||||
inline void b2Fixture::SetFriction(float32 friction)
|
||||
{
|
||||
m_friction = friction;
|
||||
}
|
||||
|
||||
inline float32 b2Fixture::GetRestitution() const
|
||||
{
|
||||
return m_restitution;
|
||||
}
|
||||
|
||||
inline void b2Fixture::SetRestitution(float32 restitution)
|
||||
{
|
||||
m_restitution = restitution;
|
||||
}
|
||||
|
||||
inline bool b2Fixture::TestPoint(const b2Vec2& p) const
|
||||
{
|
||||
return m_shape->TestPoint(m_body->GetTransform(), p);
|
||||
}
|
||||
|
||||
inline bool b2Fixture::RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const
|
||||
{
|
||||
return m_shape->RayCast(output, input, m_body->GetTransform(), childIndex);
|
||||
}
|
||||
|
||||
inline void b2Fixture::GetMassData(b2MassData* massData) const
|
||||
{
|
||||
m_shape->ComputeMass(massData, m_density);
|
||||
}
|
||||
|
||||
inline const b2AABB& b2Fixture::GetAABB(int32 childIndex) const
|
||||
{
|
||||
b2Assert(0 <= childIndex && childIndex < m_proxyCount);
|
||||
return m_proxies[childIndex].aabb;
|
||||
}
|
||||
|
||||
#endif
|
||||
93
inc/Box2D/Dynamics/b2Island.h
Normal file
93
inc/Box2D/Dynamics/b2Island.h
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_ISLAND_H
|
||||
#define B2_ISLAND_H
|
||||
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
#include <Box2D/Dynamics/b2Body.h>
|
||||
#include <Box2D/Dynamics/b2TimeStep.h>
|
||||
|
||||
class b2Contact;
|
||||
class b2Joint;
|
||||
class b2StackAllocator;
|
||||
class b2ContactListener;
|
||||
struct b2ContactVelocityConstraint;
|
||||
struct b2Profile;
|
||||
|
||||
/// This is an internal class.
|
||||
class b2Island
|
||||
{
|
||||
public:
|
||||
b2Island(int32 bodyCapacity, int32 contactCapacity, int32 jointCapacity,
|
||||
b2StackAllocator* allocator, b2ContactListener* listener);
|
||||
~b2Island();
|
||||
|
||||
void Clear()
|
||||
{
|
||||
m_bodyCount = 0;
|
||||
m_contactCount = 0;
|
||||
m_jointCount = 0;
|
||||
}
|
||||
|
||||
void Solve(b2Profile* profile, const b2TimeStep& step, const b2Vec2& gravity, bool allowSleep);
|
||||
|
||||
void SolveTOI(const b2TimeStep& subStep, int32 toiIndexA, int32 toiIndexB);
|
||||
|
||||
void Add(b2Body* body)
|
||||
{
|
||||
b2Assert(m_bodyCount < m_bodyCapacity);
|
||||
body->m_islandIndex = m_bodyCount;
|
||||
m_bodies[m_bodyCount] = body;
|
||||
++m_bodyCount;
|
||||
}
|
||||
|
||||
void Add(b2Contact* contact)
|
||||
{
|
||||
b2Assert(m_contactCount < m_contactCapacity);
|
||||
m_contacts[m_contactCount++] = contact;
|
||||
}
|
||||
|
||||
void Add(b2Joint* joint)
|
||||
{
|
||||
b2Assert(m_jointCount < m_jointCapacity);
|
||||
m_joints[m_jointCount++] = joint;
|
||||
}
|
||||
|
||||
void Report(const b2ContactVelocityConstraint* constraints);
|
||||
|
||||
b2StackAllocator* m_allocator;
|
||||
b2ContactListener* m_listener;
|
||||
|
||||
b2Body** m_bodies;
|
||||
b2Contact** m_contacts;
|
||||
b2Joint** m_joints;
|
||||
|
||||
b2Position* m_positions;
|
||||
b2Velocity* m_velocities;
|
||||
|
||||
int32 m_bodyCount;
|
||||
int32 m_jointCount;
|
||||
int32 m_contactCount;
|
||||
|
||||
int32 m_bodyCapacity;
|
||||
int32 m_contactCapacity;
|
||||
int32 m_jointCapacity;
|
||||
};
|
||||
|
||||
#endif
|
||||
70
inc/Box2D/Dynamics/b2TimeStep.h
Normal file
70
inc/Box2D/Dynamics/b2TimeStep.h
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_TIME_STEP_H
|
||||
#define B2_TIME_STEP_H
|
||||
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
|
||||
/// Profiling data. Times are in milliseconds.
|
||||
struct b2Profile
|
||||
{
|
||||
float32 step;
|
||||
float32 collide;
|
||||
float32 solve;
|
||||
float32 solveInit;
|
||||
float32 solveVelocity;
|
||||
float32 solvePosition;
|
||||
float32 broadphase;
|
||||
float32 solveTOI;
|
||||
};
|
||||
|
||||
/// This is an internal structure.
|
||||
struct b2TimeStep
|
||||
{
|
||||
float32 dt; // time step
|
||||
float32 inv_dt; // inverse time step (0 if dt == 0).
|
||||
float32 dtRatio; // dt * inv_dt0
|
||||
int32 velocityIterations;
|
||||
int32 positionIterations;
|
||||
bool warmStarting;
|
||||
};
|
||||
|
||||
/// This is an internal structure.
|
||||
struct b2Position
|
||||
{
|
||||
b2Vec2 c;
|
||||
float32 a;
|
||||
};
|
||||
|
||||
/// This is an internal structure.
|
||||
struct b2Velocity
|
||||
{
|
||||
b2Vec2 v;
|
||||
float32 w;
|
||||
};
|
||||
|
||||
/// Solver Data
|
||||
struct b2SolverData
|
||||
{
|
||||
b2TimeStep step;
|
||||
b2Position* positions;
|
||||
b2Velocity* velocities;
|
||||
};
|
||||
|
||||
#endif
|
||||
354
inc/Box2D/Dynamics/b2World.h
Normal file
354
inc/Box2D/Dynamics/b2World.h
Normal file
@@ -0,0 +1,354 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_WORLD_H
|
||||
#define B2_WORLD_H
|
||||
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
#include <Box2D/Common/b2BlockAllocator.h>
|
||||
#include <Box2D/Common/b2StackAllocator.h>
|
||||
#include <Box2D/Dynamics/b2ContactManager.h>
|
||||
#include <Box2D/Dynamics/b2WorldCallbacks.h>
|
||||
#include <Box2D/Dynamics/b2TimeStep.h>
|
||||
|
||||
struct b2AABB;
|
||||
struct b2BodyDef;
|
||||
struct b2Color;
|
||||
struct b2JointDef;
|
||||
class b2Body;
|
||||
class b2Draw;
|
||||
class b2Fixture;
|
||||
class b2Joint;
|
||||
|
||||
/// The world class manages all physics entities, dynamic simulation,
|
||||
/// and asynchronous queries. The world also contains efficient memory
|
||||
/// management facilities.
|
||||
class b2World
|
||||
{
|
||||
public:
|
||||
/// Construct a world object.
|
||||
/// @param gravity the world gravity vector.
|
||||
b2World(const b2Vec2& gravity);
|
||||
|
||||
/// Destruct the world. All physics entities are destroyed and all heap memory is released.
|
||||
~b2World();
|
||||
|
||||
/// Register a destruction listener. The listener is owned by you and must
|
||||
/// remain in scope.
|
||||
void SetDestructionListener(b2DestructionListener* listener);
|
||||
|
||||
/// Register a contact filter to provide specific control over collision.
|
||||
/// Otherwise the default filter is used (b2_defaultFilter). The listener is
|
||||
/// owned by you and must remain in scope.
|
||||
void SetContactFilter(b2ContactFilter* filter);
|
||||
|
||||
/// Register a contact event listener. The listener is owned by you and must
|
||||
/// remain in scope.
|
||||
void SetContactListener(b2ContactListener* listener);
|
||||
|
||||
/// Register a routine for debug drawing. The debug draw functions are called
|
||||
/// inside with b2World::DrawDebugData method. The debug draw object is owned
|
||||
/// by you and must remain in scope.
|
||||
void SetDebugDraw(b2Draw* debugDraw);
|
||||
|
||||
/// Create a rigid body given a definition. No reference to the definition
|
||||
/// is retained.
|
||||
/// @warning This function is locked during callbacks.
|
||||
b2Body* CreateBody(const b2BodyDef* def);
|
||||
|
||||
/// Destroy a rigid body given a definition. No reference to the definition
|
||||
/// is retained. This function is locked during callbacks.
|
||||
/// @warning This automatically deletes all associated shapes and joints.
|
||||
/// @warning This function is locked during callbacks.
|
||||
void DestroyBody(b2Body* body);
|
||||
|
||||
/// Create a joint to constrain bodies together. No reference to the definition
|
||||
/// is retained. This may cause the connected bodies to cease colliding.
|
||||
/// @warning This function is locked during callbacks.
|
||||
b2Joint* CreateJoint(const b2JointDef* def);
|
||||
|
||||
/// Destroy a joint. This may cause the connected bodies to begin colliding.
|
||||
/// @warning This function is locked during callbacks.
|
||||
void DestroyJoint(b2Joint* joint);
|
||||
|
||||
/// Take a time step. This performs collision detection, integration,
|
||||
/// and constraint solution.
|
||||
/// @param timeStep the amount of time to simulate, this should not vary.
|
||||
/// @param velocityIterations for the velocity constraint solver.
|
||||
/// @param positionIterations for the position constraint solver.
|
||||
void Step( float32 timeStep,
|
||||
int32 velocityIterations,
|
||||
int32 positionIterations);
|
||||
|
||||
/// Manually clear the force buffer on all bodies. By default, forces are cleared automatically
|
||||
/// after each call to Step. The default behavior is modified by calling SetAutoClearForces.
|
||||
/// The purpose of this function is to support sub-stepping. Sub-stepping is often used to maintain
|
||||
/// a fixed sized time step under a variable frame-rate.
|
||||
/// When you perform sub-stepping you will disable auto clearing of forces and instead call
|
||||
/// ClearForces after all sub-steps are complete in one pass of your game loop.
|
||||
/// @see SetAutoClearForces
|
||||
void ClearForces();
|
||||
|
||||
/// Call this to draw shapes and other debug draw data. This is intentionally non-const.
|
||||
void DrawDebugData();
|
||||
|
||||
/// Query the world for all fixtures that potentially overlap the
|
||||
/// provided AABB.
|
||||
/// @param callback a user implemented callback class.
|
||||
/// @param aabb the query box.
|
||||
void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;
|
||||
|
||||
/// Ray-cast the world for all fixtures in the path of the ray. Your callback
|
||||
/// controls whether you get the closest point, any point, or n-points.
|
||||
/// The ray-cast ignores shapes that contain the starting point.
|
||||
/// @param callback a user implemented callback class.
|
||||
/// @param point1 the ray starting point
|
||||
/// @param point2 the ray ending point
|
||||
void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;
|
||||
|
||||
/// Get the world body list. With the returned body, use b2Body::GetNext to get
|
||||
/// the next body in the world list. A NULL body indicates the end of the list.
|
||||
/// @return the head of the world body list.
|
||||
b2Body* GetBodyList();
|
||||
const b2Body* GetBodyList() const;
|
||||
|
||||
/// Get the world joint list. With the returned joint, use b2Joint::GetNext to get
|
||||
/// the next joint in the world list. A NULL joint indicates the end of the list.
|
||||
/// @return the head of the world joint list.
|
||||
b2Joint* GetJointList();
|
||||
const b2Joint* GetJointList() const;
|
||||
|
||||
/// Get the world contact list. With the returned contact, use b2Contact::GetNext to get
|
||||
/// the next contact in the world list. A NULL contact indicates the end of the list.
|
||||
/// @return the head of the world contact list.
|
||||
/// @warning contacts are created and destroyed in the middle of a time step.
|
||||
/// Use b2ContactListener to avoid missing contacts.
|
||||
b2Contact* GetContactList();
|
||||
const b2Contact* GetContactList() const;
|
||||
|
||||
/// Enable/disable sleep.
|
||||
void SetAllowSleeping(bool flag);
|
||||
bool GetAllowSleeping() const { return m_allowSleep; }
|
||||
|
||||
/// Enable/disable warm starting. For testing.
|
||||
void SetWarmStarting(bool flag) { m_warmStarting = flag; }
|
||||
bool GetWarmStarting() const { return m_warmStarting; }
|
||||
|
||||
/// Enable/disable continuous physics. For testing.
|
||||
void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
|
||||
bool GetContinuousPhysics() const { return m_continuousPhysics; }
|
||||
|
||||
/// Enable/disable single stepped continuous physics. For testing.
|
||||
void SetSubStepping(bool flag) { m_subStepping = flag; }
|
||||
bool GetSubStepping() const { return m_subStepping; }
|
||||
|
||||
/// Get the number of broad-phase proxies.
|
||||
int32 GetProxyCount() const;
|
||||
|
||||
/// Get the number of bodies.
|
||||
int32 GetBodyCount() const;
|
||||
|
||||
/// Get the number of joints.
|
||||
int32 GetJointCount() const;
|
||||
|
||||
/// Get the number of contacts (each may have 0 or more contact points).
|
||||
int32 GetContactCount() const;
|
||||
|
||||
/// Get the height of the dynamic tree.
|
||||
int32 GetTreeHeight() const;
|
||||
|
||||
/// Get the balance of the dynamic tree.
|
||||
int32 GetTreeBalance() const;
|
||||
|
||||
/// Get the quality metric of the dynamic tree. The smaller the better.
|
||||
/// The minimum is 1.
|
||||
float32 GetTreeQuality() const;
|
||||
|
||||
/// Change the global gravity vector.
|
||||
void SetGravity(const b2Vec2& gravity);
|
||||
|
||||
/// Get the global gravity vector.
|
||||
b2Vec2 GetGravity() const;
|
||||
|
||||
/// Is the world locked (in the middle of a time step).
|
||||
bool IsLocked() const;
|
||||
|
||||
/// Set flag to control automatic clearing of forces after each time step.
|
||||
void SetAutoClearForces(bool flag);
|
||||
|
||||
/// Get the flag that controls automatic clearing of forces after each time step.
|
||||
bool GetAutoClearForces() const;
|
||||
|
||||
/// Shift the world origin. Useful for large worlds.
|
||||
/// The body shift formula is: position -= newOrigin
|
||||
/// @param newOrigin the new origin with respect to the old origin
|
||||
void ShiftOrigin(const b2Vec2& newOrigin);
|
||||
|
||||
/// Get the contact manager for testing.
|
||||
const b2ContactManager& GetContactManager() const;
|
||||
|
||||
/// Get the current profile.
|
||||
const b2Profile& GetProfile() const;
|
||||
|
||||
/// Dump the world into the log file.
|
||||
/// @warning this should be called outside of a time step.
|
||||
void Dump();
|
||||
|
||||
private:
|
||||
|
||||
// m_flags
|
||||
enum
|
||||
{
|
||||
e_newFixture = 0x0001,
|
||||
e_locked = 0x0002,
|
||||
e_clearForces = 0x0004
|
||||
};
|
||||
|
||||
friend class b2Body;
|
||||
friend class b2Fixture;
|
||||
friend class b2ContactManager;
|
||||
friend class b2Controller;
|
||||
|
||||
void Solve(const b2TimeStep& step);
|
||||
void SolveTOI(const b2TimeStep& step);
|
||||
|
||||
void DrawJoint(b2Joint* joint);
|
||||
void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);
|
||||
|
||||
b2BlockAllocator m_blockAllocator;
|
||||
b2StackAllocator m_stackAllocator;
|
||||
|
||||
int32 m_flags;
|
||||
|
||||
b2ContactManager m_contactManager;
|
||||
|
||||
b2Body* m_bodyList;
|
||||
b2Joint* m_jointList;
|
||||
|
||||
int32 m_bodyCount;
|
||||
int32 m_jointCount;
|
||||
|
||||
b2Vec2 m_gravity;
|
||||
bool m_allowSleep;
|
||||
|
||||
b2DestructionListener* m_destructionListener;
|
||||
b2Draw* g_debugDraw;
|
||||
|
||||
// This is used to compute the time step ratio to
|
||||
// support a variable time step.
|
||||
float32 m_inv_dt0;
|
||||
|
||||
// These are for debugging the solver.
|
||||
bool m_warmStarting;
|
||||
bool m_continuousPhysics;
|
||||
bool m_subStepping;
|
||||
|
||||
bool m_stepComplete;
|
||||
|
||||
b2Profile m_profile;
|
||||
};
|
||||
|
||||
inline b2Body* b2World::GetBodyList()
|
||||
{
|
||||
return m_bodyList;
|
||||
}
|
||||
|
||||
inline const b2Body* b2World::GetBodyList() const
|
||||
{
|
||||
return m_bodyList;
|
||||
}
|
||||
|
||||
inline b2Joint* b2World::GetJointList()
|
||||
{
|
||||
return m_jointList;
|
||||
}
|
||||
|
||||
inline const b2Joint* b2World::GetJointList() const
|
||||
{
|
||||
return m_jointList;
|
||||
}
|
||||
|
||||
inline b2Contact* b2World::GetContactList()
|
||||
{
|
||||
return m_contactManager.m_contactList;
|
||||
}
|
||||
|
||||
inline const b2Contact* b2World::GetContactList() const
|
||||
{
|
||||
return m_contactManager.m_contactList;
|
||||
}
|
||||
|
||||
inline int32 b2World::GetBodyCount() const
|
||||
{
|
||||
return m_bodyCount;
|
||||
}
|
||||
|
||||
inline int32 b2World::GetJointCount() const
|
||||
{
|
||||
return m_jointCount;
|
||||
}
|
||||
|
||||
inline int32 b2World::GetContactCount() const
|
||||
{
|
||||
return m_contactManager.m_contactCount;
|
||||
}
|
||||
|
||||
inline void b2World::SetGravity(const b2Vec2& gravity)
|
||||
{
|
||||
m_gravity = gravity;
|
||||
}
|
||||
|
||||
inline b2Vec2 b2World::GetGravity() const
|
||||
{
|
||||
return m_gravity;
|
||||
}
|
||||
|
||||
inline bool b2World::IsLocked() const
|
||||
{
|
||||
return (m_flags & e_locked) == e_locked;
|
||||
}
|
||||
|
||||
inline void b2World::SetAutoClearForces(bool flag)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
m_flags |= e_clearForces;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_flags &= ~e_clearForces;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the flag that controls automatic clearing of forces after each time step.
|
||||
inline bool b2World::GetAutoClearForces() const
|
||||
{
|
||||
return (m_flags & e_clearForces) == e_clearForces;
|
||||
}
|
||||
|
||||
inline const b2ContactManager& b2World::GetContactManager() const
|
||||
{
|
||||
return m_contactManager;
|
||||
}
|
||||
|
||||
inline const b2Profile& b2World::GetProfile() const
|
||||
{
|
||||
return m_profile;
|
||||
}
|
||||
|
||||
#endif
|
||||
155
inc/Box2D/Dynamics/b2WorldCallbacks.h
Normal file
155
inc/Box2D/Dynamics/b2WorldCallbacks.h
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_WORLD_CALLBACKS_H
|
||||
#define B2_WORLD_CALLBACKS_H
|
||||
|
||||
#include <Box2D/Common/b2Settings.h>
|
||||
|
||||
struct b2Vec2;
|
||||
struct b2Transform;
|
||||
class b2Fixture;
|
||||
class b2Body;
|
||||
class b2Joint;
|
||||
class b2Contact;
|
||||
struct b2ContactResult;
|
||||
struct b2Manifold;
|
||||
|
||||
/// Joints and fixtures are destroyed when their associated
|
||||
/// body is destroyed. Implement this listener so that you
|
||||
/// may nullify references to these joints and shapes.
|
||||
class b2DestructionListener
|
||||
{
|
||||
public:
|
||||
virtual ~b2DestructionListener() {}
|
||||
|
||||
/// Called when any joint is about to be destroyed due
|
||||
/// to the destruction of one of its attached bodies.
|
||||
virtual void SayGoodbye(b2Joint* joint) = 0;
|
||||
|
||||
/// Called when any fixture is about to be destroyed due
|
||||
/// to the destruction of its parent body.
|
||||
virtual void SayGoodbye(b2Fixture* fixture) = 0;
|
||||
};
|
||||
|
||||
/// Implement this class to provide collision filtering. In other words, you can implement
|
||||
/// this class if you want finer control over contact creation.
|
||||
class b2ContactFilter
|
||||
{
|
||||
public:
|
||||
virtual ~b2ContactFilter() {}
|
||||
|
||||
/// Return true if contact calculations should be performed between these two shapes.
|
||||
/// @warning for performance reasons this is only called when the AABBs begin to overlap.
|
||||
virtual bool ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB);
|
||||
};
|
||||
|
||||
/// Contact impulses for reporting. Impulses are used instead of forces because
|
||||
/// sub-step forces may approach infinity for rigid body collisions. These
|
||||
/// match up one-to-one with the contact points in b2Manifold.
|
||||
struct b2ContactImpulse
|
||||
{
|
||||
float32 normalImpulses[b2_maxManifoldPoints];
|
||||
float32 tangentImpulses[b2_maxManifoldPoints];
|
||||
int32 count;
|
||||
};
|
||||
|
||||
/// Implement this class to get contact information. You can use these results for
|
||||
/// things like sounds and game logic. You can also get contact results by
|
||||
/// traversing the contact lists after the time step. However, you might miss
|
||||
/// some contacts because continuous physics leads to sub-stepping.
|
||||
/// Additionally you may receive multiple callbacks for the same contact in a
|
||||
/// single time step.
|
||||
/// You should strive to make your callbacks efficient because there may be
|
||||
/// many callbacks per time step.
|
||||
/// @warning You cannot create/destroy Box2D entities inside these callbacks.
|
||||
class b2ContactListener
|
||||
{
|
||||
public:
|
||||
virtual ~b2ContactListener() {}
|
||||
|
||||
/// Called when two fixtures begin to touch.
|
||||
virtual void BeginContact(b2Contact* contact) { B2_NOT_USED(contact); }
|
||||
|
||||
/// Called when two fixtures cease to touch.
|
||||
virtual void EndContact(b2Contact* contact) { B2_NOT_USED(contact); }
|
||||
|
||||
/// This is called after a contact is updated. This allows you to inspect a
|
||||
/// contact before it goes to the solver. If you are careful, you can modify the
|
||||
/// contact manifold (e.g. disable contact).
|
||||
/// A copy of the old manifold is provided so that you can detect changes.
|
||||
/// Note: this is called only for awake bodies.
|
||||
/// Note: this is called even when the number of contact points is zero.
|
||||
/// Note: this is not called for sensors.
|
||||
/// Note: if you set the number of contact points to zero, you will not
|
||||
/// get an EndContact callback. However, you may get a BeginContact callback
|
||||
/// the next step.
|
||||
virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
|
||||
{
|
||||
B2_NOT_USED(contact);
|
||||
B2_NOT_USED(oldManifold);
|
||||
}
|
||||
|
||||
/// This lets you inspect a contact after the solver is finished. This is useful
|
||||
/// for inspecting impulses.
|
||||
/// Note: the contact manifold does not include time of impact impulses, which can be
|
||||
/// arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly
|
||||
/// in a separate data structure.
|
||||
/// Note: this is only called for contacts that are touching, solid, and awake.
|
||||
virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
|
||||
{
|
||||
B2_NOT_USED(contact);
|
||||
B2_NOT_USED(impulse);
|
||||
}
|
||||
};
|
||||
|
||||
/// Callback class for AABB queries.
|
||||
/// See b2World::Query
|
||||
class b2QueryCallback
|
||||
{
|
||||
public:
|
||||
virtual ~b2QueryCallback() {}
|
||||
|
||||
/// Called for each fixture found in the query AABB.
|
||||
/// @return false to terminate the query.
|
||||
virtual bool ReportFixture(b2Fixture* fixture) = 0;
|
||||
};
|
||||
|
||||
/// Callback class for ray casts.
|
||||
/// See b2World::RayCast
|
||||
class b2RayCastCallback
|
||||
{
|
||||
public:
|
||||
virtual ~b2RayCastCallback() {}
|
||||
|
||||
/// Called for each fixture found in the query. You control how the ray cast
|
||||
/// proceeds by returning a float:
|
||||
/// return -1: ignore this fixture and continue
|
||||
/// return 0: terminate the ray cast
|
||||
/// return fraction: clip the ray to this point
|
||||
/// return 1: don't clip the ray and continue
|
||||
/// @param fixture the fixture hit by the ray
|
||||
/// @param point the point of initial intersection
|
||||
/// @param normal the normal vector at the point of intersection
|
||||
/// @return -1 to filter, 0 to terminate, fraction to clip the ray for
|
||||
/// closest hit, 1 to continue
|
||||
virtual float32 ReportFixture( b2Fixture* fixture, const b2Vec2& point,
|
||||
const b2Vec2& normal, float32 fraction) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
115
inc/Box2D/Rope/b2Rope.h
Normal file
115
inc/Box2D/Rope/b2Rope.h
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright (c) 2011 Erin Catto http://www.box2d.org
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B2_ROPE_H
|
||||
#define B2_ROPE_H
|
||||
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
|
||||
class b2Draw;
|
||||
|
||||
///
|
||||
struct b2RopeDef
|
||||
{
|
||||
b2RopeDef()
|
||||
{
|
||||
vertices = NULL;
|
||||
count = 0;
|
||||
masses = NULL;
|
||||
gravity.SetZero();
|
||||
damping = 0.1f;
|
||||
k2 = 0.9f;
|
||||
k3 = 0.1f;
|
||||
}
|
||||
|
||||
///
|
||||
b2Vec2* vertices;
|
||||
|
||||
///
|
||||
int32 count;
|
||||
|
||||
///
|
||||
float32* masses;
|
||||
|
||||
///
|
||||
b2Vec2 gravity;
|
||||
|
||||
///
|
||||
float32 damping;
|
||||
|
||||
/// Stretching stiffness
|
||||
float32 k2;
|
||||
|
||||
/// Bending stiffness. Values above 0.5 can make the simulation blow up.
|
||||
float32 k3;
|
||||
};
|
||||
|
||||
///
|
||||
class b2Rope
|
||||
{
|
||||
public:
|
||||
b2Rope();
|
||||
~b2Rope();
|
||||
|
||||
///
|
||||
void Initialize(const b2RopeDef* def);
|
||||
|
||||
///
|
||||
void Step(float32 timeStep, int32 iterations);
|
||||
|
||||
///
|
||||
int32 GetVertexCount() const
|
||||
{
|
||||
return m_count;
|
||||
}
|
||||
|
||||
///
|
||||
const b2Vec2* GetVertices() const
|
||||
{
|
||||
return m_ps;
|
||||
}
|
||||
|
||||
///
|
||||
void Draw(b2Draw* draw) const;
|
||||
|
||||
///
|
||||
void SetAngle(float32 angle);
|
||||
|
||||
private:
|
||||
|
||||
void SolveC2();
|
||||
void SolveC3();
|
||||
|
||||
int32 m_count;
|
||||
b2Vec2* m_ps;
|
||||
b2Vec2* m_p0s;
|
||||
b2Vec2* m_vs;
|
||||
|
||||
float32* m_ims;
|
||||
|
||||
float32* m_Ls;
|
||||
float32* m_as;
|
||||
|
||||
b2Vec2 m_gravity;
|
||||
float32 m_damping;
|
||||
|
||||
float32 m_k2;
|
||||
float32 m_k3;
|
||||
};
|
||||
|
||||
#endif
|
||||
3
inc/CMakeLists.txt
Normal file
3
inc/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
# ./inc CMakeLists.txt
|
||||
|
||||
set(EXAM_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" PARENT_SCOPE)
|
||||
176
inc/EliteInput/EInputCodes.h
Normal file
176
inc/EliteInput/EInputCodes.h
Normal file
@@ -0,0 +1,176 @@
|
||||
/*=============================================================================*/
|
||||
// Copyright 2021-2022 Elite Engine
|
||||
// Authors: Matthieu Delaere, Thomas Goussaert
|
||||
/*=============================================================================*/
|
||||
// EInputCodes.h: all codes for all possible input types.
|
||||
/*=============================================================================*/
|
||||
#ifndef ELITE_INPUT_CODES
|
||||
#define ELITE_INPUT_CODES
|
||||
namespace Elite
|
||||
{
|
||||
/*! InputType: holds what type of input this action comes from */
|
||||
enum InputType
|
||||
{
|
||||
eDefault,
|
||||
eKeyboard,
|
||||
eMouseButton,
|
||||
eMouseWheel,
|
||||
eMouseMotion
|
||||
};
|
||||
|
||||
/*! InputState: holds current state of input */
|
||||
enum InputState
|
||||
{
|
||||
eDown,
|
||||
eReleased,
|
||||
};
|
||||
|
||||
/*! InputMouseButton: holds current button of mouse used */
|
||||
enum InputMouseButton
|
||||
{
|
||||
eLeft = 1,
|
||||
eMiddle = 2,
|
||||
eRight = 3,
|
||||
};
|
||||
|
||||
/*! InputScancode: scancode based on SDL
|
||||
(which is based on the USB usage page: http://www.usb.org/developers/hidpage/Hut1_12v2.pdf) */
|
||||
enum InputScancode
|
||||
{
|
||||
eScancode_Unknown = 0,
|
||||
eScancode_A = 4,
|
||||
eScancode_B = 5,
|
||||
eScancode_C = 6,
|
||||
eScancode_D = 7,
|
||||
eScancode_E = 8,
|
||||
eScancode_F = 9,
|
||||
eScancode_G = 10,
|
||||
eScancode_H = 11,
|
||||
eScancode_I = 12,
|
||||
eScancode_J = 13,
|
||||
eScancode_K = 14,
|
||||
eScancode_L = 15,
|
||||
eScancode_M = 16,
|
||||
eScancode_N = 17,
|
||||
eScancode_O = 18,
|
||||
eScancode_P = 19,
|
||||
eScancode_Q = 20,
|
||||
eScancode_R = 21,
|
||||
eScancode_S = 22,
|
||||
eScancode_T = 23,
|
||||
eScancode_U = 24,
|
||||
eScancode_V = 25,
|
||||
eScancode_W = 26,
|
||||
eScancode_X = 27,
|
||||
eScancode_Y = 28,
|
||||
eScancode_Z = 29,
|
||||
|
||||
eScancode_1 = 30,
|
||||
eScancode_2 = 31,
|
||||
eScancode_3 = 32,
|
||||
eScancode_4 = 33,
|
||||
eScancode_5 = 34,
|
||||
eScancode_6 = 35,
|
||||
eScancode_7 = 36,
|
||||
eScancode_8 = 37,
|
||||
eScancode_9 = 38,
|
||||
eScancode_0 = 39,
|
||||
|
||||
eScancode_Return = 40,
|
||||
eScancode_Escape = 41,
|
||||
eScancode_Backspace = 42,
|
||||
eScancode_Tab = 43,
|
||||
eScancode_Space = 44,
|
||||
|
||||
eScancode_Minus = 45,
|
||||
eScancode_Equals = 46,
|
||||
eScancode_LeftBracket = 47,
|
||||
eScancode_RightBracket = 48,
|
||||
eScancode_Backslash = 49,
|
||||
eScancode_Nonuslash = 50,
|
||||
eScancode_Semicolon = 51,
|
||||
eScancode_Apostrophe = 52,
|
||||
eScancode_Grave = 53,
|
||||
eScancode_Comma = 54,
|
||||
eScancode_Period = 55,
|
||||
eScancode_Slash = 56,
|
||||
|
||||
eScancode_Capslock = 57,
|
||||
|
||||
eScancode_F1 = 58,
|
||||
eScancode_F2 = 59,
|
||||
eScancode_F3 = 60,
|
||||
eScancode_F4 = 61,
|
||||
eScancode_F5 = 62,
|
||||
eScancode_F6 = 63,
|
||||
eScancode_F7 = 64,
|
||||
eScancode_F8 = 65,
|
||||
eScancode_F9 = 66,
|
||||
eScancode_F10 = 67,
|
||||
eScancode_F11 = 68,
|
||||
eScancode_F12 = 69,
|
||||
|
||||
eScancode_PrintScreen = 70,
|
||||
eScancode_ScrollLock = 71,
|
||||
eScancode_Pause = 72,
|
||||
eScancode_Insert = 73,
|
||||
eScancode_Home = 74,
|
||||
eScancode_PageUp = 75,
|
||||
eScancode_Delete = 76,
|
||||
eScancode_End = 77,
|
||||
eScancode_PageDown = 78,
|
||||
eScancode_Right = 79,
|
||||
eScancode_Left = 80,
|
||||
eScancode_Down = 81,
|
||||
eScancode_Up = 82,
|
||||
|
||||
eScancode_NumLockClear = 83,
|
||||
eScancode_KP_Divide = 84,
|
||||
eScancode_KP_Mulitply = 85,
|
||||
eScancode_KP_Minus = 86,
|
||||
eScancode_KP_Plus = 87,
|
||||
eScancode_KP_Enter = 88,
|
||||
eScancode_KP_1 = 89,
|
||||
eScancode_KP_2 = 90,
|
||||
eScancode_KP_3 = 91,
|
||||
eScancode_KP_4 = 92,
|
||||
eScancode_KP_5 = 93,
|
||||
eScancode_KP_6 = 94,
|
||||
eScancode_KP_7 = 95,
|
||||
eScancode_KP_8 = 96,
|
||||
eScancode_KP_9 = 97,
|
||||
eScancode_KP_0 = 98,
|
||||
eScancode_KP_Period = 99,
|
||||
|
||||
eScancode_F13 = 104,
|
||||
eScancode_F14 = 105,
|
||||
eScancode_F15 = 106,
|
||||
eScancode_F16 = 107,
|
||||
eScancode_F17 = 108,
|
||||
eScancode_F18 = 109,
|
||||
eScancode_F19 = 110,
|
||||
eScancode_F20 = 111,
|
||||
eScancode_F21 = 112,
|
||||
eScancode_F22 = 113,
|
||||
eScancode_F23 = 114,
|
||||
eScancode_F24 = 115,
|
||||
|
||||
eScancode_VolumeUp = 128,
|
||||
eScancode_VolumeDown = 129,
|
||||
eScancode_LockingCapsLock = 130,
|
||||
eScancode_LockingNumLock = 131,
|
||||
eScancode_LockingScrollLock = 132,
|
||||
eScancode_KP_Comma = 133,
|
||||
eScancode_KP_EqualsSAS400 = 134,
|
||||
|
||||
eScancode_LCtrl = 224,
|
||||
eScancode_LShift = 225,
|
||||
eScancode_LAlt = 226,
|
||||
eScancode_LGUI = 227,
|
||||
eScancode_RCtrl = 228,
|
||||
eScancode_RShift = 229,
|
||||
eScancode_RAlt = 230,
|
||||
eScancode_RGUI = 231,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
60
inc/EliteInput/EInputData.h
Normal file
60
inc/EliteInput/EInputData.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/*=============================================================================*/
|
||||
// Copyright 2018-2019 Elite Engine
|
||||
// Authors: Matthieu Delaere
|
||||
/*=============================================================================*/
|
||||
// EInputData.h: all structs/data used by the input manager
|
||||
/*=============================================================================*/
|
||||
#ifndef ELITE_INPUT_DATA
|
||||
#define ELITE_INPUT_DATA
|
||||
namespace Elite
|
||||
{
|
||||
struct KeyboardData //8 bytes
|
||||
{
|
||||
int TimeStamp = 0;
|
||||
InputScancode ScanCode = eScancode_Unknown;
|
||||
|
||||
KeyboardData() = default;
|
||||
KeyboardData(int timeStamp, InputScancode keyCode) :
|
||||
TimeStamp(timeStamp), ScanCode(keyCode)
|
||||
{}
|
||||
};
|
||||
|
||||
struct MouseData //== 24 bytes
|
||||
{
|
||||
int TimeStamp = 0;
|
||||
InputMouseButton Button = InputMouseButton(0);
|
||||
int X = 0; //Position X relative to window OR amound of scroll, based on Type!
|
||||
int Y = 0; //Position Y relative to window OR amound of scroll, based on Type!
|
||||
int XRel = 0;
|
||||
int YRel = 0; //Y == Direction when scrolling (1 == UP, -1 == DOWN)
|
||||
|
||||
MouseData() = default;
|
||||
MouseData(int timeStamp, InputMouseButton button, int x, int y,
|
||||
int xRel = 0, int yRel = 0) :
|
||||
TimeStamp(timeStamp), Button(button), X(x), Y(y),
|
||||
XRel(xRel), YRel(yRel)
|
||||
{}
|
||||
};
|
||||
|
||||
union InputData //"Wasting" 16 bytes for a more user-friendly setup, SDL wastes even more memory (= 48 bytes)
|
||||
{
|
||||
MouseData MouseInputData;
|
||||
KeyboardData KeyboardInputData;
|
||||
|
||||
InputData(MouseData data) : MouseInputData(data) {}
|
||||
InputData(KeyboardData data) : KeyboardInputData(data) {}
|
||||
};
|
||||
|
||||
//=== Actual InputAction used by the InputManager ===
|
||||
struct InputAction
|
||||
{
|
||||
InputType InputActionType = InputType::eDefault;
|
||||
InputState InputActionState = eDown;
|
||||
InputData InputActionData;
|
||||
|
||||
InputAction(InputType type, InputState state, InputData data) :
|
||||
InputActionType(type), InputActionState(state), InputActionData(data) {}
|
||||
InputAction() = default;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
48
inc/EliteInput/EInputManager.cpp
Normal file
48
inc/EliteInput/EInputManager.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
//=== General Includes ===
|
||||
#include "stdafx.h"
|
||||
#include "EInputManager.h"
|
||||
using namespace Elite;
|
||||
|
||||
//=== Public Functions ===
|
||||
MouseData EInputManager::GetMouseData(InputType type, InputMouseButton button)
|
||||
{
|
||||
auto result = std::find_if(m_InputContainer.begin(), m_InputContainer.end(),
|
||||
[type, button](const InputAction& ia)
|
||||
{
|
||||
return
|
||||
(ia.InputActionType == type) &&
|
||||
(ia.InputActionData.MouseInputData.Button == button);
|
||||
});
|
||||
|
||||
if (result != m_InputContainer.end())
|
||||
return (*result).InputActionData.MouseInputData;
|
||||
return
|
||||
MouseData();
|
||||
}
|
||||
|
||||
//=== Private Functions ===
|
||||
bool EInputManager::IsKeyPresent(InputType type, InputState state, InputScancode code)
|
||||
{
|
||||
auto result = std::find_if(m_InputContainer.begin(), m_InputContainer.end(),
|
||||
[type, state, code](const InputAction& ia)
|
||||
{
|
||||
return
|
||||
(ia.InputActionType == type) &&
|
||||
(ia.InputActionState == state) &&
|
||||
(ia.InputActionData.KeyboardInputData.ScanCode == code);
|
||||
});
|
||||
return (result != m_InputContainer.end());
|
||||
}
|
||||
|
||||
bool EInputManager::IsMousePresent(InputType type, InputState state, InputMouseButton button)
|
||||
{
|
||||
auto result = std::find_if(m_InputContainer.begin(), m_InputContainer.end(),
|
||||
[type, state, button](const InputAction& ia)
|
||||
{
|
||||
return
|
||||
(ia.InputActionType == type) &&
|
||||
(ia.InputActionState == state) &&
|
||||
(ia.InputActionData.MouseInputData.Button == button);
|
||||
});
|
||||
return (result != m_InputContainer.end());
|
||||
}
|
||||
49
inc/EliteInput/EInputManager.h
Normal file
49
inc/EliteInput/EInputManager.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*=============================================================================*/
|
||||
// Copyright 2021-2022 Elite Engine
|
||||
// Authors: Matthieu Delaere
|
||||
/*=============================================================================*/
|
||||
// EInputManager.h: manager class that controls the input in the engine.
|
||||
/*=============================================================================*/
|
||||
#ifndef ELITE_INPUT_MANAGER
|
||||
#define ELITE_INPUT_MANAGER
|
||||
|
||||
namespace Elite
|
||||
{
|
||||
//=== Forward Declaration ===
|
||||
#if (PLATFORM_ID == PLATFORM_WINDOWS)
|
||||
class SDLWindow;
|
||||
#endif
|
||||
|
||||
/*! EInputManager: manager class that controls all the input, captured from active platform & window*/
|
||||
class EInputManager final : public ESingleton<EInputManager>
|
||||
{
|
||||
public:
|
||||
bool IsKeyboardKeyDown(InputScancode key) { return IsKeyPresent(eKeyboard, eDown, key); };
|
||||
bool IsKeyboardKeyUp(InputScancode key) { return IsKeyPresent(eKeyboard, eReleased, key); }
|
||||
|
||||
bool IsMouseButtonDown(InputMouseButton button) { return IsMousePresent(eMouseButton, eDown, button); }
|
||||
bool IsMouseButtonUp(InputMouseButton button) { return IsMousePresent(eMouseButton, eReleased, button); }
|
||||
bool IsMouseScrolling() { return IsMousePresent(eMouseWheel); }
|
||||
bool IsMouseMoving() { return IsMousePresent(eMouseMotion); }
|
||||
MouseData GetMouseData(InputType type, InputMouseButton button = InputMouseButton(0));
|
||||
|
||||
private:
|
||||
//=== Friends ===
|
||||
//Our window has access to add input events to our queue, our application can later use these events
|
||||
#if (PLATFORM_ID == PLATFORM_WINDOWS)
|
||||
friend SDLWindow;
|
||||
#endif
|
||||
|
||||
//=== Internal Functions
|
||||
void Flush(){ m_InputContainer.clear();};
|
||||
void AddInputAction(const InputAction& inputAction)
|
||||
{ m_InputContainer.push_back(inputAction); };
|
||||
|
||||
bool IsKeyPresent(InputType type, InputState state, InputScancode code);
|
||||
bool IsMousePresent(InputType type, InputState state = InputState(0), InputMouseButton button = InputMouseButton(0));
|
||||
|
||||
//=== Datamembers ===
|
||||
std::vector<InputAction> m_InputContainer;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
110
inc/EliteMath/EMat22.h
Normal file
110
inc/EliteMath/EMat22.h
Normal file
@@ -0,0 +1,110 @@
|
||||
/*=============================================================================*/
|
||||
// Copyright 2021-2022 Elite Engine
|
||||
// Authors: Matthieu Delaere
|
||||
/*=============================================================================*/
|
||||
// EMat22.h: Row Major Matrix 2x2 struct
|
||||
// | x1 , y1 |
|
||||
// | x2 , y2 |
|
||||
// Info: Row Major Matrix for cache coherency, even though this is a small piece of data,
|
||||
// it is a good practice.
|
||||
/*=============================================================================*/
|
||||
#ifndef ELITE_MATH_MATRIX22
|
||||
#define ELITE_MATH_MATRIX22
|
||||
|
||||
namespace Elite {
|
||||
|
||||
#define IdentityMat22 Mat22();
|
||||
|
||||
//Matrix 2x2
|
||||
struct Mat22
|
||||
{
|
||||
//=== Datamembers ===
|
||||
Vector2 r[2] = { {1.f,0.f},{0.f, 1.f} };
|
||||
|
||||
//=== Constructors ===
|
||||
Mat22() {};
|
||||
Mat22(float x1, float y1, float x2, float y2)
|
||||
{ r[0] = Vector2(x1, y1); r[1] = Vector2(x2, y2); };
|
||||
Mat22(const Vector2& r1, const Vector2& r2)
|
||||
{ r[0] = r1; r[1] = r2; };
|
||||
|
||||
//=== Matrix Conversions Functions ===
|
||||
#ifdef USE_BOX2D
|
||||
//From row-major to column-major
|
||||
Mat22& operator=(const b2Mat22& m)
|
||||
{
|
||||
r[0][0] = m.ex.x; r[0][1] = m.ex.y;
|
||||
r[1][0] = m.ey.x; r[1][1] = m.ey.y;
|
||||
return *this;
|
||||
}
|
||||
operator b2Mat22() const
|
||||
{
|
||||
return b2Mat22(r[0][0], r[1][0], r[0][1], r[1][1]);
|
||||
}
|
||||
#endif
|
||||
|
||||
//=== Arithmetic Operators ===
|
||||
inline auto operator+(const Mat22& m) const
|
||||
{ return Mat22(r[0] + m.r[0], r[1] + m.r[1]); }
|
||||
inline auto operator-(const Mat22& m) const
|
||||
{ return Mat22(r[0] - m.r[0], r[1] - m.r[1]); }
|
||||
inline auto operator*(float scale) const
|
||||
{ return Mat22(r[0] * scale, r[1] * scale); }
|
||||
inline auto operator*(const Mat22& m) const
|
||||
{
|
||||
//The rows of the first matrix are multiplied by the columns of the second one
|
||||
Mat22 res = {};
|
||||
for(auto row = 0; row < 2; ++row)
|
||||
{
|
||||
for(auto col = 0; col < 2; ++col)
|
||||
{
|
||||
res.r[row][col] = r[row][0] * m.r[0][col] +
|
||||
r[row][1] * m.r[1][col];
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
inline auto operator*(const Vector2& v) const
|
||||
{
|
||||
//Same principle as mat22 * mat22, but with a "column" Vector2...
|
||||
return Vector2(
|
||||
r[0][0] * v.x + r[0][1] * v.y,
|
||||
r[1][0] * v.x + r[1][1] * v.y);
|
||||
}
|
||||
|
||||
//=== Compound Assignment Operators ===
|
||||
inline auto& operator+=(const Mat22& m)
|
||||
{ r[0] += m.r[0]; r[1] += m.r[1]; return *this; }
|
||||
inline auto& operator-=(const Mat22& m)
|
||||
{ r[0] -= m.r[0]; r[1] -= m.r[1]; return *this; }
|
||||
inline auto& operator*=(float scale)
|
||||
{ r[0] *= scale; r[1] *= scale; return *this; }
|
||||
inline auto& operator*=(const Mat22& m)
|
||||
{ auto result = *this * m; return *this = result; }
|
||||
|
||||
//=== Internal Vector Functions ===
|
||||
void SetIdentity()
|
||||
{ r[0] = Vector2( 1.f, 0.f); r[1] = Vector2(0.f, 1.f); }
|
||||
auto Determinant() const //Formula: x1*y2 - y1*x2
|
||||
{ return r[0][0] * r[1][1] - r[0][1] * r[1][0]; }
|
||||
auto Inverse() const
|
||||
{
|
||||
auto det = Determinant();
|
||||
if (AreEqual(det, 0.f)) //We cannot get inverse because determinant is zero, return identity matrix instead
|
||||
return IdentityMat22;
|
||||
|
||||
det = 1.f / det;
|
||||
auto m00 = det * r[1][1], m01 = det * (-r[0][1]);
|
||||
auto m10 = det * (-r[1][0]), m11 = det * r[0][0];
|
||||
return Mat22(m00, m01, m10, m11);
|
||||
}
|
||||
};
|
||||
|
||||
//=== Global Matrix Functions ===
|
||||
inline auto GetDeterminant(const Mat22& m)
|
||||
{ return m.Determinant(); }
|
||||
|
||||
inline auto GetInverse(const Mat22& m)
|
||||
{ return m.Inverse(); }
|
||||
}
|
||||
#endif
|
||||
21
inc/EliteMath/EMath.h
Normal file
21
inc/EliteMath/EMath.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/*=============================================================================*/
|
||||
// Copyright 2021-2022 Elite Engine
|
||||
// Authors: Matthieu Delaere
|
||||
/*=============================================================================*/
|
||||
// EMath.h: General Math header that includes all math types and utilities
|
||||
/*=============================================================================*/
|
||||
#ifndef ELITE_MATH
|
||||
#define ELITE_MATH
|
||||
|
||||
/* --- STANDARD --- */
|
||||
#include <math.h>
|
||||
/* --- UTILITIES --- */
|
||||
#include "EMathUtilities.h"
|
||||
/* --- TYPES --- */
|
||||
#include "EVector2.h"
|
||||
#include "EVector3.h"
|
||||
#include "EMat22.h"
|
||||
#include "FMatrix.h"
|
||||
|
||||
/* --- TYPE DEFINES --- */
|
||||
#endif
|
||||
133
inc/EliteMath/EMathUtilities.h
Normal file
133
inc/EliteMath/EMathUtilities.h
Normal file
@@ -0,0 +1,133 @@
|
||||
/*=============================================================================*/
|
||||
// Copyright 2021-2022 Elite Engine
|
||||
// Authors: Matthieu Delaere, Thomas Goussaert
|
||||
/*=============================================================================*/
|
||||
// EMathUtilities.h: Utility class containing a bunch of commonely used functionality (not custom-type specific)
|
||||
/*=============================================================================*/
|
||||
#ifndef ELITE_MATH_UTILITIES
|
||||
#define ELITE_MATH_UTILITIES
|
||||
//Standard C++ includes
|
||||
#include <cstdlib>
|
||||
#include <cfloat>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Elite {
|
||||
/* --- CONSTANTS --- */
|
||||
#define E_PI 3.14159265358979323846
|
||||
#define E_PI_2 1.57079632679489661923
|
||||
#define E_PI_4 0.785398163397448309616
|
||||
|
||||
/* --- FUNCTIONS --- */
|
||||
/*! Comparing two values (preferably float or doubles) and see if they are equal. You can change the precision (by default: epsilon)*/
|
||||
template<typename T, typename = std::enable_if<std::is_pod<T>::value>>
|
||||
constexpr bool AreEqual(T a, T b, float precision = FLT_EPSILON)
|
||||
{
|
||||
if (abs(a - b) > precision)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
/*! An accurate inverse square root*/
|
||||
inline float InvSqrt(float f) //sqrtf not defined as constexpr
|
||||
{
|
||||
return 1.0f / sqrtf(f);
|
||||
}
|
||||
/*! An fast inverse square root, not fully accurate. Implementation based on Quake III Arena*/
|
||||
/*! Reference: https://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/ */
|
||||
inline float InvSqrtFst(float f)
|
||||
{
|
||||
const float xHalf = 0.5f * f;
|
||||
int i = *reinterpret_cast<int*>(&f);
|
||||
i = 0x5f3759df - (i >> 1);
|
||||
f = *reinterpret_cast<float*>(&i);
|
||||
f = f*(1.5f - xHalf*f*f);
|
||||
return f;
|
||||
}
|
||||
/*! Function to square a number */
|
||||
template<typename T, typename = std::enable_if<std::is_pod<T>::value>>
|
||||
constexpr auto Square(T v)
|
||||
{
|
||||
return v*v;
|
||||
}
|
||||
/*! Function to convert degrees to radians */
|
||||
constexpr float ToRadians(const float angle)
|
||||
{
|
||||
return angle * (static_cast<float>(E_PI) / 180.f);
|
||||
}
|
||||
/*! Function to convert radians to degrees */
|
||||
constexpr float ToDegrees(const float angle)
|
||||
{
|
||||
return angle * (180.f / static_cast<float>(E_PI));
|
||||
}
|
||||
/*! Clamped angle between -pi, pi (in radians) */
|
||||
inline float ClampedAngle(const float radians)
|
||||
{
|
||||
float a = fmodf(radians + b2_pi, 2 * b2_pi);
|
||||
a = a >= 0 ? (a - b2_pi) : (a + b2_pi);
|
||||
return a;
|
||||
}
|
||||
/*! Template function to clamp between a minimum and a maximum value -> in STD since c++17 */
|
||||
template<typename T>
|
||||
constexpr T Clamp(const T a, T min, T max)
|
||||
{
|
||||
if (a < min)
|
||||
return min;
|
||||
|
||||
if (a > max)
|
||||
return max;
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
/*! Template function to clamp between a minimum and a maximum value*/
|
||||
template<typename T>
|
||||
constexpr T ClampRef(T& a, T min, T max)
|
||||
{
|
||||
if (a < min)
|
||||
a = min;
|
||||
|
||||
if (a > max)
|
||||
a = max;
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
/*! Random Integer */
|
||||
inline int randomInt(int max = 1)
|
||||
{ return rand() % max; }
|
||||
|
||||
/*! Random Float */
|
||||
inline float randomFloat(float max = 1.f)
|
||||
{ return max * (float(rand()) / RAND_MAX); }
|
||||
|
||||
/*! Random Float */
|
||||
inline float randomFloat(float min, float max)
|
||||
{
|
||||
auto range = max - min;
|
||||
return (range * (float(rand()) / RAND_MAX)) + min;
|
||||
}
|
||||
|
||||
/*! Random Binomial Float */
|
||||
inline float randomBinomial(float max = 1.f)
|
||||
{ return randomFloat(max) - randomFloat(max); }
|
||||
|
||||
/*! Linear Interpolation */
|
||||
/*inline float Lerp(float v0, float v1, float t)
|
||||
{ return (1 - t) * v0 + t * v1; }*/
|
||||
template<typename T>
|
||||
inline T Lerp(T v0, T v1, float t)
|
||||
{ return (1 - t) * v0 + t * v1; }
|
||||
|
||||
/*! Smooth Step */
|
||||
inline float smoothStep(float edge0, float edge1, float x)
|
||||
{
|
||||
// Scale, bias and saturate x to 0..1 range
|
||||
x = Clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
|
||||
// Evaluate polynomial
|
||||
return x * x * (3 - 2 * x);
|
||||
}
|
||||
|
||||
/*! Sign Function*/
|
||||
template <typename T> int sign(T val)
|
||||
{ return (T(0) < val) - (val < T(0)); }
|
||||
}
|
||||
#endif
|
||||
158
inc/EliteMath/EMatrix2x3.cpp
Normal file
158
inc/EliteMath/EMatrix2x3.cpp
Normal file
@@ -0,0 +1,158 @@
|
||||
#include "stdafx.h"
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "EMatrix2x3.h"
|
||||
|
||||
|
||||
Matrix2x3::Matrix2x3(Elite::Vector2 dirX, Elite::Vector2 dirY, Elite::Vector2 orig)
|
||||
: dirX{ dirX }, dirY{ dirY }, orig{ orig }
|
||||
{}
|
||||
|
||||
Matrix2x3::Matrix2x3(float e1X, float e1Y, float e2X, float e2Y, float oX, float oY)
|
||||
: Matrix2x3{ Elite::Vector2{e1X, e1Y}, Elite::Vector2{e2X, e2Y}, Elite::Vector2{oX, oY} }
|
||||
{}
|
||||
|
||||
Elite::Vector2 Matrix2x3::Transform(const Elite::Vector2& vector) const
|
||||
{
|
||||
return Elite::Vector2{ vector.x * dirX + vector.y * dirY } + orig;
|
||||
}
|
||||
|
||||
float Matrix2x3::Determinant() const
|
||||
{
|
||||
return dirX.x * dirY.y - dirX.y * dirY.x;
|
||||
}
|
||||
|
||||
Matrix2x3 Matrix2x3::Inverse() const
|
||||
{
|
||||
//Calculate Determinant
|
||||
float det = Determinant();
|
||||
|
||||
//1)calculate matrix of minors
|
||||
//2)Use the alternating law of signs to produce the matrix of cofactors
|
||||
//3)Transpose
|
||||
//4)the inverse matrix is 1/Determinant * the resulting matrix
|
||||
return Matrix2x3{
|
||||
Elite::Vector2(+dirY.y, -dirX.y) / det,
|
||||
Elite::Vector2(-dirY.x, +dirX.x) / det,
|
||||
Elite::Vector2(dirY.x * orig.y - dirY.y * orig.x, -(dirX.x * orig.y - dirX.y * orig.x)) / det
|
||||
};
|
||||
}
|
||||
|
||||
bool Matrix2x3::Equals(const Matrix2x3& other, float epsilon ) const
|
||||
{
|
||||
return (dirX == other.dirX) &&
|
||||
(dirY == other.dirY) &&
|
||||
(orig == other.orig);
|
||||
}
|
||||
|
||||
std::string Matrix2x3::ToString() const
|
||||
{
|
||||
return std::string( "Matrix2x3( x( ") +
|
||||
std::to_string(dirX.x) + ", " + std::to_string( dirX.y )
|
||||
+ " ), y( " + std::to_string( dirY.x ) + ", " + std::to_string( dirY.y )
|
||||
+ " ), orig( " + std::to_string( orig.x ) + ", " + std::to_string( orig.y ) + " ) )";
|
||||
}
|
||||
|
||||
void Matrix2x3::SetAsIdentity()
|
||||
{
|
||||
dirX = Elite::Vector2{1, 0};
|
||||
dirY = Elite::Vector2{0, 1};
|
||||
orig = Elite::Vector2{0, 0};
|
||||
}
|
||||
|
||||
void Matrix2x3::SetAsRotate(float degrees)
|
||||
{
|
||||
float radians = degrees * 3.1415926535f / 180;
|
||||
dirX = Elite::Vector2{ cos( radians ), sin( radians ) };
|
||||
dirY = Elite::Vector2{ -sin( radians ), cos( radians ) };
|
||||
orig = Elite::Vector2{ 0, 0 };
|
||||
}
|
||||
void Matrix2x3::SetAsTranslate(float tx, float ty)
|
||||
{
|
||||
dirX = Elite::Vector2{ 1, 0 };
|
||||
dirY = Elite::Vector2{ 0, 1 };
|
||||
orig = Elite::Vector2{ tx, ty };
|
||||
}
|
||||
|
||||
void Matrix2x3::SetAsTranslate(Elite::Vector2 pt)
|
||||
{
|
||||
dirX = Elite::Vector2{ 1, 0 };
|
||||
dirY = Elite::Vector2{ 0, 1 };
|
||||
orig = Elite::Vector2{ pt.x, pt.y };
|
||||
}
|
||||
|
||||
void Matrix2x3::SetAsScale(float scaleX, float scaleY)
|
||||
{
|
||||
dirX = Elite::Vector2{ scaleX, 0 };
|
||||
dirY = Elite::Vector2{ 0, scaleY };
|
||||
orig = Elite::Vector2{ 0, 0 };
|
||||
}
|
||||
|
||||
void Matrix2x3::SetAsScale(float scale)
|
||||
{
|
||||
SetAsScale(scale, scale);
|
||||
}
|
||||
|
||||
Matrix2x3 Matrix2x3::CreateRotationMatrix(float degrees)
|
||||
{
|
||||
float radians = degrees * 3.1415926535f / 180;
|
||||
return Matrix2x3( Elite::Vector2{ cos( radians ), sin( radians ) }, Elite::Vector2{ -sin(radians), cos( radians ) }, Elite::Vector2{} );
|
||||
}
|
||||
|
||||
Matrix2x3 Matrix2x3::CreateIdentityMatrix()
|
||||
{
|
||||
return Matrix2x3( Elite::Vector2{ 1, 0 }, Elite::Vector2{ 0, 1 }, Elite::Vector2{} );
|
||||
}
|
||||
|
||||
Matrix2x3 Matrix2x3::CreateScalingMatrix(float scale)
|
||||
{
|
||||
return CreateScalingMatrix(scale, scale);
|
||||
}
|
||||
|
||||
Matrix2x3 Matrix2x3::CreateScalingMatrix(Elite::Vector2 scaleVector)
|
||||
{
|
||||
return CreateScalingMatrix(scaleVector.x, scaleVector.y);
|
||||
}
|
||||
|
||||
Matrix2x3 Matrix2x3::CreateScalingMatrix(float scaleX, float scaleY)
|
||||
{
|
||||
return Matrix2x3( Elite::Vector2{ scaleX, 0 }, Elite::Vector2{ 0, scaleY }, Elite::Vector2{} );
|
||||
}
|
||||
|
||||
Matrix2x3 Matrix2x3::CreateTranslationMatrix(Elite::Vector2 origin)
|
||||
{
|
||||
return Matrix2x3( Elite::Vector2{ 1, 0 }, Elite::Vector2{ 0, 1 }, origin );
|
||||
}
|
||||
|
||||
Matrix2x3 Matrix2x3::CreateTranslationMatrix(float tx, float ty)
|
||||
{
|
||||
return CreateTranslationMatrix( Elite::Vector2{ tx, ty } );
|
||||
}
|
||||
|
||||
// Operator overloading functionality
|
||||
bool operator==(const Matrix2x3& lhs, const Matrix2x3& rhs)
|
||||
{
|
||||
return lhs.Equals(rhs);
|
||||
}
|
||||
|
||||
bool operator!=(const Matrix2x3& lhs, const Matrix2x3& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
Matrix2x3 operator*(const Matrix2x3& lhs, const Matrix2x3& rhs)
|
||||
{
|
||||
return Matrix2x3{
|
||||
Elite::Vector2{ rhs.dirX.x * lhs.dirX.x + rhs.dirX.y * lhs.dirY.x, rhs.dirX.x * lhs.dirX.y + rhs.dirX.y * lhs.dirY.y },
|
||||
Elite::Vector2{ rhs.dirY.x * lhs.dirX.x + rhs.dirY.y * lhs.dirY.x, rhs.dirY.x * lhs.dirX.y + rhs.dirY.y * lhs.dirY.y },
|
||||
Elite::Vector2{ rhs.orig.x * lhs.dirX.x + rhs.orig.y * lhs.dirY.x + lhs.orig.x, rhs.orig.x * lhs.dirX.y + rhs.orig.y * lhs.dirY.y + lhs.orig.y }
|
||||
};
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const Matrix2x3& matrix )
|
||||
{
|
||||
os << matrix.ToString( );
|
||||
return os;
|
||||
}
|
||||
|
||||
93
inc/EliteMath/EMatrix2x3.h
Normal file
93
inc/EliteMath/EMatrix2x3.h
Normal file
@@ -0,0 +1,93 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
struct Matrix2x3
|
||||
{
|
||||
// -------------------------
|
||||
// Constructors
|
||||
// -------------------------
|
||||
// Default constructor results in a unity matrix
|
||||
explicit Matrix2x3( Elite::Vector2 dirX = Elite::Vector2{ 1, 0 }, Elite::Vector2 dirY = Elite::Vector2{ 0, 1 }, Elite::Vector2 origTrans = Elite::Vector2{ 0, 0 } );
|
||||
// Constructor, using floats, all required
|
||||
explicit Matrix2x3( float e1X, float e1Y, float e2X, float e2Y, float oX, float oY );
|
||||
|
||||
// -------------------------
|
||||
// General Methods
|
||||
// -------------------------
|
||||
// Elite::Vector2 vTransformed = mat.Transform(v);
|
||||
Elite::Vector2 Transform( const Elite::Vector2& v ) const;
|
||||
|
||||
// Calculate the determinant
|
||||
float Determinant( ) const;
|
||||
|
||||
// Calculate the inverse matrix
|
||||
Matrix2x3 Inverse( ) const;
|
||||
|
||||
// Are two matrices equal within a threshold?
|
||||
// mat1.Equals(mat2)
|
||||
bool Equals( const Matrix2x3& other, float epsilon = 0.001f ) const;
|
||||
|
||||
// Creates a string containing a text representation of the values of the matrix
|
||||
std::string ToString( ) const;
|
||||
|
||||
// Converts this matrix into a Identity matrix
|
||||
void SetAsIdentity( );
|
||||
// Converts this matrix into a Rotate matrix
|
||||
void SetAsRotate( float degrees );
|
||||
// Converts this matrix into a Translation matrix
|
||||
void SetAsTranslate( float tx, float ty );
|
||||
// Converts this matrix into a Translation matrix
|
||||
void SetAsTranslate( Elite::Vector2 pt );
|
||||
// Converts this matrix into a Scale matrix
|
||||
void SetAsScale( float sx, float sy );
|
||||
// Converts this matrix into a Scale matrix
|
||||
void SetAsScale( float s );
|
||||
|
||||
// -------------------------------------------
|
||||
// Static Matrix2x3 object creation methods
|
||||
// -------------------------------------------
|
||||
// Instantiate a rotation matrix:
|
||||
// Matrix matRot = Matrix::Rotation(45.0f);
|
||||
static Matrix2x3 CreateRotationMatrix( float degrees );
|
||||
// Instantiate an identity matrix:
|
||||
// Matrix2x3 matId = Matrix2x3::Identity();
|
||||
static Matrix2x3 CreateIdentityMatrix( );
|
||||
// Instantiate a scale matrix:
|
||||
// Matrix matScale = Matrix::Scaling(2.0f);
|
||||
static Matrix2x3 CreateScalingMatrix( float scale );
|
||||
// Instantiate a scale matrix:
|
||||
// Matrix matScale = Matrix::Scaling(2.0f,-3.0f);
|
||||
static Matrix2x3 CreateScalingMatrix( float scaleX, float scaleY );
|
||||
// Instantiate a scale matrix:
|
||||
// Matrix matScale = Matrix::Scaling( Elite::Vector2(2.0f,-3.0f) );
|
||||
static Matrix2x3 CreateScalingMatrix( Elite::Vector2 scaleVector );
|
||||
// Instantiate a translation matrix:
|
||||
// Matrix matTrans = Matrix::Translation( Elite::Vector2(2.0f,3.0f) );
|
||||
static Matrix2x3 CreateTranslationMatrix( Elite::Vector2 origin );
|
||||
// Instantiate a translation matrix:
|
||||
// Matrix matTrans = Matrix::Translation(2.0f, 3.0f);
|
||||
static Matrix2x3 CreateTranslationMatrix( float tx, float ty );
|
||||
|
||||
// -------------------------
|
||||
// Datamembers
|
||||
// -------------------------
|
||||
Elite::Vector2 dirX; // The first matrix vector (the "x-axis"), 1st column
|
||||
Elite::Vector2 dirY; // The second matrix vector (the "y-axis"), second column
|
||||
Elite::Vector2 orig; // The origin of the coordinate matrix (the "translation"), third column
|
||||
};
|
||||
|
||||
// -------------------------
|
||||
// Operators
|
||||
// -------------------------
|
||||
// Are two matrices exactly equal?
|
||||
// mat1 == mat2
|
||||
bool operator==( const Matrix2x3& lhs, const Matrix2x3& rhs );
|
||||
// Are two matrices exactly unequal?
|
||||
// mat1 != mat2
|
||||
bool operator!=( const Matrix2x3& lhs, const Matrix2x3& rhs );
|
||||
// Multiply matrices
|
||||
// Matrix2x3 matProduct {mat1 * mat2};
|
||||
Matrix2x3 operator*( const Matrix2x3& lhs, const Matrix2x3& rhs );
|
||||
// Send matrix to output stream
|
||||
// std::cout << mat;
|
||||
std::ostream& operator<<( std::ostream& os, const Matrix2x3& matrix );
|
||||
237
inc/EliteMath/EVector2.h
Normal file
237
inc/EliteMath/EVector2.h
Normal file
@@ -0,0 +1,237 @@
|
||||
/*=============================================================================*/
|
||||
// Copyright 2021-2022 Elite Engine
|
||||
// Authors: Matthieu Delaere
|
||||
/*=============================================================================*/
|
||||
// EVector2.h: Vector2D struct
|
||||
/*=============================================================================*/
|
||||
#ifndef ELITE_MATH_VECTOR2
|
||||
#define ELITE_MATH_VECTOR2
|
||||
namespace Elite
|
||||
{
|
||||
#define ZeroVector2 Vector2()
|
||||
#define UnitVector2 Vector2(1.f,1.f)
|
||||
|
||||
//Vector 2D
|
||||
struct Vector2
|
||||
{
|
||||
//=== Datamembers ===
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
|
||||
//=== Constructors ===
|
||||
Vector2() = default;
|
||||
Vector2(float _x, float _y) :x(_x), y(_y) {};
|
||||
|
||||
//=== Vector Conversions Functions ===
|
||||
#ifdef USE_BOX2D
|
||||
explicit Vector2(const b2Vec2& v) : x(v.x), y(v.y) {};
|
||||
Vector2& operator=(const b2Vec2& v) { x = v.x; y = v.y; return *this; }
|
||||
operator b2Vec2() const
|
||||
{return {x, y};};
|
||||
#endif
|
||||
|
||||
//=== Arithmetic Operators ===
|
||||
inline auto operator-(const Vector2& v) const
|
||||
{ return Vector2(x - v.x, y - v.y); }
|
||||
inline auto operator-() const
|
||||
{ return Vector2(-x, -y); }
|
||||
inline auto operator*(float scale) const
|
||||
{ return Vector2(x * scale, y * scale); }
|
||||
inline auto operator/(float scale) const
|
||||
{
|
||||
const auto revScale = 1.0f / scale;
|
||||
return Vector2(x * revScale, y * revScale);
|
||||
}
|
||||
|
||||
//=== Compound Assignment Operators === //auto& for type deduction
|
||||
inline auto& operator+=(const Vector2& v)
|
||||
{ x += v.x; y += v.y; return *this; }
|
||||
inline auto& operator-=(const Vector2& v)
|
||||
{ x -= v.x; y -= v.y; return *this; }
|
||||
inline auto& operator*=(float scale)
|
||||
{ x *= scale; y *= scale; return *this; }
|
||||
inline auto& operator/=(float scale)
|
||||
{
|
||||
const auto revScale = 1.0f / scale;
|
||||
x *= revScale; y *= revScale; return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//=== Relational Operators ===
|
||||
inline auto operator==(const Vector2& v) const /*Check if both components are equal*/
|
||||
{ return AreEqual(x, v.x) && AreEqual(y, v.y); }
|
||||
inline auto operator!=(const Vector2& v) const /*Check if one or both components are NOT equal*/
|
||||
{ return !(*this == v); }
|
||||
|
||||
//=== Member Access Operators ===
|
||||
inline float operator[](unsigned int i) const
|
||||
{
|
||||
return ((i == 0) ? x : y);
|
||||
//if (i >= 0 && i < 2)
|
||||
// return ((i == 0) ? x : y);
|
||||
//throw; /*TODO : specify error thrown;*/
|
||||
}
|
||||
inline float& operator[](unsigned int i)
|
||||
{
|
||||
return ((i == 0) ? x : y);
|
||||
//if (i >= 0 && i < 2)
|
||||
// return ((i == 0) ? x : y);
|
||||
//throw; /*TODO : specify error thrown;*/
|
||||
}
|
||||
|
||||
//=== Internal Vector Functions ===
|
||||
inline auto Dot(const Vector2& v) const
|
||||
{ return x * v.x + y * v.y; }
|
||||
|
||||
inline auto Cross(const Vector2& v) const
|
||||
{ return x * v.y - y * v.x; }
|
||||
|
||||
inline auto GetAbs() const
|
||||
{ return Vector2(abs(x), abs(y)); }
|
||||
|
||||
inline auto MagnitudeSquared() const
|
||||
{ return x*x + y*y; }
|
||||
|
||||
inline auto Magnitude() const
|
||||
{ return sqrtf(MagnitudeSquared()); }
|
||||
|
||||
inline float Normalize()
|
||||
{
|
||||
auto m = Magnitude();
|
||||
if (AreEqual(m, 0.f))
|
||||
{
|
||||
*this = ZeroVector2;
|
||||
return 0.f;
|
||||
}
|
||||
|
||||
auto invM = 1.f / m;
|
||||
x *= invM;
|
||||
y *= invM;
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
inline Vector2 GetNormalized() const /*! Returns a normalized copy of this vector. This vector does not change.*/
|
||||
{
|
||||
auto v = Vector2(*this);
|
||||
v.Normalize();
|
||||
return v;
|
||||
}
|
||||
|
||||
inline auto DistanceSquared(const Vector2& v) const
|
||||
{ return Square(v.x - x) + Square(v.y - y); }
|
||||
|
||||
inline auto Distance(const Vector2& v) const
|
||||
{ return sqrtf(DistanceSquared(v)); }
|
||||
|
||||
inline auto Clamp(float max)
|
||||
{
|
||||
auto scale = max / Magnitude();
|
||||
scale = scale < 1.f ? scale : 1.f;
|
||||
return *this * scale;
|
||||
}
|
||||
};
|
||||
|
||||
//=== Global Vector Operators ===
|
||||
#pragma region GlobalVectorOperators
|
||||
inline auto operator+(const Vector2& v, const Vector2& v2)
|
||||
{ return Vector2(v.x + v2.x, v.y + v2.y); }
|
||||
|
||||
inline auto operator* (float s, const Vector2& v)
|
||||
{ return Vector2(s * v.x, s * v.y); }
|
||||
|
||||
inline auto operator*(const Vector2& a, const Vector2& b)
|
||||
{ return Vector2(a.x*b.x, a.y*b.y); }
|
||||
|
||||
inline auto operator/ (float s, const Vector2& v)
|
||||
{
|
||||
const auto revScale = 1.0f / s;
|
||||
return Vector2(revScale * v.x, revScale * v.y);
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const Vector2& rhs)
|
||||
{
|
||||
os << "(" << rhs.x << ", " << rhs.y << " )";
|
||||
return os;
|
||||
}
|
||||
#pragma endregion //GlobalVectorOperators
|
||||
|
||||
//=== Global Vector Functions ===
|
||||
#pragma region GlobalVectorFunctions
|
||||
inline auto Dot(const Vector2& v1, const Vector2& v2)
|
||||
{ return v1.Dot(v2); }
|
||||
|
||||
inline auto Cross(const Vector2& v1, const Vector2& v2)
|
||||
{ return v1.Cross(v2); }
|
||||
|
||||
inline auto GetAbs(const Vector2& v)
|
||||
{ return v.GetAbs(); }
|
||||
|
||||
inline void Abs(Vector2& v) /*! Make absolute Vector2 of this Vector2 */
|
||||
{ v = v.GetAbs(); }
|
||||
|
||||
inline void Normalize(Vector2& v)
|
||||
{ v.Normalize(); }
|
||||
|
||||
inline auto GetNormalized(const Vector2& v)
|
||||
{ return v.GetNormalized(); }
|
||||
|
||||
inline auto DistanceSquared(const Vector2& v1, const Vector2& v2)
|
||||
{ return v1.DistanceSquared(v2); }
|
||||
|
||||
inline auto Distance(const Vector2& v1, const Vector2& v2)
|
||||
{ return v1.Distance(v2); }
|
||||
|
||||
inline auto Clamp(const Vector2& v, float max)
|
||||
{
|
||||
auto scale = max / v.Magnitude();
|
||||
scale = scale < 1.f ? scale : 1.f;
|
||||
return v * scale;
|
||||
}
|
||||
#pragma endregion //GlobalVectorFunctions
|
||||
|
||||
#pragma region ExtraFunctions
|
||||
/*! Random Vector2 */
|
||||
inline Vector2 randomVector2(float max = 1.f)
|
||||
{
|
||||
return{ randomBinomial(max),randomBinomial(max) };
|
||||
}
|
||||
inline Vector2 randomVector2(float min, float max)
|
||||
{
|
||||
return{ randomFloat(min, max),randomFloat(min, max) };
|
||||
}
|
||||
|
||||
/* Get orientation from an a velocity vector
|
||||
-- [Deprecated] -- Use VectorToAngle instead*/
|
||||
[[deprecated("Use VectorToOrientation instead")]]
|
||||
inline float GetOrientationFromVelocity(const Elite::Vector2& velocity)
|
||||
{
|
||||
if (velocity.Magnitude() == 0)
|
||||
return 0.f;
|
||||
|
||||
return atan2f(velocity.x, -velocity.y);
|
||||
}
|
||||
|
||||
/* Creates a normalized vector from an angle in radians. */
|
||||
inline Vector2 OrientationToVector(float orientation)
|
||||
{
|
||||
return Vector2(cos(orientation), sin(orientation));
|
||||
}
|
||||
|
||||
/*Calculates the orientation angle from a vector*/
|
||||
inline float VectorToOrientation(const Vector2& vector)
|
||||
{
|
||||
return atan2f(vector.y, vector.x);
|
||||
}
|
||||
|
||||
/*! Get Angle Between 2 vectors*/
|
||||
inline float AngleBetween(const Elite::Vector2& v1, const Elite::Vector2& v2) {
|
||||
float x = v1.Dot(v2);
|
||||
float y = v1.Cross(v2);
|
||||
return atan2(y, x);
|
||||
}
|
||||
|
||||
#pragma endregion //ExtraFunctions
|
||||
}
|
||||
#endif
|
||||
170
inc/EliteMath/EVector3.h
Normal file
170
inc/EliteMath/EVector3.h
Normal file
@@ -0,0 +1,170 @@
|
||||
/*=============================================================================*/
|
||||
// Copyright 2021-2022 Elite Engine
|
||||
// Authors: Matthieu Delaere
|
||||
/*=============================================================================*/
|
||||
// EVector2.h: Vector3D struct
|
||||
/*=============================================================================*/
|
||||
#ifndef ELITE_MATH_VECTOR3
|
||||
#define ELITE_MATH_VECTOR3
|
||||
namespace Elite {
|
||||
|
||||
#define ZeroVector3 Vector3()
|
||||
#define UnitVector3 Vector3(1.f,1.f,1.f)
|
||||
|
||||
//Vector 3D
|
||||
struct Vector3
|
||||
{
|
||||
//=== Datamembers ===
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float z = 0.0f;
|
||||
|
||||
//=== Constructors ===
|
||||
Vector3(){};
|
||||
Vector3(float _x, float _y, float _z):x(_x), y(_y), z(_z) {};
|
||||
explicit Vector3(const Vector2 v, float _z = 0.f):x(v.x), y(v.y), z(_z) {};
|
||||
//Vector3(const Vector3& other); //Copy Constructor
|
||||
//Vector3& operator=(const Vector3& other); //Copy Assignment Operator
|
||||
|
||||
//=== Vector Conversions Functions ===
|
||||
#ifdef USE_BOX2D
|
||||
explicit Vector3(const b2Vec3& v) : x(v.x), y(v.y), z(v.z) {};
|
||||
Vector3& operator=(const b2Vec3& v) { x = v.x; y = v.y; z = v.z; return *this; }
|
||||
explicit operator b2Vec3() const
|
||||
{ return b2Vec3(x, y, z); };
|
||||
#endif
|
||||
|
||||
//=== Arithmetic Operators ===
|
||||
inline auto operator+(const Vector3& v) const
|
||||
{ return Vector3(x + v.x, y + v.y, z + v.z); }
|
||||
inline auto operator-(const Vector3& v) const
|
||||
{ return Vector3(x - v.x, y - v.y, z - v.z); }
|
||||
inline auto operator*(float scale) const
|
||||
{ return Vector3(x * scale, y * scale, z * scale); }
|
||||
inline auto operator/(float scale) const
|
||||
{
|
||||
const auto revScale = 1.0f / scale;
|
||||
return Vector3(x * revScale, y * revScale, z * revScale);
|
||||
}
|
||||
|
||||
//=== Compound Assignment Operators === //auto& for type deduction
|
||||
inline auto& operator+=(const Vector3& v)
|
||||
{ x += v.x; y += v.y; z += v.z; return *this; }
|
||||
inline auto& operator-=(const Vector3& v)
|
||||
{ x -= v.x; y -= v.y; z -= v.z; return *this; }
|
||||
inline auto& operator*=(float scale)
|
||||
{ x *= scale; y *= scale; z *= scale; return *this; }
|
||||
inline auto& operator/=(float scale)
|
||||
{
|
||||
const auto revScale = 1.0f / scale;
|
||||
x *= revScale; y *= revScale; z *= revScale; return *this;
|
||||
}
|
||||
|
||||
//=== Relational Operators ===
|
||||
inline auto operator==(const Vector3& v) const /*Check if both components are equal*/
|
||||
{ return AreEqual(x, v.x) && AreEqual(y, v.y) && AreEqual(z, v.z); }
|
||||
inline auto operator!=(const Vector3& v) const /*Check if one or both components are NOT equal*/
|
||||
{ return !(*this == v); }
|
||||
|
||||
//=== Member Access Operators ===
|
||||
inline float operator[](unsigned int i) const
|
||||
{ return ((i == 0) ? x : y); }
|
||||
inline float& operator[](unsigned int i)
|
||||
{ return ((i == 0) ? x : y); }
|
||||
|
||||
//=== Internal Vector Functions ===
|
||||
inline auto Dot(const Vector3& v) const
|
||||
{ return x * v.x + y * v.y + z * v.z; }
|
||||
|
||||
inline auto Cross(const Vector3& v) const
|
||||
{
|
||||
return Vector3(
|
||||
y * v.z - z * v.y,
|
||||
z * v.x - x * v.z,
|
||||
x * v.y - y * v.x);
|
||||
}
|
||||
|
||||
inline auto GetAbs() const
|
||||
{ return Vector3(abs(x), abs(y), abs(z)); }
|
||||
|
||||
inline auto SqrtMagnitude() const
|
||||
{ return x*x + y*y + z*z; }
|
||||
|
||||
inline auto Magnitude() const
|
||||
{ return sqrtf(SqrtMagnitude()); }
|
||||
|
||||
inline void Normalize()
|
||||
{
|
||||
auto m = Magnitude();
|
||||
if (AreEqual(m, 0.f))
|
||||
{
|
||||
*this = ZeroVector3;
|
||||
return;
|
||||
}
|
||||
|
||||
auto invM = 1.f / m;
|
||||
x *= invM;
|
||||
y *= invM;
|
||||
z *= invM;
|
||||
}
|
||||
|
||||
inline Vector3 GetNormalized() const /*! Returns a normalized copy of this vector. This vector does not change.*/
|
||||
{
|
||||
auto v = Vector3(*this);
|
||||
v.Normalize();
|
||||
return v;
|
||||
}
|
||||
|
||||
inline auto DistanceSquared(const Vector3& v) const
|
||||
{ return Square(v.x - x) + Square(v.y - y) + Square(v.z - z); }
|
||||
|
||||
inline auto Distance(const Vector3& v) const
|
||||
{ return sqrtf(DistanceSquared(v)); }
|
||||
|
||||
inline auto Project(const Vector3& v) const
|
||||
{ return v * ((*this).Dot(v) / v.Dot(v)); }
|
||||
|
||||
inline auto Reject(const Vector3& v) const
|
||||
{ return *this - (*this).Project(v);}
|
||||
};
|
||||
|
||||
//=== Global Vector Operators ===
|
||||
#pragma region GlobalVectorOperators
|
||||
inline auto operator * (float s, const Vector3& v)
|
||||
{ return Vector3(s * v.x, s * v.y, s * v.z); }
|
||||
#pragma endregion //GlobalVectorOperators
|
||||
|
||||
//=== Global Vector Functions ===
|
||||
#pragma region GlobalVectorFunctions
|
||||
inline auto Dot(const Vector3& v1, const Vector3& v2)
|
||||
{ return v1.Dot(v2); }
|
||||
|
||||
inline auto Cross(const Vector3& v1, const Vector3& v2)
|
||||
{ return v1.Cross(v2); }
|
||||
|
||||
inline auto GetAbs(const Vector3& v)
|
||||
{ return v.GetAbs(); }
|
||||
|
||||
inline void Abs(Vector3& v) /*! Make absolute Vector3 of this Vector3 */
|
||||
{ v = v.GetAbs(); }
|
||||
|
||||
inline void Normalize(Vector3& v)
|
||||
{ v.Normalize(); }
|
||||
|
||||
inline auto GetNormalized(Vector3& v)
|
||||
{ return v.GetNormalized(); }
|
||||
|
||||
inline auto DistanceSquared(const Vector3& v1, const Vector3& v2)
|
||||
{ return v1.DistanceSquared(v2); }
|
||||
|
||||
inline auto Distance(const Vector3& v1, const Vector3& v2)
|
||||
{ return v1.Distance(v2); }
|
||||
|
||||
inline auto Project(const Vector3& v1, const Vector3& v2)
|
||||
{ return v1.Project(v2); }
|
||||
|
||||
inline auto Reject(const Vector3& v1, const Vector3& v2)
|
||||
{ return v1.Reject(v2); }
|
||||
#pragma endregion //GlobalVectorFunctions
|
||||
}
|
||||
#endif
|
||||
267
inc/EliteMath/FMatrix.h
Normal file
267
inc/EliteMath/FMatrix.h
Normal file
@@ -0,0 +1,267 @@
|
||||
/*=============================================================================*/
|
||||
// Copyright 2021-2022 Elite Engine
|
||||
// Authors: Koen Samyn
|
||||
/*=============================================================================*/
|
||||
// FMatrix.cpp: FMatrix class
|
||||
/*=============================================================================*/
|
||||
#ifndef ELITE_MATH_FMATRIX
|
||||
#define ELITE_MATH_FMATRIX
|
||||
|
||||
#include <random>
|
||||
namespace Elite
|
||||
{
|
||||
class FMatrix
|
||||
{
|
||||
public:
|
||||
FMatrix(): m_Data(nullptr), m_Rows(0), m_Columns(0), m_Size(0) {}
|
||||
FMatrix(int rows, int columns):
|
||||
m_Rows(rows),
|
||||
m_Columns(columns),
|
||||
m_Data(new float[rows * columns]),
|
||||
m_Size(rows * columns)
|
||||
{}
|
||||
|
||||
virtual ~FMatrix()
|
||||
{
|
||||
delete[] m_Data;
|
||||
m_Data = nullptr;
|
||||
}
|
||||
|
||||
void Resize(int nrOfRows, int nrOfColumns)
|
||||
{
|
||||
m_Rows = nrOfRows;
|
||||
m_Columns = nrOfColumns;
|
||||
m_Size = m_Rows * m_Columns;
|
||||
delete[] m_Data; //ATTENTION: DELETES OLD DATA IN MATRIX
|
||||
m_Data = new float[m_Size];
|
||||
}
|
||||
|
||||
void Set(int row, int column, float value)
|
||||
{
|
||||
int index = RcToIndex(row, column);
|
||||
if (index > -1 && index < m_Size)
|
||||
{
|
||||
m_Data[index] = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Wrong index! [%d, %d]\n", row, column);
|
||||
}
|
||||
}
|
||||
void SetAll(float value)
|
||||
{
|
||||
for (int i = 0; i < m_Size; ++i)
|
||||
{
|
||||
m_Data[i] = value;
|
||||
}
|
||||
}
|
||||
void SetRowAll(int row, float value)
|
||||
{
|
||||
for (int c = 0; c < m_Columns; ++c)
|
||||
{
|
||||
Set(row, c, value);
|
||||
}
|
||||
}
|
||||
void Randomize(float min, float max)
|
||||
{
|
||||
for (int i = 0; i < m_Size; ++i)
|
||||
{
|
||||
float r = min + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (max - min)));
|
||||
m_Data[i] = r;
|
||||
}
|
||||
}
|
||||
|
||||
void Add(int row, int column, float toAdd)
|
||||
{
|
||||
int index = RcToIndex(row, column);
|
||||
if (index > -1 && index < m_Size) {
|
||||
m_Data[index] += toAdd;
|
||||
}
|
||||
else {
|
||||
printf("Wrong index! [%d, %d]\n", row, column);
|
||||
}
|
||||
}
|
||||
void Add(const FMatrix& other)
|
||||
{
|
||||
int maxRows = min(GetNrOfRows(), other.GetNrOfRows());
|
||||
int maxColumns = min(GetNrOfColumns(), other.GetNrOfColumns());
|
||||
|
||||
for (int c_row = 0; c_row < maxRows; ++c_row) {
|
||||
for (int c_column = 0; c_column < maxColumns; ++c_column) {
|
||||
float oVal = other.Get(c_row, c_column);
|
||||
float thisVal = Get(c_row, c_column);
|
||||
Set(c_row, c_column, thisVal + oVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float Get(int row, int column) const
|
||||
{
|
||||
int index = RcToIndex(row, column);
|
||||
if (index > -1 && index < m_Size) {
|
||||
return m_Data[index];
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
int GetNrOfRows() const
|
||||
{
|
||||
return m_Rows;
|
||||
}
|
||||
int GetNrOfColumns() const
|
||||
{
|
||||
return m_Columns;
|
||||
}
|
||||
void MatrixMultiply(const FMatrix& op2, FMatrix& result)
|
||||
{
|
||||
int maxRows = min(GetNrOfRows(), result.GetNrOfRows());
|
||||
int maxColumns = min(op2.GetNrOfColumns(), result.GetNrOfColumns());
|
||||
|
||||
for (int c_row = 0; c_row < maxRows; ++c_row)
|
||||
{
|
||||
for (int c_column = 0; c_column < maxColumns; ++c_column)
|
||||
{
|
||||
float sum = 0;
|
||||
for (int index = 0; index < GetNrOfColumns(); ++index)
|
||||
{
|
||||
sum += Get(c_row, index) * op2.Get(index, c_column);
|
||||
}
|
||||
result.Set(c_row, c_column, sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
void ScalarMultiply(float scalar)
|
||||
{
|
||||
for (int i = 0; i < m_Size; ++i)
|
||||
{
|
||||
m_Data[i] *= scalar;
|
||||
}
|
||||
}
|
||||
|
||||
void Copy(const FMatrix& other)
|
||||
{
|
||||
int maxRows = min(GetNrOfRows(), other.GetNrOfRows());
|
||||
int maxColumns = min(GetNrOfColumns(), other.GetNrOfColumns());
|
||||
|
||||
for (int c_row = 0; c_row < maxRows; ++c_row) {
|
||||
for (int c_column = 0; c_column < maxColumns; ++c_column) {
|
||||
float oVal = other.Get(c_row, c_column);
|
||||
Set(c_row, c_column, oVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Subtract(const FMatrix& other)
|
||||
{
|
||||
int maxRows = min(GetNrOfRows(), other.GetNrOfRows());
|
||||
int maxColumns = min(GetNrOfColumns(), other.GetNrOfColumns());
|
||||
|
||||
for (int c_row = 0; c_row < maxRows; ++c_row)
|
||||
{
|
||||
for (int c_column = 0; c_column < maxColumns; ++c_column)
|
||||
{
|
||||
float oVal = other.Get(c_row, c_column);
|
||||
float thisVal = Get(c_row, c_column);
|
||||
Set(c_row, c_column, thisVal - oVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
void Sigmoid()
|
||||
{
|
||||
for (int i = 0; i < m_Size; ++i)
|
||||
{
|
||||
float val = m_Data[i];
|
||||
m_Data[i] = 1 / (1 + exp(-val));
|
||||
}
|
||||
}
|
||||
|
||||
float Sum() const
|
||||
{
|
||||
float sum = 0;
|
||||
for (int i = 0; i < m_Size; ++i)
|
||||
{
|
||||
sum += m_Data[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
float Dot(const FMatrix& op2) const
|
||||
{
|
||||
int mR = min(GetNrOfRows(), op2.GetNrOfRows());
|
||||
int mC = min(GetNrOfColumns(), op2.GetNrOfColumns());
|
||||
|
||||
float dot = 0;
|
||||
for (int c_row = 0; c_row < mR; ++c_row) {
|
||||
for (int c_column = 0; c_column < mC; ++c_column) {
|
||||
float v1 = Get(c_row, c_column);
|
||||
float v2 = Get(c_row, c_column);
|
||||
dot += v1 * v2;
|
||||
}
|
||||
}
|
||||
return dot;
|
||||
}
|
||||
float Max() const
|
||||
{
|
||||
float max = -FLT_MAX;
|
||||
for (int c_row = 0; c_row < m_Rows; ++c_row) {
|
||||
for (int c_column = 0; c_column < m_Columns; ++c_column) {
|
||||
float value = Get(c_row, c_column);
|
||||
if (value > max) {
|
||||
max = value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
float Max(int r, int c) const
|
||||
{
|
||||
float max = -FLT_MAX;
|
||||
for (int c_row = 0; c_row < m_Rows; ++c_row) {
|
||||
for (int c_column = 0; c_column < m_Columns; ++c_column) {
|
||||
float value = Get(c_row, c_column);
|
||||
if (value > max) {
|
||||
max = value;
|
||||
r = c_row;
|
||||
c = c_column;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
float MaxOfRow(int r) const
|
||||
{
|
||||
float max = -FLT_MAX;
|
||||
for (int c_column = 0; c_column < m_Columns; ++c_column) {
|
||||
float value = Get(r, c_column);
|
||||
if (value > max) {
|
||||
max = value;
|
||||
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void Print() const
|
||||
{
|
||||
for (int c_row = 0; c_row < m_Rows; ++c_row) {
|
||||
for (int c_column = 0; c_column < m_Columns; ++c_column) {
|
||||
float value = Get(c_row, c_column);
|
||||
printf("%.3f\t", value);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
private:
|
||||
float* m_Data;
|
||||
int m_Rows, m_Columns;
|
||||
int m_Size;
|
||||
int RcToIndex(int r, int c) const
|
||||
{
|
||||
return c * m_Rows + r;
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
190
inc/Exam_HelperStructs.h
Normal file
190
inc/Exam_HelperStructs.h
Normal file
@@ -0,0 +1,190 @@
|
||||
#pragma once
|
||||
#pragma region MISC
|
||||
#include <string>
|
||||
|
||||
struct SteeringPlugin_Output
|
||||
{
|
||||
Elite::Vector2 LinearVelocity = { 0.f,0.f };
|
||||
float AngularVelocity = 0.f;
|
||||
bool AutoOrient = true;
|
||||
bool RunMode = false;
|
||||
};
|
||||
|
||||
struct PluginInfo
|
||||
{
|
||||
std::string BotName = "NoName";
|
||||
std::string Student_Name = "John Cena";
|
||||
std::string Student_Class = "2DAE00";
|
||||
std::string LB_Password = "ABC123";
|
||||
};
|
||||
|
||||
struct GameDebugParams //Debuggin Purposes only (Ignored during release build)
|
||||
{
|
||||
GameDebugParams(bool spawnEnemies = true, int enemyCount = 20, bool godMode = false, bool autoFollowCam = false)
|
||||
{
|
||||
SpawnEnemies = spawnEnemies;
|
||||
GodMode = godMode;
|
||||
AutoFollowCam = autoFollowCam;
|
||||
EnemyCount = enemyCount;
|
||||
}
|
||||
|
||||
bool SpawnEnemies = true; //Spawn enemies?
|
||||
int EnemyCount = 20; //Amount of enemies?
|
||||
int ItemCount = 40; //Amount of items?
|
||||
bool GodMode = false; //Use GodMode? (Invincible)
|
||||
bool IgnoreEnergy = false; //Ignore energy depletion
|
||||
bool AutoFollowCam = false; //Auto follow the player
|
||||
bool RenderUI = false; //Render Player UI (Parameters)
|
||||
bool AutoGrabClosestItem = false; //Auto Grab closest item (Item_Grab)
|
||||
std::string LevelFile = "GameLevel.gppl"; //Level to load?
|
||||
int Seed = -1; //Seed for random generator
|
||||
int StartingDifficultyStage = 0; // Overwrites the difficulty stage
|
||||
bool InfiniteStamina = false; // Agent has infinite stamina
|
||||
bool SpawnDebugPistol = false; // Spawns pistol with 1000 ammo at start
|
||||
bool SpawnDebugShotgun = false; // Spawns shotgun with 1000 ammo at start
|
||||
bool SpawnPurgeZonesOnMiddleClick = false; // Middle mouse clicks spawn purge zones
|
||||
bool SpawnZombieOnRightClick = false; // Right mouse clicks spawn purge zones
|
||||
bool PrintDebugMessages = true;
|
||||
bool ShowDebugItemNames = true;
|
||||
};
|
||||
#pragma endregion
|
||||
|
||||
#pragma region ENTITIES
|
||||
//Enumerations
|
||||
//************
|
||||
enum class eEntityType
|
||||
{
|
||||
ITEM,
|
||||
ENEMY,
|
||||
PURGEZONE,
|
||||
|
||||
//@END
|
||||
_LAST = PURGEZONE
|
||||
};
|
||||
|
||||
enum class eItemType
|
||||
{
|
||||
PISTOL,
|
||||
SHOTGUN,
|
||||
MEDKIT,
|
||||
FOOD,
|
||||
GARBAGE,
|
||||
RANDOM_DROP, //Internal Only
|
||||
RANDOM_DROP_WITH_CHANCE, //Internal Only
|
||||
|
||||
//@END
|
||||
_LAST = GARBAGE
|
||||
};
|
||||
|
||||
enum class eEnemyType
|
||||
{
|
||||
DEFAULT,
|
||||
ZOMBIE_NORMAL,
|
||||
ZOMBIE_RUNNER,
|
||||
ZOMBIE_HEAVY,
|
||||
RANDOM_ENEMY, //Internal Only
|
||||
//...
|
||||
|
||||
//@END
|
||||
_LAST = ZOMBIE_HEAVY
|
||||
};
|
||||
|
||||
|
||||
//Structures
|
||||
//**********
|
||||
struct StatisticsInfo
|
||||
{
|
||||
int Score; //Current Score
|
||||
float Difficulty; //Current difficulty (0 > 1 > ... / Easy > Normal > Hard)
|
||||
float TimeSurvived; //Total time survived
|
||||
float KillCountdown; //Make sure to make a kill before this timer runs out
|
||||
|
||||
int NumEnemiesKilled; //Amount of enemies killed
|
||||
int NumEnemiesHit; //Amount of enemy hits
|
||||
int NumItemsPickUp; //Amount of items picked up
|
||||
int NumMissedShots; //Shots missed after firing
|
||||
int NumChkpntsReached; //Amount of checkpoints reached
|
||||
|
||||
};
|
||||
|
||||
struct FOVStats
|
||||
{
|
||||
int NumHouses;
|
||||
int NumEnemies;
|
||||
int NumItems;
|
||||
int NumPurgeZones;
|
||||
};
|
||||
|
||||
struct HouseInfo
|
||||
{
|
||||
Elite::Vector2 Center;
|
||||
Elite::Vector2 Size;
|
||||
};
|
||||
|
||||
struct EnemyInfo
|
||||
{
|
||||
eEnemyType Type;
|
||||
Elite::Vector2 Location;
|
||||
Elite::Vector2 LinearVelocity;
|
||||
|
||||
int EnemyHash = 0;
|
||||
float Size;
|
||||
float Health;
|
||||
};
|
||||
|
||||
struct ItemInfo
|
||||
{
|
||||
eItemType Type;
|
||||
Elite::Vector2 Location;
|
||||
|
||||
int ItemHash = 0;
|
||||
int Value = 0;
|
||||
};
|
||||
|
||||
struct PurgeZoneInfo
|
||||
{
|
||||
Elite::Vector2 Center;
|
||||
float Radius = 0.0f;
|
||||
|
||||
int ZoneHash = 0;
|
||||
};
|
||||
|
||||
struct EntityInfo
|
||||
{
|
||||
eEntityType Type;
|
||||
Elite::Vector2 Location;
|
||||
|
||||
int EntityHash = 0;
|
||||
};
|
||||
|
||||
struct WorldInfo
|
||||
{
|
||||
Elite::Vector2 Center;
|
||||
Elite::Vector2 Dimensions;
|
||||
};
|
||||
|
||||
struct AgentInfo
|
||||
{
|
||||
float Stamina;
|
||||
float Health;
|
||||
float Energy;
|
||||
bool RunMode;
|
||||
bool IsInHouse;
|
||||
bool Bitten; // agent was bitten by a zombie this frame (for internal use)
|
||||
bool WasBitten; // agent was bitten by a zombie recently (0.5 seconds)
|
||||
bool Death;
|
||||
|
||||
float FOV_Angle;
|
||||
float FOV_Range;
|
||||
|
||||
Elite::Vector2 LinearVelocity;
|
||||
float AngularVelocity;
|
||||
float CurrentLinearSpeed;
|
||||
Elite::Vector2 Position;
|
||||
float Orientation;
|
||||
float MaxLinearSpeed;
|
||||
float MaxAngularSpeed;
|
||||
float GrabRange;
|
||||
float AgentSize;
|
||||
};
|
||||
#pragma endregion
|
||||
1768
inc/GL/gl3w.h
Normal file
1768
inc/GL/gl3w.h
Normal file
File diff suppressed because it is too large
Load Diff
4505
inc/GL/glcorearb.h
Normal file
4505
inc/GL/glcorearb.h
Normal file
File diff suppressed because it is too large
Load Diff
29
inc/IBaseInterface.h
Normal file
29
inc/IBaseInterface.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
class IBaseInterface
|
||||
{
|
||||
public:
|
||||
IBaseInterface();
|
||||
virtual ~IBaseInterface();
|
||||
|
||||
//RENDERER
|
||||
virtual void Draw_Polygon(const Elite::Vector2* points, int count, const Elite::Vector3& color, float depth) = 0;
|
||||
void Draw_Polygon(const Elite::Vector2* points, int count, const Elite::Vector3& color);
|
||||
virtual void Draw_SolidPolygon(const Elite::Vector2* points, int count, const Elite::Vector3& color, float depth, bool triangulate = false) = 0;
|
||||
void Draw_SolidPolygon(const Elite::Vector2* points, int count, const Elite::Vector3& color);
|
||||
virtual void Draw_Circle(const Elite::Vector2& center, float radius, const Elite::Vector3& color, float depth) = 0;
|
||||
void Draw_Circle(const Elite::Vector2& center, float radius, const Elite::Vector3& color);
|
||||
virtual void Draw_SolidCircle(const Elite::Vector2& center, float32 radius, const Elite::Vector2& axis, const Elite::Vector3& color, float depth) = 0;
|
||||
void Draw_SolidCircle(const Elite::Vector2& center, float32 radius, const Elite::Vector2& axis, const Elite::Vector3& color);
|
||||
virtual void Draw_Segment(const Elite::Vector2& p1, const Elite::Vector2& p2, const Elite::Vector3& color, float depth) = 0;
|
||||
void Draw_Segment(const Elite::Vector2& p1, const Elite::Vector2& p2, const Elite::Vector3& color);
|
||||
virtual void Draw_Direction(const Elite::Vector2& p, Elite::Vector2 dir, float length, const Elite::Vector3& color, float depth = 0.9f) = 0;
|
||||
virtual void Draw_Transform(const b2Transform& xf, float depth) = 0;
|
||||
void Draw_Transform(const b2Transform& xf);
|
||||
virtual void Draw_Point(const Elite::Vector2& p, float size, const Elite::Vector3& color, float depth) = 0;
|
||||
void Draw_Point(const Elite::Vector2& p, float size, const Elite::Vector3& color);
|
||||
//void Draw_String(int x, int y, const char* string, ...) = 0;
|
||||
//virtual void Draw_String(const Elite::Vector2& pw, const char* string, ...) = 0;
|
||||
|
||||
virtual float NextDepthSlice() = 0;
|
||||
};
|
||||
|
||||
52
inc/IExamInterface.h
Normal file
52
inc/IExamInterface.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
#include "IBaseInterface.h"
|
||||
|
||||
class IExamInterface : public IBaseInterface
|
||||
{
|
||||
public:
|
||||
IExamInterface();
|
||||
~IExamInterface();
|
||||
|
||||
//WORLD & ENTITIES
|
||||
virtual WorldInfo World_GetInfo() const = 0;
|
||||
virtual StatisticsInfo World_GetStats() const = 0;
|
||||
|
||||
virtual std::vector<HouseInfo> GetHousesInFOV() const = 0;
|
||||
virtual std::vector<EnemyInfo> GetEnemiesInFOV() const = 0;
|
||||
virtual std::vector<PurgeZoneInfo> GetPurgeZonesInFOV() const = 0;
|
||||
virtual std::vector<ItemInfo> GetItemsInFOV() const = 0;
|
||||
|
||||
virtual const FOVStats& FOV_GetStats() const = 0;
|
||||
|
||||
virtual AgentInfo Agent_GetInfo() const = 0;
|
||||
|
||||
//NAVMESH
|
||||
virtual Elite::Vector2 NavMesh_GetClosestPathPoint(Elite::Vector2 goal) const = 0;
|
||||
|
||||
//INVENTORY
|
||||
virtual bool Inventory_AddItem(UINT slotId, ItemInfo item) = 0;
|
||||
virtual bool Inventory_UseItem(UINT slotId) = 0;
|
||||
virtual bool Inventory_RemoveItem(UINT slotId) = 0;
|
||||
virtual bool Inventory_GetItem(UINT slotId, ItemInfo& item) = 0;
|
||||
virtual UINT Inventory_GetCapacity() const = 0;
|
||||
|
||||
//ITEMS
|
||||
virtual bool GrabNearestItem(ItemInfo& item) = 0;
|
||||
virtual bool GrabItem(const ItemInfo& item) = 0;
|
||||
virtual bool DestroyItem(const ItemInfo& item) = 0;
|
||||
|
||||
//DEBUG
|
||||
virtual Elite::Vector2 Debug_ConvertScreenToWorld(Elite::Vector2 screenPos) const = 0;
|
||||
virtual Elite::Vector2 Debug_ConvertWorldToScreen(Elite::Vector2 worldPos) const = 0;
|
||||
|
||||
//INPUT
|
||||
virtual bool Input_IsKeyboardKeyDown(Elite::InputScancode key) const = 0;
|
||||
virtual bool Input_IsKeyboardKeyUp(Elite::InputScancode key) const = 0;
|
||||
virtual bool Input_IsMouseButtonDown(Elite::InputMouseButton button) const = 0;
|
||||
virtual bool Input_IsMouseButtonUp(Elite::InputMouseButton button) const = 0;
|
||||
virtual Elite::MouseData Input_GetMouseData(Elite::InputType type, Elite::InputMouseButton button = Elite::InputMouseButton(0)) const = 0;
|
||||
|
||||
//EVENT
|
||||
virtual void RequestShutdown() const = 0;
|
||||
};
|
||||
|
||||
23
inc/IExamPlugin.h
Normal file
23
inc/IExamPlugin.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include "IPluginBase.h"
|
||||
|
||||
struct SteeringPlugin_Output;
|
||||
struct GameDebugParams;
|
||||
|
||||
class IExamPlugin :public IPluginBase
|
||||
{
|
||||
public:
|
||||
IExamPlugin() {};
|
||||
~IExamPlugin() {};
|
||||
|
||||
void Initialize(IBaseInterface* pInterface, PluginInfo& info) override = 0;
|
||||
void DllInit() override = 0;
|
||||
void DllShutdown() override = 0;
|
||||
|
||||
virtual void InitGameDebugParams(GameDebugParams& params) {};
|
||||
virtual void Update_Debug(float dt) {};
|
||||
|
||||
virtual SteeringPlugin_Output UpdateSteering(float dt) = 0;
|
||||
virtual void Render(float dt) const = 0;
|
||||
};
|
||||
|
||||
16
inc/IPluginBase.h
Normal file
16
inc/IPluginBase.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
struct PluginInfo;
|
||||
class IBaseInterface;
|
||||
|
||||
class IPluginBase
|
||||
{
|
||||
public:
|
||||
IPluginBase(){};
|
||||
virtual ~IPluginBase(){};
|
||||
|
||||
virtual void Initialize(IBaseInterface *pInterface, PluginInfo& info) = 0;
|
||||
virtual void DllInit() = 0;
|
||||
virtual void DllShutdown() = 0;
|
||||
};
|
||||
|
||||
52
inc/ImGui/imconfig.h
Normal file
52
inc/ImGui/imconfig.h
Normal file
@@ -0,0 +1,52 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// USER IMPLEMENTATION
|
||||
// This file contains compile-time options for ImGui.
|
||||
// Other options (memory allocation overrides, callbacks, etc.) can be set at runtime via the ImGuiIO structure - ImGui::GetIO().
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
|
||||
//---- Define assertion handler. Defaults to calling assert().
|
||||
//#define IM_ASSERT(_EXPR) MyAssert(_EXPR)
|
||||
|
||||
//---- Define attributes of all API symbols declarations, e.g. for DLL under Windows.
|
||||
//#define IMGUI_API __declspec( dllexport )
|
||||
//#define IMGUI_API __declspec( dllimport )
|
||||
|
||||
//---- Include imgui_user.h at the end of imgui.h
|
||||
//#define IMGUI_INCLUDE_IMGUI_USER_H
|
||||
|
||||
//---- Don't implement default handlers for Windows (so as not to link with OpenClipboard() and others Win32 functions)
|
||||
//#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS
|
||||
//#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCS
|
||||
|
||||
//---- Don't implement help and test window functionality (ShowUserGuide()/ShowStyleEditor()/ShowTestWindow() methods will be empty)
|
||||
//#define IMGUI_DISABLE_TEST_WINDOWS
|
||||
|
||||
//---- Don't define obsolete functions names
|
||||
//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS
|
||||
|
||||
//---- Implement STB libraries in a namespace to avoid conflicts
|
||||
//#define IMGUI_STB_NAMESPACE ImGuiStb
|
||||
|
||||
//---- Define constructor and implicit cast operators to convert back<>forth from your math types and ImVec2/ImVec4.
|
||||
#include <Box2D/Common/b2Math.h>
|
||||
#define IM_VEC2_CLASS_EXTRA \
|
||||
ImVec2(const b2Vec2& f) { x = f.x; y = f.y; } \
|
||||
operator b2Vec2() const { return b2Vec2(x,y); }
|
||||
|
||||
/*
|
||||
#define IM_VEC4_CLASS_EXTRA \
|
||||
ImVec4(const MyVec4& f) { x = f.x; y = f.y; z = f.z; w = f.w; } \
|
||||
operator MyVec4() const { return MyVec4(x,y,z,w); }
|
||||
*/
|
||||
|
||||
//---- Tip: You can add extra functions within the ImGui:: namespace, here or in your own headers files.
|
||||
//---- e.g. create variants of the ImGui::Value() helper for your low-level math types, or your own widgets/helpers.
|
||||
/*
|
||||
namespace ImGui
|
||||
{
|
||||
void Value(const char* prefix, const MyMatrix44& v, const char* float_format = NULL);
|
||||
}
|
||||
*/
|
||||
|
||||
1333
inc/ImGui/imgui.h
Normal file
1333
inc/ImGui/imgui.h
Normal file
File diff suppressed because it is too large
Load Diff
25
inc/ImGui/imgui_impl_glfw_gl3.h
Normal file
25
inc/ImGui/imgui_impl_glfw_gl3.h
Normal file
@@ -0,0 +1,25 @@
|
||||
// ImGui GLFW binding with OpenGL3 + shaders
|
||||
// In this binding, ImTextureID is used to store an OpenGL 'GLuint' texture identifier. Read the FAQ about ImTextureID in imgui.cpp.
|
||||
|
||||
// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
|
||||
// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
|
||||
// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
|
||||
// https://github.com/ocornut/imgui
|
||||
|
||||
struct GLFWwindow;
|
||||
|
||||
IMGUI_API bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks);
|
||||
IMGUI_API void ImGui_ImplGlfwGL3_Shutdown();
|
||||
IMGUI_API void ImGui_ImplGlfwGL3_NewFrame();
|
||||
|
||||
// Use if you want to reset your rendering device without losing ImGui state.
|
||||
IMGUI_API void ImGui_ImplGlfwGL3_InvalidateDeviceObjects();
|
||||
IMGUI_API bool ImGui_ImplGlfwGL3_CreateDeviceObjects();
|
||||
|
||||
// GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization)
|
||||
// Provided here if you want to chain callbacks.
|
||||
// You can also handle inputs yourself and use those as a reference.
|
||||
IMGUI_API void ImGui_ImplGlfwGL3_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
|
||||
IMGUI_API void ImGui_ImplGlfwGL3_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
|
||||
IMGUI_API void ImGui_ImplGlfwGL3_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||
IMGUI_API void ImGui_ImplGlfwGL3_CharCallback(GLFWwindow* window, unsigned int c);
|
||||
744
inc/ImGui/imgui_internal.h
Normal file
744
inc/ImGui/imgui_internal.h
Normal file
@@ -0,0 +1,744 @@
|
||||
// dear imgui, v1.49 WIP
|
||||
// (internals)
|
||||
|
||||
// You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility!
|
||||
// Implement maths operators for ImVec2 (disabled by default to not collide with using IM_VEC2_CLASS_EXTRA along with your own math types+operators)
|
||||
// #define IMGUI_DEFINE_MATH_OPERATORS
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef IMGUI_VERSION
|
||||
#error Must include imgui.h before imgui_internal.h
|
||||
#endif
|
||||
|
||||
#include <stdio.h> // FILE*
|
||||
#include <math.h> // sqrtf()
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning (push)
|
||||
#pragma warning (disable: 4251) // class 'xxx' needs to have dll-interface to be used by clients of struct 'xxx' // when IMGUI_API is set to__declspec(dllexport)
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Forward Declarations
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
struct ImRect;
|
||||
struct ImGuiColMod;
|
||||
struct ImGuiStyleMod;
|
||||
struct ImGuiGroupData;
|
||||
struct ImGuiSimpleColumns;
|
||||
struct ImGuiDrawContext;
|
||||
struct ImGuiTextEditState;
|
||||
struct ImGuiIniData;
|
||||
struct ImGuiMouseCursorData;
|
||||
struct ImGuiPopupRef;
|
||||
struct ImGuiState;
|
||||
struct ImGuiWindow;
|
||||
|
||||
typedef int ImGuiLayoutType; // enum ImGuiLayoutType_
|
||||
typedef int ImGuiButtonFlags; // enum ImGuiButtonFlags_
|
||||
typedef int ImGuiTreeNodeFlags; // enum ImGuiTreeNodeFlags_
|
||||
typedef int ImGuiSliderFlags; // enum ImGuiSliderFlags_
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// STB libraries
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
namespace ImGuiStb
|
||||
{
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
#pragma clang diagnostic ignored "-Wmissing-prototypes"
|
||||
#endif
|
||||
|
||||
#undef STB_TEXTEDIT_STRING
|
||||
#undef STB_TEXTEDIT_CHARTYPE
|
||||
#define STB_TEXTEDIT_STRING ImGuiTextEditState
|
||||
#define STB_TEXTEDIT_CHARTYPE ImWchar
|
||||
#define STB_TEXTEDIT_GETWIDTH_NEWLINE -1.0f
|
||||
#include "stb_textedit.h"
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
} // namespace ImGuiStb
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Context
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
extern IMGUI_API ImGuiState* GImGui;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Helpers
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR)))
|
||||
#define IM_PI 3.14159265358979323846f
|
||||
|
||||
// Helpers: UTF-8 <> wchar
|
||||
IMGUI_API int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count
|
||||
IMGUI_API int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end); // return input UTF-8 bytes count
|
||||
IMGUI_API int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_remaining = NULL); // return input UTF-8 bytes count
|
||||
IMGUI_API int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end); // return number of UTF-8 code-points (NOT bytes count)
|
||||
IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string as UTF-8 code-points
|
||||
|
||||
// Helpers: Misc
|
||||
IMGUI_API ImU32 ImHash(const void* data, int data_size, ImU32 seed = 0); // Pass data_size==0 for zero-terminated strings
|
||||
IMGUI_API void* ImLoadFileToMemory(const char* filename, const char* file_open_mode, int* out_file_size = NULL, int padding_bytes = 0);
|
||||
IMGUI_API bool ImIsPointInTriangle(const ImVec2& p, const ImVec2& a, const ImVec2& b, const ImVec2& c);
|
||||
static inline bool ImCharIsSpace(int c) { return c == ' ' || c == '\t' || c == 0x3000; }
|
||||
static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; }
|
||||
|
||||
// Helpers: String
|
||||
IMGUI_API int ImStricmp(const char* str1, const char* str2);
|
||||
IMGUI_API int ImStrnicmp(const char* str1, const char* str2, int count);
|
||||
IMGUI_API char* ImStrdup(const char* str);
|
||||
IMGUI_API int ImStrlenW(const ImWchar* str);
|
||||
IMGUI_API const ImWchar*ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin); // Find beginning-of-line
|
||||
IMGUI_API const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end);
|
||||
IMGUI_API int ImFormatString(char* buf, int buf_size, const char* fmt, ...) IM_PRINTFARGS(3);
|
||||
IMGUI_API int ImFormatStringV(char* buf, int buf_size, const char* fmt, va_list args);
|
||||
|
||||
// Helpers: Math
|
||||
// We are keeping those not leaking to the user by default, in the case the user has implicit cast operators between ImVec2 and its own types (when IM_VEC2_CLASS_EXTRA is defined)
|
||||
#ifdef IMGUI_DEFINE_MATH_OPERATORS
|
||||
static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x*rhs, lhs.y*rhs); }
|
||||
static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x/rhs, lhs.y/rhs); }
|
||||
static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x+rhs.x, lhs.y+rhs.y); }
|
||||
static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x-rhs.x, lhs.y-rhs.y); }
|
||||
static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x*rhs.x, lhs.y*rhs.y); }
|
||||
static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x/rhs.x, lhs.y/rhs.y); }
|
||||
static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; }
|
||||
static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; }
|
||||
static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; }
|
||||
static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; }
|
||||
static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x-rhs.x, lhs.y-rhs.y, lhs.z-rhs.z, lhs.w-rhs.w); }
|
||||
#endif
|
||||
|
||||
static inline int ImMin(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; }
|
||||
static inline int ImMax(int lhs, int rhs) { return lhs >= rhs ? lhs : rhs; }
|
||||
static inline float ImMin(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; }
|
||||
static inline float ImMax(float lhs, float rhs) { return lhs >= rhs ? lhs : rhs; }
|
||||
static inline ImVec2 ImMin(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(ImMin(lhs.x,rhs.x), ImMin(lhs.y,rhs.y)); }
|
||||
static inline ImVec2 ImMax(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(ImMax(lhs.x,rhs.x), ImMax(lhs.y,rhs.y)); }
|
||||
static inline int ImClamp(int v, int mn, int mx) { return (v < mn) ? mn : (v > mx) ? mx : v; }
|
||||
static inline float ImClamp(float v, float mn, float mx) { return (v < mn) ? mn : (v > mx) ? mx : v; }
|
||||
static inline ImVec2 ImClamp(const ImVec2& f, const ImVec2& mn, ImVec2 mx) { return ImVec2(ImClamp(f.x,mn.x,mx.x), ImClamp(f.y,mn.y,mx.y)); }
|
||||
static inline float ImSaturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; }
|
||||
static inline float ImLerp(float a, float b, float t) { return a + (b - a) * t; }
|
||||
static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, const ImVec2& t) { return ImVec2(a.x + (b.x - a.x) * t.x, a.y + (b.y - a.y) * t.y); }
|
||||
static inline float ImLengthSqr(const ImVec2& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y; }
|
||||
static inline float ImLengthSqr(const ImVec4& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y + lhs.z*lhs.z + lhs.w*lhs.w; }
|
||||
static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = lhs.x*lhs.x + lhs.y*lhs.y; if (d > 0.0f) return 1.0f / sqrtf(d); return fail_value; }
|
||||
static inline ImVec2 ImRound(ImVec2 v) { return ImVec2((float)(int)v.x, (float)(int)v.y); }
|
||||
|
||||
// We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax.
|
||||
// Defining a custom placement new() with a dummy parameter allows us to bypass including <new> which on some platforms complains when user has disabled exceptions.
|
||||
#ifdef IMGUI_DEFINE_PLACEMENT_NEW
|
||||
struct ImPlacementNewDummy {};
|
||||
inline void* operator new(size_t, ImPlacementNewDummy, void* ptr) { return ptr; }
|
||||
inline void operator delete(void*, ImPlacementNewDummy, void*) {}
|
||||
#define IM_PLACEMENT_NEW(_PTR) new(ImPlacementNewDummy() ,_PTR)
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Types
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
enum ImGuiButtonFlags_
|
||||
{
|
||||
ImGuiButtonFlags_Repeat = 1 << 0, // hold to repeat
|
||||
ImGuiButtonFlags_PressedOnClick = 1 << 1, // return pressed on click (default requires click+release)
|
||||
ImGuiButtonFlags_PressedOnRelease = 1 << 2, // return pressed on release (default requires click+release)
|
||||
ImGuiButtonFlags_PressedOnDoubleClick = 1 << 3, // return pressed on double-click (default requires click+release)
|
||||
ImGuiButtonFlags_FlattenChilds = 1 << 4, // allow interaction even if a child window is overlapping
|
||||
ImGuiButtonFlags_DontClosePopups = 1 << 5, // disable automatically closing parent popup on press
|
||||
ImGuiButtonFlags_Disabled = 1 << 6, // disable interaction
|
||||
ImGuiButtonFlags_AlignTextBaseLine = 1 << 7, // vertically align button to match text baseline - ButtonEx() only
|
||||
ImGuiButtonFlags_NoKeyModifiers = 1 << 8 // disable interaction if a key modifier is held
|
||||
};
|
||||
|
||||
enum ImGuiTreeNodeFlags_
|
||||
{
|
||||
ImGuiTreeNodeFlags_DefaultOpen = 1 << 0,
|
||||
ImGuiTreeNodeFlags_NoAutoExpandOnLog = 1 << 1
|
||||
};
|
||||
|
||||
enum ImGuiSliderFlags_
|
||||
{
|
||||
ImGuiSliderFlags_Vertical = 1 << 0
|
||||
};
|
||||
|
||||
enum ImGuiSelectableFlagsPrivate_
|
||||
{
|
||||
// NB: need to be in sync with last value of ImGuiSelectableFlags_
|
||||
ImGuiSelectableFlags_Menu = 1 << 3,
|
||||
ImGuiSelectableFlags_MenuItem = 1 << 4,
|
||||
ImGuiSelectableFlags_Disabled = 1 << 5,
|
||||
ImGuiSelectableFlags_DrawFillAvailWidth = 1 << 6
|
||||
};
|
||||
|
||||
// FIXME: this is in development, not exposed/functional as a generic feature yet.
|
||||
enum ImGuiLayoutType_
|
||||
{
|
||||
ImGuiLayoutType_Vertical,
|
||||
ImGuiLayoutType_Horizontal
|
||||
};
|
||||
|
||||
enum ImGuiPlotType
|
||||
{
|
||||
ImGuiPlotType_Lines,
|
||||
ImGuiPlotType_Histogram
|
||||
};
|
||||
|
||||
enum ImGuiDataType
|
||||
{
|
||||
ImGuiDataType_Int,
|
||||
ImGuiDataType_Float
|
||||
};
|
||||
|
||||
// 2D axis aligned bounding-box
|
||||
// NB: we can't rely on ImVec2 math operators being available here
|
||||
struct IMGUI_API ImRect
|
||||
{
|
||||
ImVec2 Min; // Upper-left
|
||||
ImVec2 Max; // Lower-right
|
||||
|
||||
ImRect() : Min(FLT_MAX,FLT_MAX), Max(-FLT_MAX,-FLT_MAX) {}
|
||||
ImRect(const ImVec2& min, const ImVec2& max) : Min(min), Max(max) {}
|
||||
ImRect(const ImVec4& v) : Min(v.x, v.y), Max(v.z, v.w) {}
|
||||
ImRect(float x1, float y1, float x2, float y2) : Min(x1, y1), Max(x2, y2) {}
|
||||
|
||||
ImVec2 GetCenter() const { return ImVec2((Min.x+Max.x)*0.5f, (Min.y+Max.y)*0.5f); }
|
||||
ImVec2 GetSize() const { return ImVec2(Max.x-Min.x, Max.y-Min.y); }
|
||||
float GetWidth() const { return Max.x-Min.x; }
|
||||
float GetHeight() const { return Max.y-Min.y; }
|
||||
ImVec2 GetTL() const { return Min; } // Top-left
|
||||
ImVec2 GetTR() const { return ImVec2(Max.x, Min.y); } // Top-right
|
||||
ImVec2 GetBL() const { return ImVec2(Min.x, Max.y); } // Bottom-left
|
||||
ImVec2 GetBR() const { return Max; } // Bottom-right
|
||||
bool Contains(const ImVec2& p) const { return p.x >= Min.x && p.y >= Min.y && p.x < Max.x && p.y < Max.y; }
|
||||
bool Contains(const ImRect& r) const { return r.Min.x >= Min.x && r.Min.y >= Min.y && r.Max.x < Max.x && r.Max.y < Max.y; }
|
||||
bool Overlaps(const ImRect& r) const { return r.Min.y < Max.y && r.Max.y > Min.y && r.Min.x < Max.x && r.Max.x > Min.x; }
|
||||
void Add(const ImVec2& rhs) { if (Min.x > rhs.x) Min.x = rhs.x; if (Min.y > rhs.y) Min.y = rhs.y; if (Max.x < rhs.x) Max.x = rhs.x; if (Max.y < rhs.y) Max.y = rhs.y; }
|
||||
void Add(const ImRect& rhs) { if (Min.x > rhs.Min.x) Min.x = rhs.Min.x; if (Min.y > rhs.Min.y) Min.y = rhs.Min.y; if (Max.x < rhs.Max.x) Max.x = rhs.Max.x; if (Max.y < rhs.Max.y) Max.y = rhs.Max.y; }
|
||||
void Expand(const float amount) { Min.x -= amount; Min.y -= amount; Max.x += amount; Max.y += amount; }
|
||||
void Expand(const ImVec2& amount) { Min.x -= amount.x; Min.y -= amount.y; Max.x += amount.x; Max.y += amount.y; }
|
||||
void Reduce(const ImVec2& amount) { Min.x += amount.x; Min.y += amount.y; Max.x -= amount.x; Max.y -= amount.y; }
|
||||
void Clip(const ImRect& clip) { if (Min.x < clip.Min.x) Min.x = clip.Min.x; if (Min.y < clip.Min.y) Min.y = clip.Min.y; if (Max.x > clip.Max.x) Max.x = clip.Max.x; if (Max.y > clip.Max.y) Max.y = clip.Max.y; }
|
||||
void Round() { Min.x = (float)(int)Min.x; Min.y = (float)(int)Min.y; Max.x = (float)(int)Max.x; Max.y = (float)(int)Max.y; }
|
||||
ImVec2 GetClosestPoint(ImVec2 p, bool on_edge) const
|
||||
{
|
||||
if (!on_edge && Contains(p))
|
||||
return p;
|
||||
if (p.x > Max.x) p.x = Max.x;
|
||||
else if (p.x < Min.x) p.x = Min.x;
|
||||
if (p.y > Max.y) p.y = Max.y;
|
||||
else if (p.y < Min.y) p.y = Min.y;
|
||||
return p;
|
||||
}
|
||||
};
|
||||
|
||||
// Stacked color modifier, backup of modified data so we can restore it
|
||||
struct ImGuiColMod
|
||||
{
|
||||
ImGuiCol Col;
|
||||
ImVec4 PreviousValue;
|
||||
};
|
||||
|
||||
// Stacked style modifier, backup of modified data so we can restore it
|
||||
struct ImGuiStyleMod
|
||||
{
|
||||
ImGuiStyleVar Var;
|
||||
ImVec2 PreviousValue;
|
||||
};
|
||||
|
||||
// Stacked data for BeginGroup()/EndGroup()
|
||||
struct ImGuiGroupData
|
||||
{
|
||||
ImVec2 BackupCursorPos;
|
||||
ImVec2 BackupCursorMaxPos;
|
||||
float BackupIndentX;
|
||||
float BackupCurrentLineHeight;
|
||||
float BackupCurrentLineTextBaseOffset;
|
||||
float BackupLogLinePosY;
|
||||
bool AdvanceCursor;
|
||||
};
|
||||
|
||||
// Per column data for Columns()
|
||||
struct ImGuiColumnData
|
||||
{
|
||||
float OffsetNorm; // Column start offset, normalized 0.0 (far left) -> 1.0 (far right)
|
||||
//float IndentX;
|
||||
};
|
||||
|
||||
// Simple column measurement currently used for MenuItem() only. This is very short-sighted for now and NOT a generic helper.
|
||||
struct IMGUI_API ImGuiSimpleColumns
|
||||
{
|
||||
int Count;
|
||||
float Spacing;
|
||||
float Width, NextWidth;
|
||||
float Pos[8], NextWidths[8];
|
||||
|
||||
ImGuiSimpleColumns();
|
||||
void Update(int count, float spacing, bool clear);
|
||||
float DeclColumns(float w0, float w1, float w2);
|
||||
float CalcExtraSpace(float avail_w);
|
||||
};
|
||||
|
||||
// Internal state of the currently focused/edited text input box
|
||||
struct IMGUI_API ImGuiTextEditState
|
||||
{
|
||||
ImGuiID Id; // widget id owning the text state
|
||||
ImVector<ImWchar> Text; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer.
|
||||
ImVector<char> InitialText; // backup of end-user buffer at the time of focus (in UTF-8, unaltered)
|
||||
ImVector<char> TempTextBuffer;
|
||||
int CurLenA, CurLenW; // we need to maintain our buffer length in both UTF-8 and wchar format.
|
||||
int BufSizeA; // end-user buffer size
|
||||
float ScrollX;
|
||||
ImGuiStb::STB_TexteditState StbState;
|
||||
float CursorAnim;
|
||||
bool CursorFollow;
|
||||
bool SelectedAllMouseLock;
|
||||
|
||||
ImGuiTextEditState() { memset(this, 0, sizeof(*this)); }
|
||||
void CursorAnimReset() { CursorAnim = -0.30f; } // After a user-input the cursor stays on for a while without blinking
|
||||
void CursorClamp() { StbState.cursor = ImMin(StbState.cursor, CurLenW); StbState.select_start = ImMin(StbState.select_start, CurLenW); StbState.select_end = ImMin(StbState.select_end, CurLenW); }
|
||||
bool HasSelection() const { return StbState.select_start != StbState.select_end; }
|
||||
void ClearSelection() { StbState.select_start = StbState.select_end = StbState.cursor; }
|
||||
void SelectAll() { StbState.select_start = 0; StbState.select_end = CurLenW; StbState.cursor = StbState.select_end; StbState.has_preferred_x = false; }
|
||||
void OnKeyPressed(int key);
|
||||
};
|
||||
|
||||
// Data saved in imgui.ini file
|
||||
struct ImGuiIniData
|
||||
{
|
||||
char* Name;
|
||||
ImGuiID ID;
|
||||
ImVec2 Pos;
|
||||
ImVec2 Size;
|
||||
bool Collapsed;
|
||||
};
|
||||
|
||||
// Mouse cursor data (used when io.MouseDrawCursor is set)
|
||||
struct ImGuiMouseCursorData
|
||||
{
|
||||
ImGuiMouseCursor Type;
|
||||
ImVec2 HotOffset;
|
||||
ImVec2 Size;
|
||||
ImVec2 TexUvMin[2];
|
||||
ImVec2 TexUvMax[2];
|
||||
};
|
||||
|
||||
// Storage for current popup stack
|
||||
struct ImGuiPopupRef
|
||||
{
|
||||
ImGuiID PopupID; // Set on OpenPopup()
|
||||
ImGuiWindow* Window; // Resolved on BeginPopup() - may stay unresolved if user never calls OpenPopup()
|
||||
ImGuiWindow* ParentWindow; // Set on OpenPopup()
|
||||
ImGuiID ParentMenuSet; // Set on OpenPopup()
|
||||
ImVec2 MousePosOnOpen; // Copy of mouse position at the time of opening popup
|
||||
|
||||
ImGuiPopupRef(ImGuiID id, ImGuiWindow* parent_window, ImGuiID parent_menu_set, const ImVec2& mouse_pos) { PopupID = id; Window = NULL; ParentWindow = parent_window; ParentMenuSet = parent_menu_set; MousePosOnOpen = mouse_pos; }
|
||||
};
|
||||
|
||||
// Main state for ImGui
|
||||
struct ImGuiState
|
||||
{
|
||||
bool Initialized;
|
||||
ImGuiIO IO;
|
||||
ImGuiStyle Style;
|
||||
ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back()
|
||||
float FontSize; // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize()
|
||||
float FontBaseSize; // (Shortcut) == IO.FontGlobalScale * Font->Scale * Font->FontSize. Size of characters.
|
||||
ImVec2 FontTexUvWhitePixel; // (Shortcut) == Font->TexUvWhitePixel
|
||||
|
||||
float Time;
|
||||
int FrameCount;
|
||||
int FrameCountEnded;
|
||||
int FrameCountRendered;
|
||||
ImVector<ImGuiWindow*> Windows;
|
||||
ImVector<ImGuiWindow*> WindowsSortBuffer;
|
||||
ImGuiWindow* CurrentWindow; // Being drawn into
|
||||
ImVector<ImGuiWindow*> CurrentWindowStack;
|
||||
ImGuiWindow* FocusedWindow; // Will catch keyboard inputs
|
||||
ImGuiWindow* HoveredWindow; // Will catch mouse inputs
|
||||
ImGuiWindow* HoveredRootWindow; // Will catch mouse inputs (for focus/move only)
|
||||
ImGuiID HoveredId; // Hovered widget
|
||||
bool HoveredIdAllowOverlap;
|
||||
ImGuiID HoveredIdPreviousFrame;
|
||||
ImGuiID ActiveId; // Active widget
|
||||
ImGuiID ActiveIdPreviousFrame;
|
||||
bool ActiveIdIsAlive;
|
||||
bool ActiveIdIsJustActivated; // Set at the time of activation for one frame
|
||||
bool ActiveIdAllowOverlap; // Set only by active widget
|
||||
ImGuiWindow* ActiveIdWindow;
|
||||
ImGuiWindow* MovedWindow; // Track the child window we clicked on to move a window. Pointer is only valid if ActiveID is the "#MOVE" identifier of a window.
|
||||
ImVector<ImGuiIniData> Settings; // .ini Settings
|
||||
float SettingsDirtyTimer; // Save .ini settinngs on disk when time reaches zero
|
||||
ImVector<ImGuiColMod> ColorModifiers; // Stack for PushStyleColor()/PopStyleColor()
|
||||
ImVector<ImGuiStyleMod> StyleModifiers; // Stack for PushStyleVar()/PopStyleVar()
|
||||
ImVector<ImFont*> FontStack; // Stack for PushFont()/PopFont()
|
||||
ImVector<ImGuiPopupRef> OpenedPopupStack; // Which popups are open (persistent)
|
||||
ImVector<ImGuiPopupRef> CurrentPopupStack; // Which level of BeginPopup() we are in (reset every frame)
|
||||
|
||||
// Storage for SetNexWindow** and SetNextTreeNode*** functions
|
||||
ImVec2 SetNextWindowPosVal;
|
||||
ImVec2 SetNextWindowSizeVal;
|
||||
ImVec2 SetNextWindowContentSizeVal;
|
||||
bool SetNextWindowCollapsedVal;
|
||||
ImGuiSetCond SetNextWindowPosCond;
|
||||
ImGuiSetCond SetNextWindowSizeCond;
|
||||
ImGuiSetCond SetNextWindowContentSizeCond;
|
||||
ImGuiSetCond SetNextWindowCollapsedCond;
|
||||
bool SetNextWindowFocus;
|
||||
bool SetNextTreeNodeOpenedVal;
|
||||
ImGuiSetCond SetNextTreeNodeOpenedCond;
|
||||
|
||||
// Render
|
||||
ImDrawData RenderDrawData; // Main ImDrawData instance to pass render information to the user
|
||||
ImVector<ImDrawList*> RenderDrawLists[3];
|
||||
float ModalWindowDarkeningRatio;
|
||||
ImDrawList OverlayDrawList; // Optional software render of mouse cursors, if io.MouseDrawCursor is set + a few debug overlays
|
||||
ImGuiMouseCursor MouseCursor;
|
||||
ImGuiMouseCursorData MouseCursorData[ImGuiMouseCursor_Count_];
|
||||
|
||||
// Widget state
|
||||
ImGuiTextEditState InputTextState;
|
||||
ImFont InputTextPasswordFont;
|
||||
ImGuiID ScalarAsInputTextId; // Temporary text input when CTRL+clicking on a slider, etc.
|
||||
ImGuiStorage ColorEditModeStorage; // Store user selection of color edit mode
|
||||
ImVec2 ActiveClickDeltaToCenter;
|
||||
float DragCurrentValue; // Currently dragged value, always float, not rounded by end-user precision settings
|
||||
ImVec2 DragLastMouseDelta;
|
||||
float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio
|
||||
float DragSpeedScaleSlow;
|
||||
float DragSpeedScaleFast;
|
||||
ImVec2 ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage?
|
||||
char Tooltip[1024];
|
||||
char* PrivateClipboard; // If no custom clipboard handler is defined
|
||||
ImVec2 OsImePosRequest, OsImePosSet; // Cursor position request & last passed to the OS Input Method Editor
|
||||
|
||||
// Logging
|
||||
bool LogEnabled;
|
||||
FILE* LogFile; // If != NULL log to stdout/ file
|
||||
ImGuiTextBuffer* LogClipboard; // Else log to clipboard. This is pointer so our GImGui static constructor doesn't call heap allocators.
|
||||
int LogStartDepth;
|
||||
int LogAutoExpandMaxDepth;
|
||||
|
||||
// Misc
|
||||
float FramerateSecPerFrame[120]; // calculate estimate of framerate for user
|
||||
int FramerateSecPerFrameIdx;
|
||||
float FramerateSecPerFrameAccum;
|
||||
int CaptureMouseNextFrame; // explicit capture via CaptureInputs() sets those flags
|
||||
int CaptureKeyboardNextFrame;
|
||||
char TempBuffer[1024*3+1]; // temporary text buffer
|
||||
|
||||
ImGuiState()
|
||||
{
|
||||
Initialized = false;
|
||||
Font = NULL;
|
||||
FontSize = FontBaseSize = 0.0f;
|
||||
FontTexUvWhitePixel = ImVec2(0.0f, 0.0f);
|
||||
|
||||
Time = 0.0f;
|
||||
FrameCount = 0;
|
||||
FrameCountEnded = FrameCountRendered = -1;
|
||||
CurrentWindow = NULL;
|
||||
FocusedWindow = NULL;
|
||||
HoveredWindow = NULL;
|
||||
HoveredRootWindow = NULL;
|
||||
HoveredId = 0;
|
||||
HoveredIdAllowOverlap = false;
|
||||
HoveredIdPreviousFrame = 0;
|
||||
ActiveId = 0;
|
||||
ActiveIdPreviousFrame = 0;
|
||||
ActiveIdIsAlive = false;
|
||||
ActiveIdIsJustActivated = false;
|
||||
ActiveIdAllowOverlap = false;
|
||||
ActiveIdWindow = NULL;
|
||||
MovedWindow = NULL;
|
||||
SettingsDirtyTimer = 0.0f;
|
||||
|
||||
SetNextWindowPosVal = ImVec2(0.0f, 0.0f);
|
||||
SetNextWindowSizeVal = ImVec2(0.0f, 0.0f);
|
||||
SetNextWindowCollapsedVal = false;
|
||||
SetNextWindowPosCond = 0;
|
||||
SetNextWindowSizeCond = 0;
|
||||
SetNextWindowContentSizeCond = 0;
|
||||
SetNextWindowCollapsedCond = 0;
|
||||
SetNextWindowFocus = false;
|
||||
SetNextTreeNodeOpenedVal = false;
|
||||
SetNextTreeNodeOpenedCond = 0;
|
||||
|
||||
ScalarAsInputTextId = 0;
|
||||
ActiveClickDeltaToCenter = ImVec2(0.0f, 0.0f);
|
||||
DragCurrentValue = 0.0f;
|
||||
DragLastMouseDelta = ImVec2(0.0f, 0.0f);
|
||||
DragSpeedDefaultRatio = 0.01f;
|
||||
DragSpeedScaleSlow = 0.01f;
|
||||
DragSpeedScaleFast = 10.0f;
|
||||
ScrollbarClickDeltaToGrabCenter = ImVec2(0.0f, 0.0f);
|
||||
memset(Tooltip, 0, sizeof(Tooltip));
|
||||
PrivateClipboard = NULL;
|
||||
OsImePosRequest = OsImePosSet = ImVec2(-1.0f, -1.0f);
|
||||
|
||||
ModalWindowDarkeningRatio = 0.0f;
|
||||
OverlayDrawList._OwnerName = "##Overlay"; // Give it a name for debugging
|
||||
MouseCursor = ImGuiMouseCursor_Arrow;
|
||||
memset(MouseCursorData, 0, sizeof(MouseCursorData));
|
||||
|
||||
LogEnabled = false;
|
||||
LogFile = NULL;
|
||||
LogClipboard = NULL;
|
||||
LogStartDepth = 0;
|
||||
LogAutoExpandMaxDepth = 2;
|
||||
|
||||
memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame));
|
||||
FramerateSecPerFrameIdx = 0;
|
||||
FramerateSecPerFrameAccum = 0.0f;
|
||||
CaptureMouseNextFrame = CaptureKeyboardNextFrame = -1;
|
||||
memset(TempBuffer, 0, sizeof(TempBuffer));
|
||||
}
|
||||
};
|
||||
|
||||
// Transient per-window data, reset at the beginning of the frame
|
||||
// FIXME: That's theory, in practice the delimitation between ImGuiWindow and ImGuiDrawContext is quite tenuous and could be reconsidered.
|
||||
struct IMGUI_API ImGuiDrawContext
|
||||
{
|
||||
ImVec2 CursorPos;
|
||||
ImVec2 CursorPosPrevLine;
|
||||
ImVec2 CursorStartPos;
|
||||
ImVec2 CursorMaxPos; // Implicitly calculate the size of our contents, always extending. Saved into window->SizeContents at the end of the frame
|
||||
float CurrentLineHeight;
|
||||
float CurrentLineTextBaseOffset;
|
||||
float PrevLineHeight;
|
||||
float PrevLineTextBaseOffset;
|
||||
float LogLinePosY;
|
||||
int TreeDepth;
|
||||
ImGuiID LastItemID;
|
||||
ImRect LastItemRect;
|
||||
bool LastItemHoveredAndUsable; // Item rectangle is hovered, and its window is currently interactable with (not blocked by a popup preventing access to the window)
|
||||
bool LastItemHoveredRect; // Item rectangle is hovered, but its window may or not be currently interactable with (might be blocked by a popup preventing access to the window)
|
||||
bool MenuBarAppending;
|
||||
float MenuBarOffsetX;
|
||||
ImVector<ImGuiWindow*> ChildWindows;
|
||||
ImGuiStorage* StateStorage;
|
||||
ImGuiLayoutType LayoutType;
|
||||
|
||||
// We store the current settings outside of the vectors to increase memory locality (reduce cache misses). The vectors are rarely modified. Also it allows us to not heap allocate for short-lived windows which are not using those settings.
|
||||
float ItemWidth; // == ItemWidthStack.back(). 0.0: default, >0.0: width in pixels, <0.0: align xx pixels to the right of window
|
||||
float TextWrapPos; // == TextWrapPosStack.back() [empty == -1.0f]
|
||||
bool AllowKeyboardFocus; // == AllowKeyboardFocusStack.back() [empty == true]
|
||||
bool ButtonRepeat; // == ButtonRepeatStack.back() [empty == false]
|
||||
ImVector<float> ItemWidthStack;
|
||||
ImVector<float> TextWrapPosStack;
|
||||
ImVector<bool> AllowKeyboardFocusStack;
|
||||
ImVector<bool> ButtonRepeatStack;
|
||||
ImVector<ImGuiGroupData>GroupStack;
|
||||
ImGuiColorEditMode ColorEditMode;
|
||||
int StackSizesBackup[6]; // Store size of various stacks for asserting
|
||||
|
||||
float IndentX; // Indentation / start position from left of window (increased by TreePush/TreePop, etc.)
|
||||
float ColumnsOffsetX; // Offset to the current column (if ColumnsCurrent > 0). FIXME: This and the above should be a stack to allow use cases like Tree->Column->Tree. Need revamp columns API.
|
||||
int ColumnsCurrent;
|
||||
int ColumnsCount;
|
||||
float ColumnsMinX;
|
||||
float ColumnsMaxX;
|
||||
float ColumnsStartPosY;
|
||||
float ColumnsCellMinY;
|
||||
float ColumnsCellMaxY;
|
||||
bool ColumnsShowBorders;
|
||||
ImGuiID ColumnsSetID;
|
||||
ImVector<ImGuiColumnData> ColumnsData;
|
||||
|
||||
ImGuiDrawContext()
|
||||
{
|
||||
CursorPos = CursorPosPrevLine = CursorStartPos = CursorMaxPos = ImVec2(0.0f, 0.0f);
|
||||
CurrentLineHeight = PrevLineHeight = 0.0f;
|
||||
CurrentLineTextBaseOffset = PrevLineTextBaseOffset = 0.0f;
|
||||
LogLinePosY = -1.0f;
|
||||
TreeDepth = 0;
|
||||
LastItemID = 0;
|
||||
LastItemRect = ImRect(0.0f,0.0f,0.0f,0.0f);
|
||||
LastItemHoveredAndUsable = LastItemHoveredRect = false;
|
||||
MenuBarAppending = false;
|
||||
MenuBarOffsetX = 0.0f;
|
||||
StateStorage = NULL;
|
||||
LayoutType = ImGuiLayoutType_Vertical;
|
||||
ItemWidth = 0.0f;
|
||||
ButtonRepeat = false;
|
||||
AllowKeyboardFocus = true;
|
||||
TextWrapPos = -1.0f;
|
||||
ColorEditMode = ImGuiColorEditMode_RGB;
|
||||
memset(StackSizesBackup, 0, sizeof(StackSizesBackup));
|
||||
|
||||
IndentX = 0.0f;
|
||||
ColumnsOffsetX = 0.0f;
|
||||
ColumnsCurrent = 0;
|
||||
ColumnsCount = 1;
|
||||
ColumnsMinX = ColumnsMaxX = 0.0f;
|
||||
ColumnsStartPosY = 0.0f;
|
||||
ColumnsCellMinY = ColumnsCellMaxY = 0.0f;
|
||||
ColumnsShowBorders = true;
|
||||
ColumnsSetID = 0;
|
||||
}
|
||||
};
|
||||
|
||||
// Windows data
|
||||
struct IMGUI_API ImGuiWindow
|
||||
{
|
||||
char* Name;
|
||||
ImGuiID ID;
|
||||
ImGuiWindowFlags Flags;
|
||||
ImVec2 PosFloat;
|
||||
ImVec2 Pos; // Position rounded-up to nearest pixel
|
||||
ImVec2 Size; // Current size (==SizeFull or collapsed title bar size)
|
||||
ImVec2 SizeFull; // Size when non collapsed
|
||||
ImVec2 SizeContents; // Size of contents (== extents reach of the drawing cursor) from previous frame
|
||||
ImVec2 SizeContentsExplicit; // Size of contents explicitly set by the user via SetNextWindowContentSize()
|
||||
ImVec2 WindowPadding; // Window padding at the time of begin. We need to lock it, in particular manipulation of the ShowBorder would have an effect
|
||||
ImGuiID MoveID; // == window->GetID("#MOVE")
|
||||
ImVec2 Scroll;
|
||||
ImVec2 ScrollTarget; // target scroll position. stored as cursor position with scrolling canceled out, so the highest point is always 0.0f. (FLT_MAX for no change)
|
||||
ImVec2 ScrollTargetCenterRatio; // 0.0f = scroll so that target position is at top, 0.5f = scroll so that target position is centered
|
||||
bool ScrollbarX, ScrollbarY;
|
||||
ImVec2 ScrollbarSizes;
|
||||
float BorderSize;
|
||||
bool Active; // Set to true on Begin()
|
||||
bool WasActive;
|
||||
bool Accessed; // Set to true when any widget access the current window
|
||||
bool Collapsed; // Set when collapsing window to become only title-bar
|
||||
bool SkipItems; // == Visible && !Collapsed
|
||||
int BeginCount; // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs)
|
||||
ImGuiID PopupID; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling)
|
||||
int AutoFitFramesX, AutoFitFramesY;
|
||||
bool AutoFitOnlyGrows;
|
||||
int AutoPosLastDirection;
|
||||
int HiddenFrames;
|
||||
int SetWindowPosAllowFlags; // bit ImGuiSetCond_*** specify if SetWindowPos() call will succeed with this particular flag.
|
||||
int SetWindowSizeAllowFlags; // bit ImGuiSetCond_*** specify if SetWindowSize() call will succeed with this particular flag.
|
||||
int SetWindowCollapsedAllowFlags; // bit ImGuiSetCond_*** specify if SetWindowCollapsed() call will succeed with this particular flag.
|
||||
bool SetWindowPosCenterWanted;
|
||||
|
||||
ImGuiDrawContext DC; // Temporary per-window data, reset at the beginning of the frame
|
||||
ImVector<ImGuiID> IDStack; // ID stack. ID are hashes seeded with the value at the top of the stack
|
||||
ImRect ClipRect; // = DrawList->clip_rect_stack.back(). Scissoring / clipping rectangle. x1, y1, x2, y2.
|
||||
ImRect ClippedWindowRect; // = ClipRect just after setup in Begin()
|
||||
int LastFrameActive;
|
||||
float ItemWidthDefault;
|
||||
ImGuiSimpleColumns MenuColumns; // Simplified columns storage for menu items
|
||||
ImGuiStorage StateStorage;
|
||||
float FontWindowScale; // Scale multiplier per-window
|
||||
ImDrawList* DrawList;
|
||||
ImGuiWindow* RootWindow;
|
||||
ImGuiWindow* RootNonPopupWindow;
|
||||
|
||||
// Focus
|
||||
int FocusIdxAllCounter; // Start at -1 and increase as assigned via FocusItemRegister()
|
||||
int FocusIdxTabCounter; // (same, but only count widgets which you can Tab through)
|
||||
int FocusIdxAllRequestCurrent; // Item being requested for focus
|
||||
int FocusIdxTabRequestCurrent; // Tab-able item being requested for focus
|
||||
int FocusIdxAllRequestNext; // Item being requested for focus, for next update (relies on layout to be stable between the frame pressing TAB and the next frame)
|
||||
int FocusIdxTabRequestNext; // "
|
||||
|
||||
public:
|
||||
ImGuiWindow(const char* name);
|
||||
~ImGuiWindow();
|
||||
|
||||
ImGuiID GetID(const char* str, const char* str_end = NULL);
|
||||
ImGuiID GetID(const void* ptr);
|
||||
|
||||
ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x+Size.x, Pos.y+Size.y); }
|
||||
float CalcFontSize() const { return GImGui->FontBaseSize * FontWindowScale; }
|
||||
float TitleBarHeight() const { return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + GImGui->Style.FramePadding.y * 2.0f; }
|
||||
ImRect TitleBarRect() const { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight())); }
|
||||
float MenuBarHeight() const { return (Flags & ImGuiWindowFlags_MenuBar) ? CalcFontSize() + GImGui->Style.FramePadding.y * 2.0f : 0.0f; }
|
||||
ImRect MenuBarRect() const { float y1 = Pos.y + TitleBarHeight(); return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight()); }
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Internal API
|
||||
// No guarantee of forward compatibility here.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
namespace ImGui
|
||||
{
|
||||
// We should always have a CurrentWindow in the stack (there is an implicit "Debug" window)
|
||||
// If this ever crash because g.CurrentWindow is NULL it means that either
|
||||
// - ImGui::NewFrame() has never been called, which is illegal.
|
||||
// - You are calling ImGui functions after ImGui::Render() and before the next ImGui::NewFrame(), which is also illegal.
|
||||
inline ImGuiWindow* GetCurrentWindowRead() { ImGuiState& g = *GImGui; return g.CurrentWindow; }
|
||||
inline ImGuiWindow* GetCurrentWindow() { ImGuiState& g = *GImGui; g.CurrentWindow->Accessed = true; return g.CurrentWindow; }
|
||||
IMGUI_API ImGuiWindow* GetParentWindow();
|
||||
IMGUI_API ImGuiWindow* FindWindowByName(const char* name);
|
||||
IMGUI_API void FocusWindow(ImGuiWindow* window);
|
||||
|
||||
IMGUI_API void EndFrame(); // Ends the ImGui frame. Automatically called by Render()! you most likely don't need to ever call that yourself directly. If you don't need to render you can call EndFrame() but you'll have wasted CPU already. If you don't need to render, don't create any windows instead!
|
||||
|
||||
IMGUI_API void SetActiveID(ImGuiID id, ImGuiWindow* window);
|
||||
IMGUI_API void SetHoveredID(ImGuiID id);
|
||||
IMGUI_API void KeepAliveID(ImGuiID id);
|
||||
|
||||
IMGUI_API void ItemSize(const ImVec2& size, float text_offset_y = 0.0f);
|
||||
IMGUI_API void ItemSize(const ImRect& bb, float text_offset_y = 0.0f);
|
||||
IMGUI_API bool ItemAdd(const ImRect& bb, const ImGuiID* id);
|
||||
IMGUI_API bool IsClippedEx(const ImRect& bb, const ImGuiID* id, bool clip_even_when_logged);
|
||||
IMGUI_API bool IsHovered(const ImRect& bb, ImGuiID id, bool flatten_childs = false);
|
||||
IMGUI_API bool FocusableItemRegister(ImGuiWindow* window, bool is_active, bool tab_stop = true); // Return true if focus is requested
|
||||
IMGUI_API void FocusableItemUnregister(ImGuiWindow* window);
|
||||
IMGUI_API ImVec2 CalcItemSize(ImVec2 size, float default_x, float default_y);
|
||||
IMGUI_API float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x);
|
||||
|
||||
IMGUI_API void OpenPopupEx(const char* str_id, bool reopen_existing);
|
||||
|
||||
inline IMGUI_API ImU32 GetColorU32(ImGuiCol idx, float alpha_mul) { ImVec4 c = GImGui->Style.Colors[idx]; c.w *= GImGui->Style.Alpha * alpha_mul; return ImGui::ColorConvertFloat4ToU32(c); }
|
||||
inline IMGUI_API ImU32 GetColorU32(const ImVec4& col) { ImVec4 c = col; c.w *= GImGui->Style.Alpha; return ImGui::ColorConvertFloat4ToU32(c); }
|
||||
|
||||
// NB: All position are in absolute pixels coordinates (not window coordinates)
|
||||
// FIXME: Refactor all RenderText* functions into one.
|
||||
IMGUI_API void RenderText(ImVec2 pos, const char* text, const char* text_end = NULL, bool hide_text_after_hash = true);
|
||||
IMGUI_API void RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width);
|
||||
IMGUI_API void RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, ImGuiAlign align = ImGuiAlign_Default, const ImVec2* clip_min = NULL, const ImVec2* clip_max = NULL);
|
||||
IMGUI_API void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f);
|
||||
IMGUI_API void RenderCollapseTriangle(ImVec2 p_min, bool opened, float scale = 1.0f, bool shadow = false);
|
||||
IMGUI_API void RenderCheckMark(ImVec2 pos, ImU32 col);
|
||||
IMGUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text.
|
||||
|
||||
IMGUI_API void PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_existing_clip_rect = true);
|
||||
IMGUI_API void PopClipRect();
|
||||
|
||||
IMGUI_API bool ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags = 0);
|
||||
IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0,0), ImGuiButtonFlags flags = 0);
|
||||
|
||||
IMGUI_API bool SliderBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_min, float v_max, float power, int decimal_precision, ImGuiSliderFlags flags = 0);
|
||||
IMGUI_API bool SliderFloatN(const char* label, float* v, int components, float v_min, float v_max, const char* display_format, float power);
|
||||
IMGUI_API bool SliderIntN(const char* label, int* v, int components, int v_min, int v_max, const char* display_format);
|
||||
|
||||
IMGUI_API bool DragBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_speed, float v_min, float v_max, int decimal_precision, float power);
|
||||
IMGUI_API bool DragFloatN(const char* label, float* v, int components, float v_speed, float v_min, float v_max, const char* display_format, float power);
|
||||
IMGUI_API bool DragIntN(const char* label, int* v, int components, float v_speed, int v_min, int v_max, const char* display_format);
|
||||
|
||||
IMGUI_API bool InputTextEx(const char* label, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback = NULL, void* user_data = NULL);
|
||||
IMGUI_API bool InputFloatN(const char* label, float* v, int components, int decimal_precision, ImGuiInputTextFlags extra_flags);
|
||||
IMGUI_API bool InputIntN(const char* label, int* v, int components, ImGuiInputTextFlags extra_flags);
|
||||
IMGUI_API bool InputScalarEx(const char* label, ImGuiDataType data_type, void* data_ptr, void* step_ptr, void* step_fast_ptr, const char* scalar_format, ImGuiInputTextFlags extra_flags);
|
||||
IMGUI_API bool InputScalarAsWidgetReplacement(const ImRect& aabb, const char* label, ImGuiDataType data_type, void* data_ptr, ImGuiID id, int decimal_precision);
|
||||
|
||||
IMGUI_API bool TreeNodeBehaviorIsOpened(ImGuiID id, ImGuiTreeNodeFlags flags = 0); // Consume previous SetNextTreeNodeOpened() data, if any. May return true when logging
|
||||
|
||||
IMGUI_API void PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size);
|
||||
|
||||
IMGUI_API int ParseFormatPrecision(const char* fmt, int default_value);
|
||||
IMGUI_API float RoundScalar(float value, int decimal_precision);
|
||||
|
||||
} // namespace ImGuiP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning (pop)
|
||||
#endif
|
||||
573
inc/ImGui/stb_rect_pack.h
Normal file
573
inc/ImGui/stb_rect_pack.h
Normal file
@@ -0,0 +1,573 @@
|
||||
// stb_rect_pack.h - v0.08 - public domain - rectangle packing
|
||||
// Sean Barrett 2014
|
||||
//
|
||||
// Useful for e.g. packing rectangular textures into an atlas.
|
||||
// Does not do rotation.
|
||||
//
|
||||
// Not necessarily the awesomest packing method, but better than
|
||||
// the totally naive one in stb_truetype (which is primarily what
|
||||
// this is meant to replace).
|
||||
//
|
||||
// Has only had a few tests run, may have issues.
|
||||
//
|
||||
// More docs to come.
|
||||
//
|
||||
// No memory allocations; uses qsort() and assert() from stdlib.
|
||||
// Can override those by defining STBRP_SORT and STBRP_ASSERT.
|
||||
//
|
||||
// This library currently uses the Skyline Bottom-Left algorithm.
|
||||
//
|
||||
// Please note: better rectangle packers are welcome! Please
|
||||
// implement them to the same API, but with a different init
|
||||
// function.
|
||||
//
|
||||
// Credits
|
||||
//
|
||||
// Library
|
||||
// Sean Barrett
|
||||
// Minor features
|
||||
// Martins Mozeiko
|
||||
// Bugfixes / warning fixes
|
||||
// Jeremy Jaussaud
|
||||
//
|
||||
// Version history:
|
||||
//
|
||||
// 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0)
|
||||
// 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0)
|
||||
// 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort
|
||||
// 0.05: added STBRP_ASSERT to allow replacing assert
|
||||
// 0.04: fixed minor bug in STBRP_LARGE_RECTS support
|
||||
// 0.01: initial release
|
||||
//
|
||||
// LICENSE
|
||||
//
|
||||
// This software is in the public domain. Where that dedication is not
|
||||
// recognized, you are granted a perpetual, irrevocable license to copy,
|
||||
// distribute, and modify this file as you see fit.
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// INCLUDE SECTION
|
||||
//
|
||||
|
||||
#ifndef STB_INCLUDE_STB_RECT_PACK_H
|
||||
#define STB_INCLUDE_STB_RECT_PACK_H
|
||||
|
||||
#define STB_RECT_PACK_VERSION 1
|
||||
|
||||
#ifdef STBRP_STATIC
|
||||
#define STBRP_DEF static
|
||||
#else
|
||||
#define STBRP_DEF extern
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct stbrp_context stbrp_context;
|
||||
typedef struct stbrp_node stbrp_node;
|
||||
typedef struct stbrp_rect stbrp_rect;
|
||||
|
||||
#ifdef STBRP_LARGE_RECTS
|
||||
typedef int stbrp_coord;
|
||||
#else
|
||||
typedef unsigned short stbrp_coord;
|
||||
#endif
|
||||
|
||||
STBRP_DEF void stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects);
|
||||
// Assign packed locations to rectangles. The rectangles are of type
|
||||
// 'stbrp_rect' defined below, stored in the array 'rects', and there
|
||||
// are 'num_rects' many of them.
|
||||
//
|
||||
// Rectangles which are successfully packed have the 'was_packed' flag
|
||||
// set to a non-zero value and 'x' and 'y' store the minimum location
|
||||
// on each axis (i.e. bottom-left in cartesian coordinates, top-left
|
||||
// if you imagine y increasing downwards). Rectangles which do not fit
|
||||
// have the 'was_packed' flag set to 0.
|
||||
//
|
||||
// You should not try to access the 'rects' array from another thread
|
||||
// while this function is running, as the function temporarily reorders
|
||||
// the array while it executes.
|
||||
//
|
||||
// To pack into another rectangle, you need to call stbrp_init_target
|
||||
// again. To continue packing into the same rectangle, you can call
|
||||
// this function again. Calling this multiple times with multiple rect
|
||||
// arrays will probably produce worse packing results than calling it
|
||||
// a single time with the full rectangle array, but the option is
|
||||
// available.
|
||||
|
||||
struct stbrp_rect
|
||||
{
|
||||
// reserved for your use:
|
||||
int id;
|
||||
|
||||
// input:
|
||||
stbrp_coord w, h;
|
||||
|
||||
// output:
|
||||
stbrp_coord x, y;
|
||||
int was_packed; // non-zero if valid packing
|
||||
|
||||
}; // 16 bytes, nominally
|
||||
|
||||
|
||||
STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes);
|
||||
// Initialize a rectangle packer to:
|
||||
// pack a rectangle that is 'width' by 'height' in dimensions
|
||||
// using temporary storage provided by the array 'nodes', which is 'num_nodes' long
|
||||
//
|
||||
// You must call this function every time you start packing into a new target.
|
||||
//
|
||||
// There is no "shutdown" function. The 'nodes' memory must stay valid for
|
||||
// the following stbrp_pack_rects() call (or calls), but can be freed after
|
||||
// the call (or calls) finish.
|
||||
//
|
||||
// Note: to guarantee best results, either:
|
||||
// 1. make sure 'num_nodes' >= 'width'
|
||||
// or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1'
|
||||
//
|
||||
// If you don't do either of the above things, widths will be quantized to multiples
|
||||
// of small integers to guarantee the algorithm doesn't run out of temporary storage.
|
||||
//
|
||||
// If you do #2, then the non-quantized algorithm will be used, but the algorithm
|
||||
// may run out of temporary storage and be unable to pack some rectangles.
|
||||
|
||||
STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem);
|
||||
// Optionally call this function after init but before doing any packing to
|
||||
// change the handling of the out-of-temp-memory scenario, described above.
|
||||
// If you call init again, this will be reset to the default (false).
|
||||
|
||||
|
||||
STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic);
|
||||
// Optionally select which packing heuristic the library should use. Different
|
||||
// heuristics will produce better/worse results for different data sets.
|
||||
// If you call init again, this will be reset to the default.
|
||||
|
||||
enum
|
||||
{
|
||||
STBRP_HEURISTIC_Skyline_default=0,
|
||||
STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
|
||||
STBRP_HEURISTIC_Skyline_BF_sortHeight
|
||||
};
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// the details of the following structures don't matter to you, but they must
|
||||
// be visible so you can handle the memory allocations for them
|
||||
|
||||
struct stbrp_node
|
||||
{
|
||||
stbrp_coord x,y;
|
||||
stbrp_node *next;
|
||||
};
|
||||
|
||||
struct stbrp_context
|
||||
{
|
||||
int width;
|
||||
int height;
|
||||
int align;
|
||||
int init_mode;
|
||||
int heuristic;
|
||||
int num_nodes;
|
||||
stbrp_node *active_head;
|
||||
stbrp_node *free_head;
|
||||
stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2'
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPLEMENTATION SECTION
|
||||
//
|
||||
|
||||
#ifdef STB_RECT_PACK_IMPLEMENTATION
|
||||
#ifndef STBRP_SORT
|
||||
#include <stdlib.h>
|
||||
#define STBRP_SORT qsort
|
||||
#endif
|
||||
|
||||
#ifndef STBRP_ASSERT
|
||||
#include <assert.h>
|
||||
#define STBRP_ASSERT assert
|
||||
#endif
|
||||
|
||||
enum
|
||||
{
|
||||
STBRP__INIT_skyline = 1
|
||||
};
|
||||
|
||||
STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic)
|
||||
{
|
||||
switch (context->init_mode) {
|
||||
case STBRP__INIT_skyline:
|
||||
STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
|
||||
context->heuristic = heuristic;
|
||||
break;
|
||||
default:
|
||||
STBRP_ASSERT(0);
|
||||
}
|
||||
}
|
||||
|
||||
STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem)
|
||||
{
|
||||
if (allow_out_of_mem)
|
||||
// if it's ok to run out of memory, then don't bother aligning them;
|
||||
// this gives better packing, but may fail due to OOM (even though
|
||||
// the rectangles easily fit). @TODO a smarter approach would be to only
|
||||
// quantize once we've hit OOM, then we could get rid of this parameter.
|
||||
context->align = 1;
|
||||
else {
|
||||
// if it's not ok to run out of memory, then quantize the widths
|
||||
// so that num_nodes is always enough nodes.
|
||||
//
|
||||
// I.e. num_nodes * align >= width
|
||||
// align >= width / num_nodes
|
||||
// align = ceil(width/num_nodes)
|
||||
|
||||
context->align = (context->width + context->num_nodes-1) / context->num_nodes;
|
||||
}
|
||||
}
|
||||
|
||||
STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
|
||||
{
|
||||
int i;
|
||||
#ifndef STBRP_LARGE_RECTS
|
||||
STBRP_ASSERT(width <= 0xffff && height <= 0xffff);
|
||||
#endif
|
||||
|
||||
for (i=0; i < num_nodes-1; ++i)
|
||||
nodes[i].next = &nodes[i+1];
|
||||
nodes[i].next = NULL;
|
||||
context->init_mode = STBRP__INIT_skyline;
|
||||
context->heuristic = STBRP_HEURISTIC_Skyline_default;
|
||||
context->free_head = &nodes[0];
|
||||
context->active_head = &context->extra[0];
|
||||
context->width = width;
|
||||
context->height = height;
|
||||
context->num_nodes = num_nodes;
|
||||
stbrp_setup_allow_out_of_mem(context, 0);
|
||||
|
||||
// node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly)
|
||||
context->extra[0].x = 0;
|
||||
context->extra[0].y = 0;
|
||||
context->extra[0].next = &context->extra[1];
|
||||
context->extra[1].x = (stbrp_coord) width;
|
||||
#ifdef STBRP_LARGE_RECTS
|
||||
context->extra[1].y = (1<<30);
|
||||
#else
|
||||
context->extra[1].y = 65535;
|
||||
#endif
|
||||
context->extra[1].next = NULL;
|
||||
}
|
||||
|
||||
// find minimum y position if it starts at x1
|
||||
static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste)
|
||||
{
|
||||
(void)c;
|
||||
stbrp_node *node = first;
|
||||
int x1 = x0 + width;
|
||||
int min_y, visited_width, waste_area;
|
||||
STBRP_ASSERT(first->x <= x0);
|
||||
|
||||
#if 0
|
||||
// skip in case we're past the node
|
||||
while (node->next->x <= x0)
|
||||
++node;
|
||||
#else
|
||||
STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency
|
||||
#endif
|
||||
|
||||
STBRP_ASSERT(node->x <= x0);
|
||||
|
||||
min_y = 0;
|
||||
waste_area = 0;
|
||||
visited_width = 0;
|
||||
while (node->x < x1) {
|
||||
if (node->y > min_y) {
|
||||
// raise min_y higher.
|
||||
// we've accounted for all waste up to min_y,
|
||||
// but we'll now add more waste for everything we've visted
|
||||
waste_area += visited_width * (node->y - min_y);
|
||||
min_y = node->y;
|
||||
// the first time through, visited_width might be reduced
|
||||
if (node->x < x0)
|
||||
visited_width += node->next->x - x0;
|
||||
else
|
||||
visited_width += node->next->x - node->x;
|
||||
} else {
|
||||
// add waste area
|
||||
int under_width = node->next->x - node->x;
|
||||
if (under_width + visited_width > width)
|
||||
under_width = width - visited_width;
|
||||
waste_area += under_width * (min_y - node->y);
|
||||
visited_width += under_width;
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
|
||||
*pwaste = waste_area;
|
||||
return min_y;
|
||||
}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int x,y;
|
||||
stbrp_node **prev_link;
|
||||
} stbrp__findresult;
|
||||
|
||||
static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)
|
||||
{
|
||||
int best_waste = (1<<30), best_x, best_y = (1 << 30);
|
||||
stbrp__findresult fr;
|
||||
stbrp_node **prev, *node, *tail, **best = NULL;
|
||||
|
||||
// align to multiple of c->align
|
||||
width = (width + c->align - 1);
|
||||
width -= width % c->align;
|
||||
STBRP_ASSERT(width % c->align == 0);
|
||||
|
||||
node = c->active_head;
|
||||
prev = &c->active_head;
|
||||
while (node->x + width <= c->width) {
|
||||
int y,waste;
|
||||
y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
|
||||
if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL
|
||||
// bottom left
|
||||
if (y < best_y) {
|
||||
best_y = y;
|
||||
best = prev;
|
||||
}
|
||||
} else {
|
||||
// best-fit
|
||||
if (y + height <= c->height) {
|
||||
// can only use it if it first vertically
|
||||
if (y < best_y || (y == best_y && waste < best_waste)) {
|
||||
best_y = y;
|
||||
best_waste = waste;
|
||||
best = prev;
|
||||
}
|
||||
}
|
||||
}
|
||||
prev = &node->next;
|
||||
node = node->next;
|
||||
}
|
||||
|
||||
best_x = (best == NULL) ? 0 : (*best)->x;
|
||||
|
||||
// if doing best-fit (BF), we also have to try aligning right edge to each node position
|
||||
//
|
||||
// e.g, if fitting
|
||||
//
|
||||
// ____________________
|
||||
// |____________________|
|
||||
//
|
||||
// into
|
||||
//
|
||||
// | |
|
||||
// | ____________|
|
||||
// |____________|
|
||||
//
|
||||
// then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned
|
||||
//
|
||||
// This makes BF take about 2x the time
|
||||
|
||||
if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
|
||||
tail = c->active_head;
|
||||
node = c->active_head;
|
||||
prev = &c->active_head;
|
||||
// find first node that's admissible
|
||||
while (tail->x < width)
|
||||
tail = tail->next;
|
||||
while (tail) {
|
||||
int xpos = tail->x - width;
|
||||
int y,waste;
|
||||
STBRP_ASSERT(xpos >= 0);
|
||||
// find the left position that matches this
|
||||
while (node->next->x <= xpos) {
|
||||
prev = &node->next;
|
||||
node = node->next;
|
||||
}
|
||||
STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
|
||||
y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
|
||||
if (y + height < c->height) {
|
||||
if (y <= best_y) {
|
||||
if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
|
||||
best_x = xpos;
|
||||
STBRP_ASSERT(y <= best_y);
|
||||
best_y = y;
|
||||
best_waste = waste;
|
||||
best = prev;
|
||||
}
|
||||
}
|
||||
}
|
||||
tail = tail->next;
|
||||
}
|
||||
}
|
||||
|
||||
fr.prev_link = best;
|
||||
fr.x = best_x;
|
||||
fr.y = best_y;
|
||||
return fr;
|
||||
}
|
||||
|
||||
static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)
|
||||
{
|
||||
// find best position according to heuristic
|
||||
stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
|
||||
stbrp_node *node, *cur;
|
||||
|
||||
// bail if:
|
||||
// 1. it failed
|
||||
// 2. the best node doesn't fit (we don't always check this)
|
||||
// 3. we're out of memory
|
||||
if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
|
||||
res.prev_link = NULL;
|
||||
return res;
|
||||
}
|
||||
|
||||
// on success, create new node
|
||||
node = context->free_head;
|
||||
node->x = (stbrp_coord) res.x;
|
||||
node->y = (stbrp_coord) (res.y + height);
|
||||
|
||||
context->free_head = node->next;
|
||||
|
||||
// insert the new node into the right starting point, and
|
||||
// let 'cur' point to the remaining nodes needing to be
|
||||
// stiched back in
|
||||
|
||||
cur = *res.prev_link;
|
||||
if (cur->x < res.x) {
|
||||
// preserve the existing one, so start testing with the next one
|
||||
stbrp_node *next = cur->next;
|
||||
cur->next = node;
|
||||
cur = next;
|
||||
} else {
|
||||
*res.prev_link = node;
|
||||
}
|
||||
|
||||
// from here, traverse cur and free the nodes, until we get to one
|
||||
// that shouldn't be freed
|
||||
while (cur->next && cur->next->x <= res.x + width) {
|
||||
stbrp_node *next = cur->next;
|
||||
// move the current node to the free list
|
||||
cur->next = context->free_head;
|
||||
context->free_head = cur;
|
||||
cur = next;
|
||||
}
|
||||
|
||||
// stitch the list back in
|
||||
node->next = cur;
|
||||
|
||||
if (cur->x < res.x + width)
|
||||
cur->x = (stbrp_coord) (res.x + width);
|
||||
|
||||
#ifdef _DEBUG
|
||||
cur = context->active_head;
|
||||
while (cur->x < context->width) {
|
||||
STBRP_ASSERT(cur->x < cur->next->x);
|
||||
cur = cur->next;
|
||||
}
|
||||
STBRP_ASSERT(cur->next == NULL);
|
||||
|
||||
{
|
||||
stbrp_node *L1 = NULL, *L2 = NULL;
|
||||
int count=0;
|
||||
cur = context->active_head;
|
||||
while (cur) {
|
||||
L1 = cur;
|
||||
cur = cur->next;
|
||||
++count;
|
||||
}
|
||||
cur = context->free_head;
|
||||
while (cur) {
|
||||
L2 = cur;
|
||||
cur = cur->next;
|
||||
++count;
|
||||
}
|
||||
STBRP_ASSERT(count == context->num_nodes+2);
|
||||
}
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int rect_height_compare(const void *a, const void *b)
|
||||
{
|
||||
stbrp_rect *p = (stbrp_rect *) a;
|
||||
stbrp_rect *q = (stbrp_rect *) b;
|
||||
if (p->h > q->h)
|
||||
return -1;
|
||||
if (p->h < q->h)
|
||||
return 1;
|
||||
return (p->w > q->w) ? -1 : (p->w < q->w);
|
||||
}
|
||||
|
||||
static int rect_width_compare(const void *a, const void *b)
|
||||
{
|
||||
stbrp_rect *p = (stbrp_rect *) a;
|
||||
stbrp_rect *q = (stbrp_rect *) b;
|
||||
if (p->w > q->w)
|
||||
return -1;
|
||||
if (p->w < q->w)
|
||||
return 1;
|
||||
return (p->h > q->h) ? -1 : (p->h < q->h);
|
||||
}
|
||||
|
||||
static int rect_original_order(const void *a, const void *b)
|
||||
{
|
||||
stbrp_rect *p = (stbrp_rect *) a;
|
||||
stbrp_rect *q = (stbrp_rect *) b;
|
||||
return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
|
||||
}
|
||||
|
||||
#ifdef STBRP_LARGE_RECTS
|
||||
#define STBRP__MAXVAL 0xffffffff
|
||||
#else
|
||||
#define STBRP__MAXVAL 0xffff
|
||||
#endif
|
||||
|
||||
STBRP_DEF void stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
|
||||
{
|
||||
int i;
|
||||
|
||||
// we use the 'was_packed' field internally to allow sorting/unsorting
|
||||
for (i=0; i < num_rects; ++i) {
|
||||
rects[i].was_packed = i;
|
||||
#ifndef STBRP_LARGE_RECTS
|
||||
STBRP_ASSERT(rects[i].w <= 0xffff && rects[i].h <= 0xffff);
|
||||
#endif
|
||||
}
|
||||
|
||||
// sort according to heuristic
|
||||
STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);
|
||||
|
||||
for (i=0; i < num_rects; ++i) {
|
||||
if (rects[i].w == 0 || rects[i].h == 0) {
|
||||
rects[i].x = rects[i].y = 0; // empty rect needs no space
|
||||
} else {
|
||||
stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
|
||||
if (fr.prev_link) {
|
||||
rects[i].x = (stbrp_coord) fr.x;
|
||||
rects[i].y = (stbrp_coord) fr.y;
|
||||
} else {
|
||||
rects[i].x = rects[i].y = STBRP__MAXVAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// unsort
|
||||
STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);
|
||||
|
||||
// set was_packed flags
|
||||
for (i=0; i < num_rects; ++i)
|
||||
rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
|
||||
}
|
||||
#endif
|
||||
1317
inc/ImGui/stb_textedit.h
Normal file
1317
inc/ImGui/stb_textedit.h
Normal file
File diff suppressed because it is too large
Load Diff
3263
inc/ImGui/stb_truetype.h
Normal file
3263
inc/ImGui/stb_truetype.h
Normal file
File diff suppressed because it is too large
Load Diff
133
inc/SDL2/SDL.h
Normal file
133
inc/SDL2/SDL.h
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file SDL.h
|
||||
*
|
||||
* Main include header for the SDL library
|
||||
*/
|
||||
|
||||
|
||||
#ifndef SDL_h_
|
||||
#define SDL_h_
|
||||
|
||||
#include "SDL_main.h"
|
||||
#include "SDL_stdinc.h"
|
||||
#include "SDL_assert.h"
|
||||
#include "SDL_atomic.h"
|
||||
#include "SDL_audio.h"
|
||||
#include "SDL_clipboard.h"
|
||||
#include "SDL_cpuinfo.h"
|
||||
#include "SDL_endian.h"
|
||||
#include "SDL_error.h"
|
||||
#include "SDL_events.h"
|
||||
#include "SDL_filesystem.h"
|
||||
#include "SDL_gamecontroller.h"
|
||||
#include "SDL_haptic.h"
|
||||
#include "SDL_hints.h"
|
||||
#include "SDL_joystick.h"
|
||||
#include "SDL_loadso.h"
|
||||
#include "SDL_log.h"
|
||||
#include "SDL_messagebox.h"
|
||||
#include "SDL_mutex.h"
|
||||
#include "SDL_power.h"
|
||||
#include "SDL_render.h"
|
||||
#include "SDL_rwops.h"
|
||||
#include "SDL_shape.h"
|
||||
#include "SDL_system.h"
|
||||
#include "SDL_thread.h"
|
||||
#include "SDL_timer.h"
|
||||
#include "SDL_version.h"
|
||||
#include "SDL_video.h"
|
||||
|
||||
#include "begin_code.h"
|
||||
/* Set up for C function definitions, even when using C++ */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* As of version 0.5, SDL is loaded dynamically into the application */
|
||||
|
||||
/**
|
||||
* \name SDL_INIT_*
|
||||
*
|
||||
* These are the flags which may be passed to SDL_Init(). You should
|
||||
* specify the subsystems which you will be using in your application.
|
||||
*/
|
||||
/* @{ */
|
||||
#define SDL_INIT_TIMER 0x00000001u
|
||||
#define SDL_INIT_AUDIO 0x00000010u
|
||||
#define SDL_INIT_VIDEO 0x00000020u /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
|
||||
#define SDL_INIT_JOYSTICK 0x00000200u /**< SDL_INIT_JOYSTICK implies SDL_INIT_EVENTS */
|
||||
#define SDL_INIT_HAPTIC 0x00001000u
|
||||
#define SDL_INIT_GAMECONTROLLER 0x00002000u /**< SDL_INIT_GAMECONTROLLER implies SDL_INIT_JOYSTICK */
|
||||
#define SDL_INIT_EVENTS 0x00004000u
|
||||
#define SDL_INIT_NOPARACHUTE 0x00100000u /**< compatibility; this flag is ignored. */
|
||||
#define SDL_INIT_EVERYTHING ( \
|
||||
SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_EVENTS | \
|
||||
SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER \
|
||||
)
|
||||
/* @} */
|
||||
|
||||
/**
|
||||
* This function initializes the subsystems specified by \c flags
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_Init(Uint32 flags);
|
||||
|
||||
/**
|
||||
* This function initializes specific SDL subsystems
|
||||
*
|
||||
* Subsystem initialization is ref-counted, you must call
|
||||
* SDL_QuitSubSystem() for each SDL_InitSubSystem() to correctly
|
||||
* shutdown a subsystem manually (or call SDL_Quit() to force shutdown).
|
||||
* If a subsystem is already loaded then this call will
|
||||
* increase the ref-count and return.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_InitSubSystem(Uint32 flags);
|
||||
|
||||
/**
|
||||
* This function cleans up specific SDL subsystems
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_QuitSubSystem(Uint32 flags);
|
||||
|
||||
/**
|
||||
* This function returns a mask of the specified subsystems which have
|
||||
* previously been initialized.
|
||||
*
|
||||
* If \c flags is 0, it returns a mask of all initialized subsystems.
|
||||
*/
|
||||
extern DECLSPEC Uint32 SDLCALL SDL_WasInit(Uint32 flags);
|
||||
|
||||
/**
|
||||
* This function cleans up all initialized subsystems. You should
|
||||
* call it upon all exit conditions.
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_Quit(void);
|
||||
|
||||
/* Ends C function definitions when using C++ */
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#include "close_code.h"
|
||||
|
||||
#endif /* SDL_h_ */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
291
inc/SDL2/SDL_assert.h
Normal file
291
inc/SDL2/SDL_assert.h
Normal file
@@ -0,0 +1,291 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL_assert_h_
|
||||
#define SDL_assert_h_
|
||||
|
||||
#include "SDL_config.h"
|
||||
|
||||
#include "begin_code.h"
|
||||
/* Set up for C function definitions, even when using C++ */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef SDL_ASSERT_LEVEL
|
||||
#ifdef SDL_DEFAULT_ASSERT_LEVEL
|
||||
#define SDL_ASSERT_LEVEL SDL_DEFAULT_ASSERT_LEVEL
|
||||
#elif defined(_DEBUG) || defined(DEBUG) || \
|
||||
(defined(__GNUC__) && !defined(__OPTIMIZE__))
|
||||
#define SDL_ASSERT_LEVEL 2
|
||||
#else
|
||||
#define SDL_ASSERT_LEVEL 1
|
||||
#endif
|
||||
#endif /* SDL_ASSERT_LEVEL */
|
||||
|
||||
/*
|
||||
These are macros and not first class functions so that the debugger breaks
|
||||
on the assertion line and not in some random guts of SDL, and so each
|
||||
assert can have unique static variables associated with it.
|
||||
*/
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
/* Don't include intrin.h here because it contains C++ code */
|
||||
extern void __cdecl __debugbreak(void);
|
||||
#define SDL_TriggerBreakpoint() __debugbreak()
|
||||
#elif ( (!defined(__NACL__)) && ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))) )
|
||||
#define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "int $3\n\t" )
|
||||
#elif defined(__386__) && defined(__WATCOMC__)
|
||||
#define SDL_TriggerBreakpoint() { _asm { int 0x03 } }
|
||||
#elif defined(HAVE_SIGNAL_H) && !defined(__WATCOMC__)
|
||||
#include <signal.h>
|
||||
#define SDL_TriggerBreakpoint() raise(SIGTRAP)
|
||||
#else
|
||||
/* How do we trigger breakpoints on this platform? */
|
||||
#define SDL_TriggerBreakpoint()
|
||||
#endif
|
||||
|
||||
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */
|
||||
# define SDL_FUNCTION __func__
|
||||
#elif ((__GNUC__ >= 2) || defined(_MSC_VER) || defined (__WATCOMC__))
|
||||
# define SDL_FUNCTION __FUNCTION__
|
||||
#else
|
||||
# define SDL_FUNCTION "???"
|
||||
#endif
|
||||
#define SDL_FILE __FILE__
|
||||
#define SDL_LINE __LINE__
|
||||
|
||||
/*
|
||||
sizeof (x) makes the compiler still parse the expression even without
|
||||
assertions enabled, so the code is always checked at compile time, but
|
||||
doesn't actually generate code for it, so there are no side effects or
|
||||
expensive checks at run time, just the constant size of what x WOULD be,
|
||||
which presumably gets optimized out as unused.
|
||||
This also solves the problem of...
|
||||
|
||||
int somevalue = blah();
|
||||
SDL_assert(somevalue == 1);
|
||||
|
||||
...which would cause compiles to complain that somevalue is unused if we
|
||||
disable assertions.
|
||||
*/
|
||||
|
||||
/* "while (0,0)" fools Microsoft's compiler's /W4 warning level into thinking
|
||||
this condition isn't constant. And looks like an owl's face! */
|
||||
#ifdef _MSC_VER /* stupid /W4 warnings. */
|
||||
#define SDL_NULL_WHILE_LOOP_CONDITION (0,0)
|
||||
#else
|
||||
#define SDL_NULL_WHILE_LOOP_CONDITION (0)
|
||||
#endif
|
||||
|
||||
#define SDL_disabled_assert(condition) \
|
||||
do { (void) sizeof ((condition)); } while (SDL_NULL_WHILE_LOOP_CONDITION)
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SDL_ASSERTION_RETRY, /**< Retry the assert immediately. */
|
||||
SDL_ASSERTION_BREAK, /**< Make the debugger trigger a breakpoint. */
|
||||
SDL_ASSERTION_ABORT, /**< Terminate the program. */
|
||||
SDL_ASSERTION_IGNORE, /**< Ignore the assert. */
|
||||
SDL_ASSERTION_ALWAYS_IGNORE /**< Ignore the assert from now on. */
|
||||
} SDL_AssertState;
|
||||
|
||||
typedef struct SDL_AssertData
|
||||
{
|
||||
int always_ignore;
|
||||
unsigned int trigger_count;
|
||||
const char *condition;
|
||||
const char *filename;
|
||||
int linenum;
|
||||
const char *function;
|
||||
const struct SDL_AssertData *next;
|
||||
} SDL_AssertData;
|
||||
|
||||
#if (SDL_ASSERT_LEVEL > 0)
|
||||
|
||||
/* Never call this directly. Use the SDL_assert* macros. */
|
||||
extern DECLSPEC SDL_AssertState SDLCALL SDL_ReportAssertion(SDL_AssertData *,
|
||||
const char *,
|
||||
const char *, int)
|
||||
#if defined(__clang__)
|
||||
#if __has_feature(attribute_analyzer_noreturn)
|
||||
/* this tells Clang's static analysis that we're a custom assert function,
|
||||
and that the analyzer should assume the condition was always true past this
|
||||
SDL_assert test. */
|
||||
__attribute__((analyzer_noreturn))
|
||||
#endif
|
||||
#endif
|
||||
;
|
||||
|
||||
/* the do {} while(0) avoids dangling else problems:
|
||||
if (x) SDL_assert(y); else blah();
|
||||
... without the do/while, the "else" could attach to this macro's "if".
|
||||
We try to handle just the minimum we need here in a macro...the loop,
|
||||
the static vars, and break points. The heavy lifting is handled in
|
||||
SDL_ReportAssertion(), in SDL_assert.c.
|
||||
*/
|
||||
#define SDL_enabled_assert(condition) \
|
||||
do { \
|
||||
while ( !(condition) ) { \
|
||||
static struct SDL_AssertData sdl_assert_data = { \
|
||||
0, 0, #condition, 0, 0, 0, 0 \
|
||||
}; \
|
||||
const SDL_AssertState sdl_assert_state = SDL_ReportAssertion(&sdl_assert_data, SDL_FUNCTION, SDL_FILE, SDL_LINE); \
|
||||
if (sdl_assert_state == SDL_ASSERTION_RETRY) { \
|
||||
continue; /* go again. */ \
|
||||
} else if (sdl_assert_state == SDL_ASSERTION_BREAK) { \
|
||||
SDL_TriggerBreakpoint(); \
|
||||
} \
|
||||
break; /* not retrying. */ \
|
||||
} \
|
||||
} while (SDL_NULL_WHILE_LOOP_CONDITION)
|
||||
|
||||
#endif /* enabled assertions support code */
|
||||
|
||||
/* Enable various levels of assertions. */
|
||||
#if SDL_ASSERT_LEVEL == 0 /* assertions disabled */
|
||||
# define SDL_assert(condition) SDL_disabled_assert(condition)
|
||||
# define SDL_assert_release(condition) SDL_disabled_assert(condition)
|
||||
# define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
|
||||
#elif SDL_ASSERT_LEVEL == 1 /* release settings. */
|
||||
# define SDL_assert(condition) SDL_disabled_assert(condition)
|
||||
# define SDL_assert_release(condition) SDL_enabled_assert(condition)
|
||||
# define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
|
||||
#elif SDL_ASSERT_LEVEL == 2 /* normal settings. */
|
||||
# define SDL_assert(condition) SDL_enabled_assert(condition)
|
||||
# define SDL_assert_release(condition) SDL_enabled_assert(condition)
|
||||
# define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
|
||||
#elif SDL_ASSERT_LEVEL == 3 /* paranoid settings. */
|
||||
# define SDL_assert(condition) SDL_enabled_assert(condition)
|
||||
# define SDL_assert_release(condition) SDL_enabled_assert(condition)
|
||||
# define SDL_assert_paranoid(condition) SDL_enabled_assert(condition)
|
||||
#else
|
||||
# error Unknown assertion level.
|
||||
#endif
|
||||
|
||||
/* this assertion is never disabled at any level. */
|
||||
#define SDL_assert_always(condition) SDL_enabled_assert(condition)
|
||||
|
||||
|
||||
typedef SDL_AssertState (SDLCALL *SDL_AssertionHandler)(
|
||||
const SDL_AssertData* data, void* userdata);
|
||||
|
||||
/**
|
||||
* \brief Set an application-defined assertion handler.
|
||||
*
|
||||
* This allows an app to show its own assertion UI and/or force the
|
||||
* response to an assertion failure. If the app doesn't provide this, SDL
|
||||
* will try to do the right thing, popping up a system-specific GUI dialog,
|
||||
* and probably minimizing any fullscreen windows.
|
||||
*
|
||||
* This callback may fire from any thread, but it runs wrapped in a mutex, so
|
||||
* it will only fire from one thread at a time.
|
||||
*
|
||||
* Setting the callback to NULL restores SDL's original internal handler.
|
||||
*
|
||||
* This callback is NOT reset to SDL's internal handler upon SDL_Quit()!
|
||||
*
|
||||
* Return SDL_AssertState value of how to handle the assertion failure.
|
||||
*
|
||||
* \param handler Callback function, called when an assertion fails.
|
||||
* \param userdata A pointer passed to the callback as-is.
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_SetAssertionHandler(
|
||||
SDL_AssertionHandler handler,
|
||||
void *userdata);
|
||||
|
||||
/**
|
||||
* \brief Get the default assertion handler.
|
||||
*
|
||||
* This returns the function pointer that is called by default when an
|
||||
* assertion is triggered. This is an internal function provided by SDL,
|
||||
* that is used for assertions when SDL_SetAssertionHandler() hasn't been
|
||||
* used to provide a different function.
|
||||
*
|
||||
* \return The default SDL_AssertionHandler that is called when an assert triggers.
|
||||
*/
|
||||
extern DECLSPEC SDL_AssertionHandler SDLCALL SDL_GetDefaultAssertionHandler(void);
|
||||
|
||||
/**
|
||||
* \brief Get the current assertion handler.
|
||||
*
|
||||
* This returns the function pointer that is called when an assertion is
|
||||
* triggered. This is either the value last passed to
|
||||
* SDL_SetAssertionHandler(), or if no application-specified function is
|
||||
* set, is equivalent to calling SDL_GetDefaultAssertionHandler().
|
||||
*
|
||||
* \param puserdata Pointer to a void*, which will store the "userdata"
|
||||
* pointer that was passed to SDL_SetAssertionHandler().
|
||||
* This value will always be NULL for the default handler.
|
||||
* If you don't care about this data, it is safe to pass
|
||||
* a NULL pointer to this function to ignore it.
|
||||
* \return The SDL_AssertionHandler that is called when an assert triggers.
|
||||
*/
|
||||
extern DECLSPEC SDL_AssertionHandler SDLCALL SDL_GetAssertionHandler(void **puserdata);
|
||||
|
||||
/**
|
||||
* \brief Get a list of all assertion failures.
|
||||
*
|
||||
* Get all assertions triggered since last call to SDL_ResetAssertionReport(),
|
||||
* or the start of the program.
|
||||
*
|
||||
* The proper way to examine this data looks something like this:
|
||||
*
|
||||
* <code>
|
||||
* const SDL_AssertData *item = SDL_GetAssertionReport();
|
||||
* while (item) {
|
||||
* printf("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\\n",
|
||||
* item->condition, item->function, item->filename,
|
||||
* item->linenum, item->trigger_count,
|
||||
* item->always_ignore ? "yes" : "no");
|
||||
* item = item->next;
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* \return List of all assertions.
|
||||
* \sa SDL_ResetAssertionReport
|
||||
*/
|
||||
extern DECLSPEC const SDL_AssertData * SDLCALL SDL_GetAssertionReport(void);
|
||||
|
||||
/**
|
||||
* \brief Reset the list of all assertion failures.
|
||||
*
|
||||
* Reset list of all assertions triggered.
|
||||
*
|
||||
* \sa SDL_GetAssertionReport
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_ResetAssertionReport(void);
|
||||
|
||||
|
||||
/* these had wrong naming conventions until 2.0.4. Please update your app! */
|
||||
#define SDL_assert_state SDL_AssertState
|
||||
#define SDL_assert_data SDL_AssertData
|
||||
|
||||
|
||||
/* Ends C function definitions when using C++ */
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#include "close_code.h"
|
||||
|
||||
#endif /* SDL_assert_h_ */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
274
inc/SDL2/SDL_atomic.h
Normal file
274
inc/SDL2/SDL_atomic.h
Normal file
@@ -0,0 +1,274 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file SDL_atomic.h
|
||||
*
|
||||
* Atomic operations.
|
||||
*
|
||||
* IMPORTANT:
|
||||
* If you are not an expert in concurrent lockless programming, you should
|
||||
* only be using the atomic lock and reference counting functions in this
|
||||
* file. In all other cases you should be protecting your data structures
|
||||
* with full mutexes.
|
||||
*
|
||||
* The list of "safe" functions to use are:
|
||||
* SDL_AtomicLock()
|
||||
* SDL_AtomicUnlock()
|
||||
* SDL_AtomicIncRef()
|
||||
* SDL_AtomicDecRef()
|
||||
*
|
||||
* Seriously, here be dragons!
|
||||
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
*
|
||||
* You can find out a little more about lockless programming and the
|
||||
* subtle issues that can arise here:
|
||||
* http://msdn.microsoft.com/en-us/library/ee418650%28v=vs.85%29.aspx
|
||||
*
|
||||
* There's also lots of good information here:
|
||||
* http://www.1024cores.net/home/lock-free-algorithms
|
||||
* http://preshing.com/
|
||||
*
|
||||
* These operations may or may not actually be implemented using
|
||||
* processor specific atomic operations. When possible they are
|
||||
* implemented as true processor specific atomic operations. When that
|
||||
* is not possible the are implemented using locks that *do* use the
|
||||
* available atomic operations.
|
||||
*
|
||||
* All of the atomic operations that modify memory are full memory barriers.
|
||||
*/
|
||||
|
||||
#ifndef SDL_atomic_h_
|
||||
#define SDL_atomic_h_
|
||||
|
||||
#include "SDL_stdinc.h"
|
||||
#include "SDL_platform.h"
|
||||
|
||||
#include "begin_code.h"
|
||||
|
||||
/* Set up for C function definitions, even when using C++ */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \name SDL AtomicLock
|
||||
*
|
||||
* The atomic locks are efficient spinlocks using CPU instructions,
|
||||
* but are vulnerable to starvation and can spin forever if a thread
|
||||
* holding a lock has been terminated. For this reason you should
|
||||
* minimize the code executed inside an atomic lock and never do
|
||||
* expensive things like API or system calls while holding them.
|
||||
*
|
||||
* The atomic locks are not safe to lock recursively.
|
||||
*
|
||||
* Porting Note:
|
||||
* The spin lock functions and type are required and can not be
|
||||
* emulated because they are used in the atomic emulation code.
|
||||
*/
|
||||
/* @{ */
|
||||
|
||||
typedef int SDL_SpinLock;
|
||||
|
||||
/**
|
||||
* \brief Try to lock a spin lock by setting it to a non-zero value.
|
||||
*
|
||||
* \param lock Points to the lock.
|
||||
*
|
||||
* \return SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.
|
||||
*/
|
||||
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock);
|
||||
|
||||
/**
|
||||
* \brief Lock a spin lock by setting it to a non-zero value.
|
||||
*
|
||||
* \param lock Points to the lock.
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
|
||||
|
||||
/**
|
||||
* \brief Unlock a spin lock by setting it to 0. Always returns immediately
|
||||
*
|
||||
* \param lock Points to the lock.
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
|
||||
|
||||
/* @} *//* SDL AtomicLock */
|
||||
|
||||
|
||||
/**
|
||||
* The compiler barrier prevents the compiler from reordering
|
||||
* reads and writes to globally visible variables across the call.
|
||||
*/
|
||||
#if defined(_MSC_VER) && (_MSC_VER > 1200) && !defined(__clang__)
|
||||
void _ReadWriteBarrier(void);
|
||||
#pragma intrinsic(_ReadWriteBarrier)
|
||||
#define SDL_CompilerBarrier() _ReadWriteBarrier()
|
||||
#elif (defined(__GNUC__) && !defined(__EMSCRIPTEN__)) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x5120))
|
||||
/* This is correct for all CPUs when using GCC or Solaris Studio 12.1+. */
|
||||
#define SDL_CompilerBarrier() __asm__ __volatile__ ("" : : : "memory")
|
||||
#elif defined(__WATCOMC__)
|
||||
extern _inline void SDL_CompilerBarrier (void);
|
||||
#pragma aux SDL_CompilerBarrier = "" parm [] modify exact [];
|
||||
#else
|
||||
#define SDL_CompilerBarrier() \
|
||||
{ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); }
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Memory barriers are designed to prevent reads and writes from being
|
||||
* reordered by the compiler and being seen out of order on multi-core CPUs.
|
||||
*
|
||||
* A typical pattern would be for thread A to write some data and a flag,
|
||||
* and for thread B to read the flag and get the data. In this case you
|
||||
* would insert a release barrier between writing the data and the flag,
|
||||
* guaranteeing that the data write completes no later than the flag is
|
||||
* written, and you would insert an acquire barrier between reading the
|
||||
* flag and reading the data, to ensure that all the reads associated
|
||||
* with the flag have completed.
|
||||
*
|
||||
* In this pattern you should always see a release barrier paired with
|
||||
* an acquire barrier and you should gate the data reads/writes with a
|
||||
* single flag variable.
|
||||
*
|
||||
* For more information on these semantics, take a look at the blog post:
|
||||
* http://preshing.com/20120913/acquire-and-release-semantics
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_MemoryBarrierReleaseFunction(void);
|
||||
extern DECLSPEC void SDLCALL SDL_MemoryBarrierAcquireFunction(void);
|
||||
|
||||
#if defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
|
||||
#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("lwsync" : : : "memory")
|
||||
#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("lwsync" : : : "memory")
|
||||
#elif defined(__GNUC__) && defined(__arm__)
|
||||
#if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7EM__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
|
||||
#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("dmb ish" : : : "memory")
|
||||
#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("dmb ish" : : : "memory")
|
||||
#elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_5TE__)
|
||||
#ifdef __thumb__
|
||||
/* The mcr instruction isn't available in thumb mode, use real functions */
|
||||
#define SDL_MemoryBarrierRelease() SDL_MemoryBarrierReleaseFunction()
|
||||
#define SDL_MemoryBarrierAcquire() SDL_MemoryBarrierAcquireFunction()
|
||||
#else
|
||||
#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
|
||||
#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
|
||||
#endif /* __thumb__ */
|
||||
#else
|
||||
#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("" : : : "memory")
|
||||
#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("" : : : "memory")
|
||||
#endif /* __GNUC__ && __arm__ */
|
||||
#else
|
||||
#if (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x5120))
|
||||
/* This is correct for all CPUs on Solaris when using Solaris Studio 12.1+. */
|
||||
#include <mbarrier.h>
|
||||
#define SDL_MemoryBarrierRelease() __machine_rel_barrier()
|
||||
#define SDL_MemoryBarrierAcquire() __machine_acq_barrier()
|
||||
#else
|
||||
/* This is correct for the x86 and x64 CPUs, and we'll expand this over time. */
|
||||
#define SDL_MemoryBarrierRelease() SDL_CompilerBarrier()
|
||||
#define SDL_MemoryBarrierAcquire() SDL_CompilerBarrier()
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief A type representing an atomic integer value. It is a struct
|
||||
* so people don't accidentally use numeric operations on it.
|
||||
*/
|
||||
typedef struct { int value; } SDL_atomic_t;
|
||||
|
||||
/**
|
||||
* \brief Set an atomic variable to a new value if it is currently an old value.
|
||||
*
|
||||
* \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
|
||||
*
|
||||
* \note If you don't know what this function is for, you shouldn't use it!
|
||||
*/
|
||||
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
|
||||
|
||||
/**
|
||||
* \brief Set an atomic variable to a value.
|
||||
*
|
||||
* \return The previous value of the atomic variable.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_AtomicSet(SDL_atomic_t *a, int v);
|
||||
|
||||
/**
|
||||
* \brief Get the value of an atomic variable
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_AtomicGet(SDL_atomic_t *a);
|
||||
|
||||
/**
|
||||
* \brief Add to an atomic variable.
|
||||
*
|
||||
* \return The previous value of the atomic variable.
|
||||
*
|
||||
* \note This same style can be used for any number operation
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_AtomicAdd(SDL_atomic_t *a, int v);
|
||||
|
||||
/**
|
||||
* \brief Increment an atomic variable used as a reference count.
|
||||
*/
|
||||
#ifndef SDL_AtomicIncRef
|
||||
#define SDL_AtomicIncRef(a) SDL_AtomicAdd(a, 1)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief Decrement an atomic variable used as a reference count.
|
||||
*
|
||||
* \return SDL_TRUE if the variable reached zero after decrementing,
|
||||
* SDL_FALSE otherwise
|
||||
*/
|
||||
#ifndef SDL_AtomicDecRef
|
||||
#define SDL_AtomicDecRef(a) (SDL_AtomicAdd(a, -1) == 1)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief Set a pointer to a new value if it is currently an old value.
|
||||
*
|
||||
* \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
|
||||
*
|
||||
* \note If you don't know what this function is for, you shouldn't use it!
|
||||
*/
|
||||
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval);
|
||||
|
||||
/**
|
||||
* \brief Set a pointer to a value atomically.
|
||||
*
|
||||
* \return The previous value of the pointer.
|
||||
*/
|
||||
extern DECLSPEC void* SDLCALL SDL_AtomicSetPtr(void **a, void* v);
|
||||
|
||||
/**
|
||||
* \brief Get the value of a pointer atomically.
|
||||
*/
|
||||
extern DECLSPEC void* SDLCALL SDL_AtomicGetPtr(void **a);
|
||||
|
||||
/* Ends C function definitions when using C++ */
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#include "close_code.h"
|
||||
|
||||
#endif /* SDL_atomic_h_ */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
825
inc/SDL2/SDL_audio.h
Normal file
825
inc/SDL2/SDL_audio.h
Normal file
@@ -0,0 +1,825 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file SDL_audio.h
|
||||
*
|
||||
* Access to the raw audio mixing buffer for the SDL library.
|
||||
*/
|
||||
|
||||
#ifndef SDL_audio_h_
|
||||
#define SDL_audio_h_
|
||||
|
||||
#include "SDL_stdinc.h"
|
||||
#include "SDL_error.h"
|
||||
#include "SDL_endian.h"
|
||||
#include "SDL_mutex.h"
|
||||
#include "SDL_thread.h"
|
||||
#include "SDL_rwops.h"
|
||||
|
||||
#include "begin_code.h"
|
||||
/* Set up for C function definitions, even when using C++ */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief Audio format flags.
|
||||
*
|
||||
* These are what the 16 bits in SDL_AudioFormat currently mean...
|
||||
* (Unspecified bits are always zero).
|
||||
*
|
||||
* \verbatim
|
||||
++-----------------------sample is signed if set
|
||||
||
|
||||
|| ++-----------sample is bigendian if set
|
||||
|| ||
|
||||
|| || ++---sample is float if set
|
||||
|| || ||
|
||||
|| || || +---sample bit size---+
|
||||
|| || || | |
|
||||
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
|
||||
\endverbatim
|
||||
*
|
||||
* There are macros in SDL 2.0 and later to query these bits.
|
||||
*/
|
||||
typedef Uint16 SDL_AudioFormat;
|
||||
|
||||
/**
|
||||
* \name Audio flags
|
||||
*/
|
||||
/* @{ */
|
||||
|
||||
#define SDL_AUDIO_MASK_BITSIZE (0xFF)
|
||||
#define SDL_AUDIO_MASK_DATATYPE (1<<8)
|
||||
#define SDL_AUDIO_MASK_ENDIAN (1<<12)
|
||||
#define SDL_AUDIO_MASK_SIGNED (1<<15)
|
||||
#define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
|
||||
#define SDL_AUDIO_ISFLOAT(x) (x & SDL_AUDIO_MASK_DATATYPE)
|
||||
#define SDL_AUDIO_ISBIGENDIAN(x) (x & SDL_AUDIO_MASK_ENDIAN)
|
||||
#define SDL_AUDIO_ISSIGNED(x) (x & SDL_AUDIO_MASK_SIGNED)
|
||||
#define SDL_AUDIO_ISINT(x) (!SDL_AUDIO_ISFLOAT(x))
|
||||
#define SDL_AUDIO_ISLITTLEENDIAN(x) (!SDL_AUDIO_ISBIGENDIAN(x))
|
||||
#define SDL_AUDIO_ISUNSIGNED(x) (!SDL_AUDIO_ISSIGNED(x))
|
||||
|
||||
/**
|
||||
* \name Audio format flags
|
||||
*
|
||||
* Defaults to LSB byte order.
|
||||
*/
|
||||
/* @{ */
|
||||
#define AUDIO_U8 0x0008 /**< Unsigned 8-bit samples */
|
||||
#define AUDIO_S8 0x8008 /**< Signed 8-bit samples */
|
||||
#define AUDIO_U16LSB 0x0010 /**< Unsigned 16-bit samples */
|
||||
#define AUDIO_S16LSB 0x8010 /**< Signed 16-bit samples */
|
||||
#define AUDIO_U16MSB 0x1010 /**< As above, but big-endian byte order */
|
||||
#define AUDIO_S16MSB 0x9010 /**< As above, but big-endian byte order */
|
||||
#define AUDIO_U16 AUDIO_U16LSB
|
||||
#define AUDIO_S16 AUDIO_S16LSB
|
||||
/* @} */
|
||||
|
||||
/**
|
||||
* \name int32 support
|
||||
*/
|
||||
/* @{ */
|
||||
#define AUDIO_S32LSB 0x8020 /**< 32-bit integer samples */
|
||||
#define AUDIO_S32MSB 0x9020 /**< As above, but big-endian byte order */
|
||||
#define AUDIO_S32 AUDIO_S32LSB
|
||||
/* @} */
|
||||
|
||||
/**
|
||||
* \name float32 support
|
||||
*/
|
||||
/* @{ */
|
||||
#define AUDIO_F32LSB 0x8120 /**< 32-bit floating point samples */
|
||||
#define AUDIO_F32MSB 0x9120 /**< As above, but big-endian byte order */
|
||||
#define AUDIO_F32 AUDIO_F32LSB
|
||||
/* @} */
|
||||
|
||||
/**
|
||||
* \name Native audio byte ordering
|
||||
*/
|
||||
/* @{ */
|
||||
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
|
||||
#define AUDIO_U16SYS AUDIO_U16LSB
|
||||
#define AUDIO_S16SYS AUDIO_S16LSB
|
||||
#define AUDIO_S32SYS AUDIO_S32LSB
|
||||
#define AUDIO_F32SYS AUDIO_F32LSB
|
||||
#else
|
||||
#define AUDIO_U16SYS AUDIO_U16MSB
|
||||
#define AUDIO_S16SYS AUDIO_S16MSB
|
||||
#define AUDIO_S32SYS AUDIO_S32MSB
|
||||
#define AUDIO_F32SYS AUDIO_F32MSB
|
||||
#endif
|
||||
/* @} */
|
||||
|
||||
/**
|
||||
* \name Allow change flags
|
||||
*
|
||||
* Which audio format changes are allowed when opening a device.
|
||||
*/
|
||||
/* @{ */
|
||||
#define SDL_AUDIO_ALLOW_FREQUENCY_CHANGE 0x00000001
|
||||
#define SDL_AUDIO_ALLOW_FORMAT_CHANGE 0x00000002
|
||||
#define SDL_AUDIO_ALLOW_CHANNELS_CHANGE 0x00000004
|
||||
#define SDL_AUDIO_ALLOW_ANY_CHANGE (SDL_AUDIO_ALLOW_FREQUENCY_CHANGE|SDL_AUDIO_ALLOW_FORMAT_CHANGE|SDL_AUDIO_ALLOW_CHANNELS_CHANGE)
|
||||
/* @} */
|
||||
|
||||
/* @} *//* Audio flags */
|
||||
|
||||
/**
|
||||
* This function is called when the audio device needs more data.
|
||||
*
|
||||
* \param userdata An application-specific parameter saved in
|
||||
* the SDL_AudioSpec structure
|
||||
* \param stream A pointer to the audio data buffer.
|
||||
* \param len The length of that buffer in bytes.
|
||||
*
|
||||
* Once the callback returns, the buffer will no longer be valid.
|
||||
* Stereo samples are stored in a LRLRLR ordering.
|
||||
*
|
||||
* You can choose to avoid callbacks and use SDL_QueueAudio() instead, if
|
||||
* you like. Just open your audio device with a NULL callback.
|
||||
*/
|
||||
typedef void (SDLCALL * SDL_AudioCallback) (void *userdata, Uint8 * stream,
|
||||
int len);
|
||||
|
||||
/**
|
||||
* The calculated values in this structure are calculated by SDL_OpenAudio().
|
||||
*
|
||||
* For multi-channel audio, the default SDL channel mapping is:
|
||||
* 2: FL FR (stereo)
|
||||
* 3: FL FR LFE (2.1 surround)
|
||||
* 4: FL FR BL BR (quad)
|
||||
* 5: FL FR FC BL BR (quad + center)
|
||||
* 6: FL FR FC LFE SL SR (5.1 surround - last two can also be BL BR)
|
||||
* 7: FL FR FC LFE BC SL SR (6.1 surround)
|
||||
* 8: FL FR FC LFE BL BR SL SR (7.1 surround)
|
||||
*/
|
||||
typedef struct SDL_AudioSpec
|
||||
{
|
||||
int freq; /**< DSP frequency -- samples per second */
|
||||
SDL_AudioFormat format; /**< Audio data format */
|
||||
Uint8 channels; /**< Number of channels: 1 mono, 2 stereo */
|
||||
Uint8 silence; /**< Audio buffer silence value (calculated) */
|
||||
Uint16 samples; /**< Audio buffer size in sample FRAMES (total samples divided by channel count) */
|
||||
Uint16 padding; /**< Necessary for some compile environments */
|
||||
Uint32 size; /**< Audio buffer size in bytes (calculated) */
|
||||
SDL_AudioCallback callback; /**< Callback that feeds the audio device (NULL to use SDL_QueueAudio()). */
|
||||
void *userdata; /**< Userdata passed to callback (ignored for NULL callbacks). */
|
||||
} SDL_AudioSpec;
|
||||
|
||||
|
||||
struct SDL_AudioCVT;
|
||||
typedef void (SDLCALL * SDL_AudioFilter) (struct SDL_AudioCVT * cvt,
|
||||
SDL_AudioFormat format);
|
||||
|
||||
/**
|
||||
* \brief Upper limit of filters in SDL_AudioCVT
|
||||
*
|
||||
* The maximum number of SDL_AudioFilter functions in SDL_AudioCVT is
|
||||
* currently limited to 9. The SDL_AudioCVT.filters array has 10 pointers,
|
||||
* one of which is the terminating NULL pointer.
|
||||
*/
|
||||
#define SDL_AUDIOCVT_MAX_FILTERS 9
|
||||
|
||||
/**
|
||||
* \struct SDL_AudioCVT
|
||||
* \brief A structure to hold a set of audio conversion filters and buffers.
|
||||
*
|
||||
* Note that various parts of the conversion pipeline can take advantage
|
||||
* of SIMD operations (like SSE2, for example). SDL_AudioCVT doesn't require
|
||||
* you to pass it aligned data, but can possibly run much faster if you
|
||||
* set both its (buf) field to a pointer that is aligned to 16 bytes, and its
|
||||
* (len) field to something that's a multiple of 16, if possible.
|
||||
*/
|
||||
#ifdef __GNUC__
|
||||
/* This structure is 84 bytes on 32-bit architectures, make sure GCC doesn't
|
||||
pad it out to 88 bytes to guarantee ABI compatibility between compilers.
|
||||
vvv
|
||||
The next time we rev the ABI, make sure to size the ints and add padding.
|
||||
*/
|
||||
#define SDL_AUDIOCVT_PACKED __attribute__((packed))
|
||||
#else
|
||||
#define SDL_AUDIOCVT_PACKED
|
||||
#endif
|
||||
/* */
|
||||
typedef struct SDL_AudioCVT
|
||||
{
|
||||
int needed; /**< Set to 1 if conversion possible */
|
||||
SDL_AudioFormat src_format; /**< Source audio format */
|
||||
SDL_AudioFormat dst_format; /**< Target audio format */
|
||||
double rate_incr; /**< Rate conversion increment */
|
||||
Uint8 *buf; /**< Buffer to hold entire audio data */
|
||||
int len; /**< Length of original audio buffer */
|
||||
int len_cvt; /**< Length of converted audio buffer */
|
||||
int len_mult; /**< buffer must be len*len_mult big */
|
||||
double len_ratio; /**< Given len, final size is len*len_ratio */
|
||||
SDL_AudioFilter filters[SDL_AUDIOCVT_MAX_FILTERS + 1]; /**< NULL-terminated list of filter functions */
|
||||
int filter_index; /**< Current audio conversion function */
|
||||
} SDL_AUDIOCVT_PACKED SDL_AudioCVT;
|
||||
|
||||
|
||||
/* Function prototypes */
|
||||
|
||||
/**
|
||||
* \name Driver discovery functions
|
||||
*
|
||||
* These functions return the list of built in audio drivers, in the
|
||||
* order that they are normally initialized by default.
|
||||
*/
|
||||
/* @{ */
|
||||
extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
|
||||
extern DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);
|
||||
/* @} */
|
||||
|
||||
/**
|
||||
* \name Initialization and cleanup
|
||||
*
|
||||
* \internal These functions are used internally, and should not be used unless
|
||||
* you have a specific need to specify the audio driver you want to
|
||||
* use. You should normally use SDL_Init() or SDL_InitSubSystem().
|
||||
*/
|
||||
/* @{ */
|
||||
extern DECLSPEC int SDLCALL SDL_AudioInit(const char *driver_name);
|
||||
extern DECLSPEC void SDLCALL SDL_AudioQuit(void);
|
||||
/* @} */
|
||||
|
||||
/**
|
||||
* This function returns the name of the current audio driver, or NULL
|
||||
* if no driver has been initialized.
|
||||
*/
|
||||
extern DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);
|
||||
|
||||
/**
|
||||
* This function opens the audio device with the desired parameters, and
|
||||
* returns 0 if successful, placing the actual hardware parameters in the
|
||||
* structure pointed to by \c obtained. If \c obtained is NULL, the audio
|
||||
* data passed to the callback function will be guaranteed to be in the
|
||||
* requested format, and will be automatically converted to the hardware
|
||||
* audio format if necessary. This function returns -1 if it failed
|
||||
* to open the audio device, or couldn't set up the audio thread.
|
||||
*
|
||||
* When filling in the desired audio spec structure,
|
||||
* - \c desired->freq should be the desired audio frequency in samples-per-
|
||||
* second.
|
||||
* - \c desired->format should be the desired audio format.
|
||||
* - \c desired->samples is the desired size of the audio buffer, in
|
||||
* samples. This number should be a power of two, and may be adjusted by
|
||||
* the audio driver to a value more suitable for the hardware. Good values
|
||||
* seem to range between 512 and 8096 inclusive, depending on the
|
||||
* application and CPU speed. Smaller values yield faster response time,
|
||||
* but can lead to underflow if the application is doing heavy processing
|
||||
* and cannot fill the audio buffer in time. A stereo sample consists of
|
||||
* both right and left channels in LR ordering.
|
||||
* Note that the number of samples is directly related to time by the
|
||||
* following formula: \code ms = (samples*1000)/freq \endcode
|
||||
* - \c desired->size is the size in bytes of the audio buffer, and is
|
||||
* calculated by SDL_OpenAudio().
|
||||
* - \c desired->silence is the value used to set the buffer to silence,
|
||||
* and is calculated by SDL_OpenAudio().
|
||||
* - \c desired->callback should be set to a function that will be called
|
||||
* when the audio device is ready for more data. It is passed a pointer
|
||||
* to the audio buffer, and the length in bytes of the audio buffer.
|
||||
* This function usually runs in a separate thread, and so you should
|
||||
* protect data structures that it accesses by calling SDL_LockAudio()
|
||||
* and SDL_UnlockAudio() in your code. Alternately, you may pass a NULL
|
||||
* pointer here, and call SDL_QueueAudio() with some frequency, to queue
|
||||
* more audio samples to be played (or for capture devices, call
|
||||
* SDL_DequeueAudio() with some frequency, to obtain audio samples).
|
||||
* - \c desired->userdata is passed as the first parameter to your callback
|
||||
* function. If you passed a NULL callback, this value is ignored.
|
||||
*
|
||||
* The audio device starts out playing silence when it's opened, and should
|
||||
* be enabled for playing by calling \c SDL_PauseAudio(0) when you are ready
|
||||
* for your audio callback function to be called. Since the audio driver
|
||||
* may modify the requested size of the audio buffer, you should allocate
|
||||
* any local mixing buffers after you open the audio device.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_OpenAudio(SDL_AudioSpec * desired,
|
||||
SDL_AudioSpec * obtained);
|
||||
|
||||
/**
|
||||
* SDL Audio Device IDs.
|
||||
*
|
||||
* A successful call to SDL_OpenAudio() is always device id 1, and legacy
|
||||
* SDL audio APIs assume you want this device ID. SDL_OpenAudioDevice() calls
|
||||
* always returns devices >= 2 on success. The legacy calls are good both
|
||||
* for backwards compatibility and when you don't care about multiple,
|
||||
* specific, or capture devices.
|
||||
*/
|
||||
typedef Uint32 SDL_AudioDeviceID;
|
||||
|
||||
/**
|
||||
* Get the number of available devices exposed by the current driver.
|
||||
* Only valid after a successfully initializing the audio subsystem.
|
||||
* Returns -1 if an explicit list of devices can't be determined; this is
|
||||
* not an error. For example, if SDL is set up to talk to a remote audio
|
||||
* server, it can't list every one available on the Internet, but it will
|
||||
* still allow a specific host to be specified to SDL_OpenAudioDevice().
|
||||
*
|
||||
* In many common cases, when this function returns a value <= 0, it can still
|
||||
* successfully open the default device (NULL for first argument of
|
||||
* SDL_OpenAudioDevice()).
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_GetNumAudioDevices(int iscapture);
|
||||
|
||||
/**
|
||||
* Get the human-readable name of a specific audio device.
|
||||
* Must be a value between 0 and (number of audio devices-1).
|
||||
* Only valid after a successfully initializing the audio subsystem.
|
||||
* The values returned by this function reflect the latest call to
|
||||
* SDL_GetNumAudioDevices(); recall that function to redetect available
|
||||
* hardware.
|
||||
*
|
||||
* The string returned by this function is UTF-8 encoded, read-only, and
|
||||
* managed internally. You are not to free it. If you need to keep the
|
||||
* string for any length of time, you should make your own copy of it, as it
|
||||
* will be invalid next time any of several other SDL functions is called.
|
||||
*/
|
||||
extern DECLSPEC const char *SDLCALL SDL_GetAudioDeviceName(int index,
|
||||
int iscapture);
|
||||
|
||||
|
||||
/**
|
||||
* Open a specific audio device. Passing in a device name of NULL requests
|
||||
* the most reasonable default (and is equivalent to calling SDL_OpenAudio()).
|
||||
*
|
||||
* The device name is a UTF-8 string reported by SDL_GetAudioDeviceName(), but
|
||||
* some drivers allow arbitrary and driver-specific strings, such as a
|
||||
* hostname/IP address for a remote audio server, or a filename in the
|
||||
* diskaudio driver.
|
||||
*
|
||||
* \return 0 on error, a valid device ID that is >= 2 on success.
|
||||
*
|
||||
* SDL_OpenAudio(), unlike this function, always acts on device ID 1.
|
||||
*/
|
||||
extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(const char
|
||||
*device,
|
||||
int iscapture,
|
||||
const
|
||||
SDL_AudioSpec *
|
||||
desired,
|
||||
SDL_AudioSpec *
|
||||
obtained,
|
||||
int
|
||||
allowed_changes);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* \name Audio state
|
||||
*
|
||||
* Get the current audio state.
|
||||
*/
|
||||
/* @{ */
|
||||
typedef enum
|
||||
{
|
||||
SDL_AUDIO_STOPPED = 0,
|
||||
SDL_AUDIO_PLAYING,
|
||||
SDL_AUDIO_PAUSED
|
||||
} SDL_AudioStatus;
|
||||
extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioStatus(void);
|
||||
|
||||
extern DECLSPEC SDL_AudioStatus SDLCALL
|
||||
SDL_GetAudioDeviceStatus(SDL_AudioDeviceID dev);
|
||||
/* @} *//* Audio State */
|
||||
|
||||
/**
|
||||
* \name Pause audio functions
|
||||
*
|
||||
* These functions pause and unpause the audio callback processing.
|
||||
* They should be called with a parameter of 0 after opening the audio
|
||||
* device to start playing sound. This is so you can safely initialize
|
||||
* data for your callback function after opening the audio device.
|
||||
* Silence will be written to the audio device during the pause.
|
||||
*/
|
||||
/* @{ */
|
||||
extern DECLSPEC void SDLCALL SDL_PauseAudio(int pause_on);
|
||||
extern DECLSPEC void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev,
|
||||
int pause_on);
|
||||
/* @} *//* Pause audio functions */
|
||||
|
||||
/**
|
||||
* This function loads a WAVE from the data source, automatically freeing
|
||||
* that source if \c freesrc is non-zero. For example, to load a WAVE file,
|
||||
* you could do:
|
||||
* \code
|
||||
* SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, ...);
|
||||
* \endcode
|
||||
*
|
||||
* If this function succeeds, it returns the given SDL_AudioSpec,
|
||||
* filled with the audio data format of the wave data, and sets
|
||||
* \c *audio_buf to a malloc()'d buffer containing the audio data,
|
||||
* and sets \c *audio_len to the length of that audio buffer, in bytes.
|
||||
* You need to free the audio buffer with SDL_FreeWAV() when you are
|
||||
* done with it.
|
||||
*
|
||||
* This function returns NULL and sets the SDL error message if the
|
||||
* wave file cannot be opened, uses an unknown data format, or is
|
||||
* corrupt. Currently raw and MS-ADPCM WAVE files are supported.
|
||||
*/
|
||||
extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
|
||||
int freesrc,
|
||||
SDL_AudioSpec * spec,
|
||||
Uint8 ** audio_buf,
|
||||
Uint32 * audio_len);
|
||||
|
||||
/**
|
||||
* Loads a WAV from a file.
|
||||
* Compatibility convenience function.
|
||||
*/
|
||||
#define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
|
||||
SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
|
||||
|
||||
/**
|
||||
* This function frees data previously allocated with SDL_LoadWAV_RW()
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_FreeWAV(Uint8 * audio_buf);
|
||||
|
||||
/**
|
||||
* This function takes a source format and rate and a destination format
|
||||
* and rate, and initializes the \c cvt structure with information needed
|
||||
* by SDL_ConvertAudio() to convert a buffer of audio data from one format
|
||||
* to the other. An unsupported format causes an error and -1 will be returned.
|
||||
*
|
||||
* \return 0 if no conversion is needed, 1 if the audio filter is set up,
|
||||
* or -1 on error.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
|
||||
SDL_AudioFormat src_format,
|
||||
Uint8 src_channels,
|
||||
int src_rate,
|
||||
SDL_AudioFormat dst_format,
|
||||
Uint8 dst_channels,
|
||||
int dst_rate);
|
||||
|
||||
/**
|
||||
* Once you have initialized the \c cvt structure using SDL_BuildAudioCVT(),
|
||||
* created an audio buffer \c cvt->buf, and filled it with \c cvt->len bytes of
|
||||
* audio data in the source format, this function will convert it in-place
|
||||
* to the desired format.
|
||||
*
|
||||
* The data conversion may expand the size of the audio data, so the buffer
|
||||
* \c cvt->buf should be allocated after the \c cvt structure is initialized by
|
||||
* SDL_BuildAudioCVT(), and should be \c cvt->len*cvt->len_mult bytes long.
|
||||
*
|
||||
* \return 0 on success or -1 if \c cvt->buf is NULL.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT * cvt);
|
||||
|
||||
/* SDL_AudioStream is a new audio conversion interface.
|
||||
The benefits vs SDL_AudioCVT:
|
||||
- it can handle resampling data in chunks without generating
|
||||
artifacts, when it doesn't have the complete buffer available.
|
||||
- it can handle incoming data in any variable size.
|
||||
- You push data as you have it, and pull it when you need it
|
||||
*/
|
||||
/* this is opaque to the outside world. */
|
||||
struct _SDL_AudioStream;
|
||||
typedef struct _SDL_AudioStream SDL_AudioStream;
|
||||
|
||||
/**
|
||||
* Create a new audio stream
|
||||
*
|
||||
* \param src_format The format of the source audio
|
||||
* \param src_channels The number of channels of the source audio
|
||||
* \param src_rate The sampling rate of the source audio
|
||||
* \param dst_format The format of the desired audio output
|
||||
* \param dst_channels The number of channels of the desired audio output
|
||||
* \param dst_rate The sampling rate of the desired audio output
|
||||
* \return 0 on success, or -1 on error.
|
||||
*
|
||||
* \sa SDL_AudioStreamPut
|
||||
* \sa SDL_AudioStreamGet
|
||||
* \sa SDL_AudioStreamAvailable
|
||||
* \sa SDL_AudioStreamFlush
|
||||
* \sa SDL_AudioStreamClear
|
||||
* \sa SDL_FreeAudioStream
|
||||
*/
|
||||
extern DECLSPEC SDL_AudioStream * SDLCALL SDL_NewAudioStream(const SDL_AudioFormat src_format,
|
||||
const Uint8 src_channels,
|
||||
const int src_rate,
|
||||
const SDL_AudioFormat dst_format,
|
||||
const Uint8 dst_channels,
|
||||
const int dst_rate);
|
||||
|
||||
/**
|
||||
* Add data to be converted/resampled to the stream
|
||||
*
|
||||
* \param stream The stream the audio data is being added to
|
||||
* \param buf A pointer to the audio data to add
|
||||
* \param int The number of bytes to write to the stream
|
||||
* \return 0 on success, or -1 on error.
|
||||
*
|
||||
* \sa SDL_NewAudioStream
|
||||
* \sa SDL_AudioStreamGet
|
||||
* \sa SDL_AudioStreamAvailable
|
||||
* \sa SDL_AudioStreamFlush
|
||||
* \sa SDL_AudioStreamClear
|
||||
* \sa SDL_FreeAudioStream
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len);
|
||||
|
||||
/**
|
||||
* Get converted/resampled data from the stream
|
||||
*
|
||||
* \param stream The stream the audio is being requested from
|
||||
* \param buf A buffer to fill with audio data
|
||||
* \param len The maximum number of bytes to fill
|
||||
* \return The number of bytes read from the stream, or -1 on error
|
||||
*
|
||||
* \sa SDL_NewAudioStream
|
||||
* \sa SDL_AudioStreamPut
|
||||
* \sa SDL_AudioStreamAvailable
|
||||
* \sa SDL_AudioStreamFlush
|
||||
* \sa SDL_AudioStreamClear
|
||||
* \sa SDL_FreeAudioStream
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len);
|
||||
|
||||
/**
|
||||
* Get the number of converted/resampled bytes available. The stream may be
|
||||
* buffering data behind the scenes until it has enough to resample
|
||||
* correctly, so this number might be lower than what you expect, or even
|
||||
* be zero. Add more data or flush the stream if you need the data now.
|
||||
*
|
||||
* \sa SDL_NewAudioStream
|
||||
* \sa SDL_AudioStreamPut
|
||||
* \sa SDL_AudioStreamGet
|
||||
* \sa SDL_AudioStreamFlush
|
||||
* \sa SDL_AudioStreamClear
|
||||
* \sa SDL_FreeAudioStream
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_AudioStreamAvailable(SDL_AudioStream *stream);
|
||||
|
||||
/**
|
||||
* Tell the stream that you're done sending data, and anything being buffered
|
||||
* should be converted/resampled and made available immediately.
|
||||
*
|
||||
* It is legal to add more data to a stream after flushing, but there will
|
||||
* be audio gaps in the output. Generally this is intended to signal the
|
||||
* end of input, so the complete output becomes available.
|
||||
*
|
||||
* \sa SDL_NewAudioStream
|
||||
* \sa SDL_AudioStreamPut
|
||||
* \sa SDL_AudioStreamGet
|
||||
* \sa SDL_AudioStreamAvailable
|
||||
* \sa SDL_AudioStreamClear
|
||||
* \sa SDL_FreeAudioStream
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_AudioStreamFlush(SDL_AudioStream *stream);
|
||||
|
||||
/**
|
||||
* Clear any pending data in the stream without converting it
|
||||
*
|
||||
* \sa SDL_NewAudioStream
|
||||
* \sa SDL_AudioStreamPut
|
||||
* \sa SDL_AudioStreamGet
|
||||
* \sa SDL_AudioStreamAvailable
|
||||
* \sa SDL_AudioStreamFlush
|
||||
* \sa SDL_FreeAudioStream
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_AudioStreamClear(SDL_AudioStream *stream);
|
||||
|
||||
/**
|
||||
* Free an audio stream
|
||||
*
|
||||
* \sa SDL_NewAudioStream
|
||||
* \sa SDL_AudioStreamPut
|
||||
* \sa SDL_AudioStreamGet
|
||||
* \sa SDL_AudioStreamAvailable
|
||||
* \sa SDL_AudioStreamFlush
|
||||
* \sa SDL_AudioStreamClear
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_FreeAudioStream(SDL_AudioStream *stream);
|
||||
|
||||
#define SDL_MIX_MAXVOLUME 128
|
||||
/**
|
||||
* This takes two audio buffers of the playing audio format and mixes
|
||||
* them, performing addition, volume adjustment, and overflow clipping.
|
||||
* The volume ranges from 0 - 128, and should be set to ::SDL_MIX_MAXVOLUME
|
||||
* for full audio volume. Note this does not change hardware volume.
|
||||
* This is provided for convenience -- you can mix your own audio data.
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_MixAudio(Uint8 * dst, const Uint8 * src,
|
||||
Uint32 len, int volume);
|
||||
|
||||
/**
|
||||
* This works like SDL_MixAudio(), but you specify the audio format instead of
|
||||
* using the format of audio device 1. Thus it can be used when no audio
|
||||
* device is open at all.
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_MixAudioFormat(Uint8 * dst,
|
||||
const Uint8 * src,
|
||||
SDL_AudioFormat format,
|
||||
Uint32 len, int volume);
|
||||
|
||||
/**
|
||||
* Queue more audio on non-callback devices.
|
||||
*
|
||||
* (If you are looking to retrieve queued audio from a non-callback capture
|
||||
* device, you want SDL_DequeueAudio() instead. This will return -1 to
|
||||
* signify an error if you use it with capture devices.)
|
||||
*
|
||||
* SDL offers two ways to feed audio to the device: you can either supply a
|
||||
* callback that SDL triggers with some frequency to obtain more audio
|
||||
* (pull method), or you can supply no callback, and then SDL will expect
|
||||
* you to supply data at regular intervals (push method) with this function.
|
||||
*
|
||||
* There are no limits on the amount of data you can queue, short of
|
||||
* exhaustion of address space. Queued data will drain to the device as
|
||||
* necessary without further intervention from you. If the device needs
|
||||
* audio but there is not enough queued, it will play silence to make up
|
||||
* the difference. This means you will have skips in your audio playback
|
||||
* if you aren't routinely queueing sufficient data.
|
||||
*
|
||||
* This function copies the supplied data, so you are safe to free it when
|
||||
* the function returns. This function is thread-safe, but queueing to the
|
||||
* same device from two threads at once does not promise which buffer will
|
||||
* be queued first.
|
||||
*
|
||||
* You may not queue audio on a device that is using an application-supplied
|
||||
* callback; doing so returns an error. You have to use the audio callback
|
||||
* or queue audio with this function, but not both.
|
||||
*
|
||||
* You should not call SDL_LockAudio() on the device before queueing; SDL
|
||||
* handles locking internally for this function.
|
||||
*
|
||||
* \param dev The device ID to which we will queue audio.
|
||||
* \param data The data to queue to the device for later playback.
|
||||
* \param len The number of bytes (not samples!) to which (data) points.
|
||||
* \return 0 on success, or -1 on error.
|
||||
*
|
||||
* \sa SDL_GetQueuedAudioSize
|
||||
* \sa SDL_ClearQueuedAudio
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
|
||||
|
||||
/**
|
||||
* Dequeue more audio on non-callback devices.
|
||||
*
|
||||
* (If you are looking to queue audio for output on a non-callback playback
|
||||
* device, you want SDL_QueueAudio() instead. This will always return 0
|
||||
* if you use it with playback devices.)
|
||||
*
|
||||
* SDL offers two ways to retrieve audio from a capture device: you can
|
||||
* either supply a callback that SDL triggers with some frequency as the
|
||||
* device records more audio data, (push method), or you can supply no
|
||||
* callback, and then SDL will expect you to retrieve data at regular
|
||||
* intervals (pull method) with this function.
|
||||
*
|
||||
* There are no limits on the amount of data you can queue, short of
|
||||
* exhaustion of address space. Data from the device will keep queuing as
|
||||
* necessary without further intervention from you. This means you will
|
||||
* eventually run out of memory if you aren't routinely dequeueing data.
|
||||
*
|
||||
* Capture devices will not queue data when paused; if you are expecting
|
||||
* to not need captured audio for some length of time, use
|
||||
* SDL_PauseAudioDevice() to stop the capture device from queueing more
|
||||
* data. This can be useful during, say, level loading times. When
|
||||
* unpaused, capture devices will start queueing data from that point,
|
||||
* having flushed any capturable data available while paused.
|
||||
*
|
||||
* This function is thread-safe, but dequeueing from the same device from
|
||||
* two threads at once does not promise which thread will dequeued data
|
||||
* first.
|
||||
*
|
||||
* You may not dequeue audio from a device that is using an
|
||||
* application-supplied callback; doing so returns an error. You have to use
|
||||
* the audio callback, or dequeue audio with this function, but not both.
|
||||
*
|
||||
* You should not call SDL_LockAudio() on the device before queueing; SDL
|
||||
* handles locking internally for this function.
|
||||
*
|
||||
* \param dev The device ID from which we will dequeue audio.
|
||||
* \param data A pointer into where audio data should be copied.
|
||||
* \param len The number of bytes (not samples!) to which (data) points.
|
||||
* \return number of bytes dequeued, which could be less than requested.
|
||||
*
|
||||
* \sa SDL_GetQueuedAudioSize
|
||||
* \sa SDL_ClearQueuedAudio
|
||||
*/
|
||||
extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
|
||||
|
||||
/**
|
||||
* Get the number of bytes of still-queued audio.
|
||||
*
|
||||
* For playback device:
|
||||
*
|
||||
* This is the number of bytes that have been queued for playback with
|
||||
* SDL_QueueAudio(), but have not yet been sent to the hardware. This
|
||||
* number may shrink at any time, so this only informs of pending data.
|
||||
*
|
||||
* Once we've sent it to the hardware, this function can not decide the
|
||||
* exact byte boundary of what has been played. It's possible that we just
|
||||
* gave the hardware several kilobytes right before you called this
|
||||
* function, but it hasn't played any of it yet, or maybe half of it, etc.
|
||||
*
|
||||
* For capture devices:
|
||||
*
|
||||
* This is the number of bytes that have been captured by the device and
|
||||
* are waiting for you to dequeue. This number may grow at any time, so
|
||||
* this only informs of the lower-bound of available data.
|
||||
*
|
||||
* You may not queue audio on a device that is using an application-supplied
|
||||
* callback; calling this function on such a device always returns 0.
|
||||
* You have to queue audio with SDL_QueueAudio()/SDL_DequeueAudio(), or use
|
||||
* the audio callback, but not both.
|
||||
*
|
||||
* You should not call SDL_LockAudio() on the device before querying; SDL
|
||||
* handles locking internally for this function.
|
||||
*
|
||||
* \param dev The device ID of which we will query queued audio size.
|
||||
* \return Number of bytes (not samples!) of queued audio.
|
||||
*
|
||||
* \sa SDL_QueueAudio
|
||||
* \sa SDL_ClearQueuedAudio
|
||||
*/
|
||||
extern DECLSPEC Uint32 SDLCALL SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev);
|
||||
|
||||
/**
|
||||
* Drop any queued audio data. For playback devices, this is any queued data
|
||||
* still waiting to be submitted to the hardware. For capture devices, this
|
||||
* is any data that was queued by the device that hasn't yet been dequeued by
|
||||
* the application.
|
||||
*
|
||||
* Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
|
||||
* playback devices, the hardware will start playing silence if more audio
|
||||
* isn't queued. Unpaused capture devices will start filling the queue again
|
||||
* as soon as they have more data available (which, depending on the state
|
||||
* of the hardware and the thread, could be before this function call
|
||||
* returns!).
|
||||
*
|
||||
* This will not prevent playback of queued audio that's already been sent
|
||||
* to the hardware, as we can not undo that, so expect there to be some
|
||||
* fraction of a second of audio that might still be heard. This can be
|
||||
* useful if you want to, say, drop any pending music during a level change
|
||||
* in your game.
|
||||
*
|
||||
* You may not queue audio on a device that is using an application-supplied
|
||||
* callback; calling this function on such a device is always a no-op.
|
||||
* You have to queue audio with SDL_QueueAudio()/SDL_DequeueAudio(), or use
|
||||
* the audio callback, but not both.
|
||||
*
|
||||
* You should not call SDL_LockAudio() on the device before clearing the
|
||||
* queue; SDL handles locking internally for this function.
|
||||
*
|
||||
* This function always succeeds and thus returns void.
|
||||
*
|
||||
* \param dev The device ID of which to clear the audio queue.
|
||||
*
|
||||
* \sa SDL_QueueAudio
|
||||
* \sa SDL_GetQueuedAudioSize
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
|
||||
|
||||
|
||||
/**
|
||||
* \name Audio lock functions
|
||||
*
|
||||
* The lock manipulated by these functions protects the callback function.
|
||||
* During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
|
||||
* the callback function is not running. Do not call these from the callback
|
||||
* function or you will cause deadlock.
|
||||
*/
|
||||
/* @{ */
|
||||
extern DECLSPEC void SDLCALL SDL_LockAudio(void);
|
||||
extern DECLSPEC void SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
|
||||
extern DECLSPEC void SDLCALL SDL_UnlockAudio(void);
|
||||
extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
|
||||
/* @} *//* Audio lock functions */
|
||||
|
||||
/**
|
||||
* This function shuts down audio processing and closes the audio device.
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDL_CloseAudio(void);
|
||||
extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
|
||||
|
||||
/* Ends C function definitions when using C++ */
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#include "close_code.h"
|
||||
|
||||
#endif /* SDL_audio_h_ */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user