externals
box2d
testbed
test.h
Go to the documentation of this file.
1
// MIT License
2
3
// Copyright (c) 2019 Erin Catto
4
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
12
// The above copyright notice and this permission notice shall be included in all
13
// copies or substantial portions of the Software.
14
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
// SOFTWARE.
22
23
#ifndef TEST_H
24
#define TEST_H
25
26
#include "
box2d/box2d.h
"
27
#include "
draw.h
"
28
29
#include <stdlib.h>
30
31
struct
Settings
;
32
class
Test
;
33
34
#define RAND_LIMIT 32767
35
37
inline
float
RandomFloat
()
38
{
39
float
r = (float)(rand() & (
RAND_LIMIT
));
40
r /=
RAND_LIMIT
;
41
r = 2.0f * r - 1.0f;
42
return
r;
43
}
44
46
inline
float
RandomFloat
(
float
lo,
float
hi)
47
{
48
float
r = (float)(rand() & (
RAND_LIMIT
));
49
r /=
RAND_LIMIT
;
50
r = (hi - lo) * r + lo;
51
return
r;
52
}
53
54
// This is called when a joint in the world is implicitly destroyed
55
// because an attached body is destroyed. This gives us a chance to
56
// nullify the mouse joint.
57
class
DestructionListener
:
public
b2DestructionListener
58
{
59
public
:
60
void
SayGoodbye
(
b2Fixture
* fixture)
override
{
B2_NOT_USED
(fixture); }
61
void
SayGoodbye
(
b2Joint
* joint)
override
;
62
63
Test
*
test
;
64
};
65
66
const
int32
k_maxContactPoints
= 2048;
67
68
struct
ContactPoint
69
{
70
b2Fixture
*
fixtureA
;
71
b2Fixture
*
fixtureB
;
72
b2Vec2
normal
;
73
b2Vec2
position
;
74
b2PointState
state
;
75
float
normalImpulse
;
76
float
tangentImpulse
;
77
float
separation
;
78
};
79
80
class
Test
:
public
b2ContactListener
81
{
82
public
:
83
84
Test
();
85
virtual
~
Test
();
86
87
void
DrawTitle(
const
char
*
string
);
88
virtual
void
Step(
Settings
& settings);
89
virtual
void
UpdateUI
() {}
90
virtual
void
Keyboard
(
int
key) {
B2_NOT_USED
(key); }
91
virtual
void
KeyboardUp
(
int
key) {
B2_NOT_USED
(key); }
92
void
ShiftMouseDown(
const
b2Vec2
& p);
93
virtual
void
MouseDown(
const
b2Vec2
& p);
94
virtual
void
MouseUp(
const
b2Vec2
& p);
95
virtual
void
MouseMove(
const
b2Vec2
& p);
96
void
LaunchBomb();
97
void
LaunchBomb(
const
b2Vec2
& position,
const
b2Vec2
& velocity);
98
99
void
SpawnBomb(
const
b2Vec2
& worldPt);
100
void
CompleteBombSpawn(
const
b2Vec2
& p);
101
102
// Let derived tests know that a joint was destroyed.
103
virtual
void
JointDestroyed
(
b2Joint
* joint) {
B2_NOT_USED
(joint); }
104
105
// Callbacks for derived classes.
106
virtual
void
BeginContact
(
b2Contact
* contact)
override
{
B2_NOT_USED
(contact); }
107
virtual
void
EndContact
(
b2Contact
* contact)
override
{
B2_NOT_USED
(contact); }
108
virtual
void
PreSolve(
b2Contact
* contact,
const
b2Manifold
* oldManifold)
override
;
109
virtual
void
PostSolve
(
b2Contact
* contact,
const
b2ContactImpulse
* impulse)
override
110
{
111
B2_NOT_USED
(contact);
112
B2_NOT_USED
(impulse);
113
}
114
115
void
ShiftOrigin(
const
b2Vec2
& newOrigin);
116
117
protected
:
118
friend
class
DestructionListener
;
119
friend
class
BoundaryListener;
120
friend
class
ContactListener;
121
122
b2Body
*
m_groundBody
;
123
b2AABB
m_worldAABB
;
124
ContactPoint
m_points[
k_maxContactPoints
];
125
int32
m_pointCount
;
126
DestructionListener
m_destructionListener
;
127
int32
m_textLine
;
128
b2World
*
m_world
;
129
b2Body
*
m_bomb
;
130
b2MouseJoint
*
m_mouseJoint
;
131
b2Vec2
m_bombSpawnPoint
;
132
bool
m_bombSpawning
;
133
b2Vec2
m_mouseWorld
;
134
int32
m_stepCount
;
135
int32
m_textIncrement
;
136
b2Profile
m_maxProfile
;
137
b2Profile
m_totalProfile
;
138
};
139
140
typedef
Test
*
TestCreateFcn
();
141
142
int
RegisterTest
(
const
char
* category,
const
char
* name,
TestCreateFcn
* fcn);
143
144
//
145
struct
TestEntry
146
{
147
const
char
*
category
;
148
const
char
*
name
;
149
TestCreateFcn
*
createFcn
;
150
};
151
152
#define MAX_TESTS 256
153
extern
TestEntry
g_testEntries
[
MAX_TESTS
];
154
extern
int
g_testCount
;
155
156
#endif
TestEntry::createFcn
TestCreateFcn * createFcn
Definition:
test.h:149
Test::m_destructionListener
DestructionListener m_destructionListener
Definition:
test.h:126
Test::UpdateUI
virtual void UpdateUI()
Definition:
test.h:89
ContactPoint::position
b2Vec2 position
Definition:
test.h:73
ContactPoint::state
b2PointState state
Definition:
test.h:74
ContactPoint::normal
b2Vec2 normal
Definition:
test.h:72
b2Manifold
Definition:
b2_collision.h:99
ContactPoint::normalImpulse
float normalImpulse
Definition:
test.h:75
draw.h
DestructionListener::test
Test * test
Definition:
test.h:63
ContactPoint
Definition:
test.h:68
Test::m_textLine
int32 m_textLine
Definition:
test.h:127
k_maxContactPoints
const int32 k_maxContactPoints
Definition:
test.h:66
Test
Definition:
test.h:80
Test::EndContact
virtual void EndContact(b2Contact *contact) override
Called when two fixtures cease to touch.
Definition:
test.h:107
TestEntry::category
const char * category
Definition:
test.h:147
b2Contact
Definition:
b2_contact.h:88
RandomFloat
float RandomFloat()
Random number in range [-1,1].
Definition:
test.h:37
Test::m_pointCount
int32 m_pointCount
Definition:
test.h:125
TestEntry
Definition:
test.h:145
b2Vec2
A 2D column vector.
Definition:
b2_math.h:41
MAX_TESTS
#define MAX_TESTS
Definition:
test.h:152
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition:
test.cpp:458
int32
signed int int32
Definition:
b2_types.h:28
Test::m_worldAABB
b2AABB m_worldAABB
Definition:
test.h:123
b2Fixture
Definition:
b2_fixture.h:116
g_testCount
int g_testCount
Definition:
test.cpp:456
ContactPoint::separation
float separation
Definition:
test.h:77
Test::m_groundBody
b2Body * m_groundBody
Definition:
test.h:122
Test::KeyboardUp
virtual void KeyboardUp(int key)
Definition:
test.h:91
DestructionListener
Definition:
test.h:57
Test::m_bomb
b2Body * m_bomb
Definition:
test.h:129
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition:
b2_body.h:128
Test::BeginContact
virtual void BeginContact(b2Contact *contact) override
Called when two fixtures begin to touch.
Definition:
test.h:106
b2Profile
Profiling data. Times are in milliseconds.
Definition:
b2_time_step.h:29
B2_NOT_USED
#define B2_NOT_USED(x)
Definition:
b2_common.h:36
Test::m_bombSpawning
bool m_bombSpawning
Definition:
test.h:132
Test::m_bombSpawnPoint
b2Vec2 m_bombSpawnPoint
Definition:
test.h:131
Test::m_totalProfile
b2Profile m_totalProfile
Definition:
test.h:137
Test::m_mouseJoint
b2MouseJoint * m_mouseJoint
Definition:
test.h:130
Test::m_textIncrement
int32 m_textIncrement
Definition:
test.h:135
Settings
Definition:
settings.h:25
Test::JointDestroyed
virtual void JointDestroyed(b2Joint *joint)
Definition:
test.h:103
Test::m_world
b2World * m_world
Definition:
test.h:128
Test::m_maxProfile
b2Profile m_maxProfile
Definition:
test.h:136
b2ContactImpulse
Definition:
b2_world_callbacks.h:70
b2DestructionListener
Definition:
b2_world_callbacks.h:41
Test::m_mouseWorld
b2Vec2 m_mouseWorld
Definition:
test.h:133
DestructionListener::SayGoodbye
void SayGoodbye(b2Fixture *fixture) override
Definition:
test.h:60
TestEntry::name
const char * name
Definition:
test.h:148
Test::m_stepCount
int32 m_stepCount
Definition:
test.h:134
b2PointState
b2PointState
This is used for determining the state of contact points.
Definition:
b2_collision.h:132
Test::Keyboard
virtual void Keyboard(int key)
Definition:
test.h:90
Test::PostSolve
virtual void PostSolve(b2Contact *contact, const b2ContactImpulse *impulse) override
Definition:
test.h:109
ContactPoint::tangentImpulse
float tangentImpulse
Definition:
test.h:76
b2AABB
An axis aligned bounding box.
Definition:
b2_collision.h:168
b2Joint
Definition:
b2_joint.h:110
b2ContactListener
Definition:
b2_world_callbacks.h:86
ContactPoint::fixtureB
b2Fixture * fixtureB
Definition:
test.h:71
ContactPoint::fixtureA
b2Fixture * fixtureA
Definition:
test.h:70
TestCreateFcn
Test * TestCreateFcn()
Definition:
test.h:140
box2d.h
g_testEntries
TestEntry g_testEntries[MAX_TESTS]
Definition:
test.cpp:455
RAND_LIMIT
#define RAND_LIMIT
Definition:
test.h:34
b2MouseJoint
Definition:
b2_mouse_joint.h:65
b2World
Definition:
b2_world.h:46
mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21