mirror of
https://github.com/libretro/cpp-cheat.git
synced 2025-04-07 13:31:33 +00:00
collision detection
This commit is contained in:
parent
0f89e3465e
commit
a6836a5063
8
TODO.md
8
TODO.md
@ -10,6 +10,14 @@ C++ has many major interesting non standard libs.
|
|||||||
|
|
||||||
Google unit test framework: <http://code.google.com/p/googletest/>
|
Google unit test framework: <http://code.google.com/p/googletest/>
|
||||||
|
|
||||||
|
### Vision
|
||||||
|
|
||||||
|
OpenCV.
|
||||||
|
|
||||||
|
Originally by Intel: <https://en.wikipedia.org/wiki/OpenCV>
|
||||||
|
|
||||||
|
BSD.
|
||||||
|
|
||||||
### Linear algebra
|
### Linear algebra
|
||||||
|
|
||||||
#### eigen
|
#### eigen
|
||||||
|
@ -2,10 +2,12 @@
|
|||||||
|
|
||||||
Tested on Bullet 2.83.
|
Tested on Bullet 2.83.
|
||||||
|
|
||||||
1. [Introduction](introduction.md)
|
1. Theory
|
||||||
1. [Concepts](concepts.md)
|
1. [Introduction](introduction.md)
|
||||||
1. [Build](build.md)
|
1. [Concepts](concepts.md)
|
||||||
1. [Users](users.md)
|
1. [Build](build.md)
|
||||||
|
1. [Source code](source-code.md)
|
||||||
|
1. [Users](users.md)
|
||||||
1. Examples
|
1. Examples
|
||||||
1. [Gravity](gravity.cpp)
|
1. [Gravity](gravity.cpp)
|
||||||
1. [Ground ball](ground ball.cpp)
|
1. [Ground ball](ground ball.cpp)
|
||||||
|
@ -1,4 +1,13 @@
|
|||||||
# TODO
|
# TODO
|
||||||
|
|
||||||
|
- detect when objects collide
|
||||||
|
- http://www.bulletphysics.org/mediawiki-1.5.8/index.php?title=Collision_Callbacks_and_Triggers
|
||||||
|
- http://stackoverflow.com/questions/9117932/detecting-collisions-with-bullet
|
||||||
|
- http://gamedev.stackexchange.com/questions/22442/how-get-collision-callback-of-two-specific-objects-using-bullet-physics
|
||||||
|
- http://www.bulletphysics.org/mediawiki-1.5.8/index.php?title=Collision_Callbacks_and_Triggers
|
||||||
|
- get object rotation: 3 Euler angles. Attempt made at `gravity.cpp`
|
||||||
- Stairs physics: http://gamedev.stackexchange.com/questions/74794/make-the-player-run-onto-stairs-smoothly
|
- Stairs physics: http://gamedev.stackexchange.com/questions/74794/make-the-player-run-onto-stairs-smoothly
|
||||||
- Voronoi: https://www.youtube.com/watch?v=FIPu9_OGFgc
|
- Voronoi: https://www.youtube.com/watch?v=FIPu9_OGFgc
|
||||||
|
- what is the point of motion state?
|
||||||
|
- substeps vs smaller time step on stepSimulation?
|
||||||
|
- collision primitives: http://www.bulletphysics.org/mediawiki-1.5.8/index.php?title=Collision_Shapes
|
||||||
|
@ -9,6 +9,8 @@ A sphere falling infinitely to gravity.
|
|||||||
|
|
||||||
#include <btBulletDynamicsCommon.h>
|
#include <btBulletDynamicsCommon.h>
|
||||||
|
|
||||||
|
#define PRINTF_FLOAT "%7.3f"
|
||||||
|
|
||||||
constexpr float gravity = -10.0f;
|
constexpr float gravity = -10.0f;
|
||||||
constexpr float initialY = 10.0f;
|
constexpr float initialY = 10.0f;
|
||||||
constexpr float timeStep = 1.0f / 60.0f;
|
constexpr float timeStep = 1.0f / 60.0f;
|
||||||
@ -58,12 +60,15 @@ int main() {
|
|||||||
} else {
|
} else {
|
||||||
trans = obj->getWorldTransform();
|
trans = obj->getWorldTransform();
|
||||||
}
|
}
|
||||||
std::printf("%d %d %7.3f %7.3f %7.3f\n",
|
btVector3 origin = trans.getOrigin();
|
||||||
|
// TODO: how to get numbers out of this?
|
||||||
|
btQuaternion rotation = trans.getRotation();
|
||||||
|
std::printf("%d %d " PRINTF_FLOAT " " PRINTF_FLOAT " " PRINTF_FLOAT "\n",
|
||||||
i,
|
i,
|
||||||
j,
|
j,
|
||||||
float(trans.getOrigin().getX()),
|
float(origin.getX()),
|
||||||
float(trans.getOrigin().getY()),
|
float(origin.getY()),
|
||||||
float(trans.getOrigin().getZ()));
|
float(origin.getZ()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,8 @@ A sphere falling and hitting the ground.
|
|||||||
|
|
||||||
#include <btBulletDynamicsCommon.h>
|
#include <btBulletDynamicsCommon.h>
|
||||||
|
|
||||||
|
#define PRINTF_FLOAT "%7.3f"
|
||||||
|
|
||||||
constexpr float gravity = -10.0f;
|
constexpr float gravity = -10.0f;
|
||||||
constexpr float initialY = 10.0f;
|
constexpr float initialY = 10.0f;
|
||||||
constexpr float timeStep = 1.0f / 60.0f;
|
constexpr float timeStep = 1.0f / 60.0f;
|
||||||
@ -16,17 +18,49 @@ constexpr float groundRestitution = 0.9f;
|
|||||||
constexpr float sphereRestitution = 0.9f;
|
constexpr float sphereRestitution = 0.9f;
|
||||||
constexpr int maxNPoints = 500;
|
constexpr int maxNPoints = 500;
|
||||||
|
|
||||||
|
static void myTickCallback(btDynamicsWorld *world, btScalar timeStep) {
|
||||||
|
bool hadCollision = false;
|
||||||
|
for (int i = 0; i < numManifolds; i++) {
|
||||||
|
btPersistentManifold* contactManifold = dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i);
|
||||||
|
const btCollisionObject* obA = static_cast<const btCollisionObject*>(contactManifold->getBody0());
|
||||||
|
const btCollisionObject* obB = static_cast<const btCollisionObject*>(contactManifold->getBody1());
|
||||||
|
int numContacts = contactManifold->getNumContacts();
|
||||||
|
for (int j = 0; j < numContacts; j++) {
|
||||||
|
if (!hadCollision)
|
||||||
|
std::printf("1 ");
|
||||||
|
hadCollision = true;
|
||||||
|
btManifoldPoint& pt = contactManifold->getContactPoint(j);
|
||||||
|
if (pt.getDistance() < 0.0f) {
|
||||||
|
const btVector3& ptA = pt.getPositionWorldOnA();
|
||||||
|
const btVector3& ptB = pt.getPositionWorldOnB();
|
||||||
|
const btVector3& normalOnB = pt.m_normalWorldOnB;
|
||||||
|
std::printf(
|
||||||
|
PRINTF_FLOAT " " PRINTF_FLOAT " " PRINTF_FLOAT " "
|
||||||
|
PRINTF_FLOAT " " PRINTF_FLOAT " " PRINTF_FLOAT " "
|
||||||
|
PRINTF_FLOAT " " PRINTF_FLOAT " " PRINTF_FLOAT " ",
|
||||||
|
ptA.getX(), ptA.getY(), ptA.getZ(),
|
||||||
|
ptB.getX(), ptB.getY(), ptB.getZ(),
|
||||||
|
normalOnB.getX(), normalOnB.getY(), normalOnB.getZ());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!hadCollision)
|
||||||
|
std::printf("0 ");
|
||||||
|
puts("");
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int i, j;
|
int i, j;
|
||||||
|
|
||||||
btDefaultCollisionConfiguration* collisionConfiguration
|
btDefaultCollisionConfiguration *collisionConfiguration
|
||||||
= new btDefaultCollisionConfiguration();
|
= new btDefaultCollisionConfiguration();
|
||||||
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
|
btCollisionDispatcher *dispatcher = new btCollisionDispatcher(collisionConfiguration);
|
||||||
btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
|
btBroadphaseInterface *overlappingPairCache = new btDbvtBroadphase();
|
||||||
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
|
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
|
||||||
btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(
|
btDiscreteDynamicsWorld *dynamicsWorld = new btDiscreteDynamicsWorld(
|
||||||
dispatcher, overlappingPairCache, solver, collisionConfiguration);
|
dispatcher, overlappingPairCache, solver, collisionConfiguration);
|
||||||
dynamicsWorld->setGravity(btVector3(0, gravity, 0));
|
dynamicsWorld->setGravity(btVector3(0, gravity, 0));
|
||||||
|
dynamicsWorld->setInternalTickCallback(myTickCallback);
|
||||||
btAlignedObjectArray<btCollisionShape*> collisionShapes;
|
btAlignedObjectArray<btCollisionShape*> collisionShapes;
|
||||||
|
|
||||||
// Ground.
|
// Ground.
|
||||||
@ -77,7 +111,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Main loop.
|
// Main loop.
|
||||||
std::printf("step body x y z\n");
|
std::printf("step body x y z collision a b normal\n");
|
||||||
for (i = 0; i < maxNPoints; ++i) {
|
for (i = 0; i < maxNPoints; ++i) {
|
||||||
dynamicsWorld->stepSimulation(timeStep, 10);
|
dynamicsWorld->stepSimulation(timeStep, 10);
|
||||||
for (j = dynamicsWorld->getNumCollisionObjects() - 1; j >= 0; --j) {
|
for (j = dynamicsWorld->getNumCollisionObjects() - 1; j >= 0; --j) {
|
||||||
@ -89,12 +123,14 @@ int main() {
|
|||||||
} else {
|
} else {
|
||||||
trans = obj->getWorldTransform();
|
trans = obj->getWorldTransform();
|
||||||
}
|
}
|
||||||
std::printf("%d %d %7.3f %7.3f %7.3f\n",
|
btVector3 origin = trans.getOrigin();
|
||||||
|
std::printf("%d %d " PRINTF_FLOAT " " PRINTF_FLOAT " " PRINTF_FLOAT " ",
|
||||||
i,
|
i,
|
||||||
j,
|
j,
|
||||||
float(trans.getOrigin().getX()),
|
float(origin.getX()),
|
||||||
float(trans.getOrigin().getY()),
|
float(origin.getY()),
|
||||||
float(trans.getOrigin().getZ()));
|
float(origin.getZ()));
|
||||||
|
int numManifolds = dynamicsWorld->getDispatcher()->getNumManifolds();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
#!/usr/bin/env gnuplot
|
#!/usr/bin/env gnuplot
|
||||||
set key autotitle columnheader
|
set key autotitle columnheader
|
||||||
plot 'ground_ball.tmp' using 1:($2 == 1 ? $4 : 1/0)
|
plot 'ground_ball.tmp' using 1:($2 == 1 ? $4 : 1/0), \
|
||||||
|
'' using 1:($2 == 1 ? $6 : 1/0)
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
# Introduction
|
# Introduction
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
Very little besides the Doxygen: <http://bulletphysics.org/Bullet/BulletFull/>
|
||||||
|
|
||||||
|
Good luck.
|
||||||
|
|
||||||
## C API
|
## C API
|
||||||
|
|
||||||
Bullet is written in C++, and exposes only a C++ API.
|
Bullet is written in C++, and exposes only a C++ API.
|
||||||
|
8
bullet/source-code.md
Normal file
8
bullet/source-code.md
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Source code
|
||||||
|
|
||||||
|
## Style
|
||||||
|
|
||||||
|
Bullet is ugly :-)
|
||||||
|
|
||||||
|
- bt hard-coded prefix instead of C++ namespace
|
||||||
|
- tabs, including in the middle of lines
|
Loading…
x
Reference in New Issue
Block a user