I’m still having trouble getting a simple collision detection example to work. I’ve tried using
trav->show_collisions(window->get_render())
What does this actually show?
I’ve also tried turning on
notify-level-collide spam
which seems to give a lot of output however I’m not sure what it means.
Below is the complete code example I’m using. Maybe someone could try running it to figure out what I’m doing wrong.
#include "pandaFramework.h"
#include "pandaSystem.h"
#include "genericAsyncTask.h"
#include "asyncTaskManager.h"
#include "collisionSphere.h"
#include "collisionPolygon.h"
#include "collisionNode.h"
#include "collisionHandlerPusher.h"
#include "pStatClient.h"
#include <cmath>
#include <vector>
//====================================================
PandaFramework framework;
WindowFramework* window;
Filename mydir;
NodePath mainCamera;
const float mvtDelta = 0.25f;
const float rotAngle = 1.0f;
CollisionTraverser* collTrav;
PT(AsyncTaskManager) taskMgr = AsyncTaskManager::get_global_ptr();
PT(ClockObject) globalClock = ClockObject::get_global_clock();
double currentTime = 0.0;
double lastUpdateTime = 0.0;
const float UPDATE_INTERVAL = 0.25;
//====================================================
void loadModels();
void setupKeyboard();
void setupCollisions();
void setupTasks();
//====================================================
// Task to check for camera collision.
AsyncTask::DoneStatus CameraCollisionTask(GenericAsyncTask* task, void* data) {
//std::cout << "Collision test..." << std::endl;
// Check collisions, will call pusher collision handler if a collision is detected.
collTrav->traverse(window->get_render());
return AsyncTask::DS_cont;
}
//====================================================
void moveFW(const Event* theEvent, void * data) {
std::cout << "I moved forwards!" << std::endl;
float x = mainCamera.get_pos().get_x();
float y = mainCamera.get_pos().get_y();
float heading_in_radians = (mainCamera.get_h() * 3.14159265) / 180.0;
x -= mvtDelta * sin(heading_in_radians);
y += mvtDelta * cos(heading_in_radians);
mainCamera.set_pos(x, y, mainCamera.get_pos().get_z());
}
//====================================================
void moveBW(const Event* theEvent, void * data) {
std::cout << "I moved backwards!" << std::endl;
float x = mainCamera.get_pos().get_x();
float y = mainCamera.get_pos().get_y();
float heading_in_radians = (mainCamera.get_h() * 3.14159265) / 180.0;
x += mvtDelta * sin(heading_in_radians);
y -= mvtDelta * cos(heading_in_radians);
mainCamera.set_pos(x, y, mainCamera.get_pos().get_z());
}
//====================================================
void moveLeft(const Event* theEvent, void * data) {
std::cout << "I moved left!" << std::endl;
float x = mainCamera.get_pos().get_x();
float y = mainCamera.get_pos().get_y();
float heading_in_radians = (mainCamera.get_h() * 3.14159265) / 180.0;
heading_in_radians += 3.14159265 / 2.0;
x -= mvtDelta * sin(heading_in_radians);
y += mvtDelta * cos(heading_in_radians);
mainCamera.set_pos(x, y, mainCamera.get_pos().get_z());
}
//====================================================
void moveRight(const Event* theEvent, void * data) {
std::cout << "I moved right!" << std::endl;
float x = mainCamera.get_pos().get_x();
float y = mainCamera.get_pos().get_y();
float heading_in_radians = (mainCamera.get_h() * 3.14159265) / 180.0;
heading_in_radians -= 3.14159265 / 2.0;
x -= mvtDelta * sin(heading_in_radians);
y += mvtDelta * cos(heading_in_radians);
mainCamera.set_pos(x, y, mainCamera.get_pos().get_z());
}
//====================================================
void moveUp(const Event* theEvent, void * data) {
std::cout << "I moved up!" << std::endl;
float z = mainCamera.get_pos().get_z();
z += mvtDelta;
mainCamera.set_pos(mainCamera.get_pos().get_x(), mainCamera.get_pos().get_y(), z);
}
//====================================================
void moveDown(const Event* theEvent, void * data) {
std::cout << "I moved down!" << std::endl;
float z = mainCamera.get_pos().get_z();
z -= mvtDelta;
mainCamera.set_pos(mainCamera.get_pos().get_x(), mainCamera.get_pos().get_y(), z);
}
//====================================================
void rotateAC(const Event* theEvent, void * data) {
std::cout << "I rotated anticlockwise!" << std::endl;
mainCamera.set_h(mainCamera.get_h() + rotAngle);
}
//====================================================
void rotateCW(const Event* theEvent, void * data) {
std::cout << "I rotated clockwise!" << std::endl;
mainCamera.set_h(mainCamera.get_h() - rotAngle);
}
//====================================================
void connectToPStats(const Event* theEvent, void* data) {
if(PStatClient::is_connected()) {
PStatClient::disconnect();
std::cout << "Disconnected from PStats server." << std::endl;
} else {
string host = ""; // Empty = default config var value
int port = -1; // -1 = default config var value
if(!PStatClient::connect(host, port)) {
std::cout << "Could not connect to PStats server." << std::endl;
} else {
std::cout << "Connected to PStats server." << std::endl;
}
}
}
//====================================================
int main(int argc, char *argv[]) {
//open a new window framework
framework.open_framework(argc, argv);
//set the window title to My Panda3D Window
framework.set_window_title("Collision Test");
//open the window
window = framework.open_window();
// Get the main camera.
mainCamera = window->get_camera_group();
mainCamera.set_pos(0, -50, 2);
// Get the location of the executable file I'm running.
mydir = ExecutionEnvironment::get_binary_name();
mydir = mydir.get_dirname();
nout << mydir << "\n";
setupKeyboard();
loadModels();
setupCollisions();
setupTasks();
//do the main loop, equal to run() in python
framework.main_loop();
//close the window framework
framework.close_framework();
return (0);
}
//====================================================
void loadModels() {
// Load the ground plane.
NodePath groundPlane = window->load_model(framework.get_models(), mydir + "./../models/ground");
groundPlane.reparent_to(window->get_render());
groundPlane.set_scale(1.0, 1.0, 1.0);
groundPlane.set_pos(0, 0, 0);
CollisionNode* cGroundNode = new CollisionNode("Ground");
cGroundNode->add_solid(new CollisionPolygon(LPoint3f(-500, -500, 0),
LPoint3f(500, -500, 0),
LPoint3f(500, 500, 0),
LPoint3f(-500, 500, 0)));
NodePath groundCollisionNodePath = groundPlane.attach_new_node(cGroundNode);
groundCollisionNodePath.show();
}
//====================================================
void setupKeyboard() {
// Setup the keyboard control.
window->enable_keyboard();
framework.define_key("arrow_up", "arrow_up", moveFW, 0);
framework.define_key("arrow_up-up", "arrow_up-up", moveFW, 0);
framework.define_key("arrow_up-repeat", "arrow_up-repeat", moveFW, 0);
framework.define_key("arrow_down", "arrow_down", moveBW, 0);
framework.define_key("arrow_down-up", "arrow_down-up", moveBW, 0);
framework.define_key("arrow_down-repeat", "arrow_down-repeat", moveBW, 0);
framework.define_key("arrow_left", "arrow_left", moveLeft, 0);
framework.define_key("arrow_left-up", "arrow_left-up", moveLeft, 0);
framework.define_key("arrow_left-repeat", "arrow_left-repeat", moveLeft, 0);
framework.define_key("arrow_right", "arrow_right", moveRight, 0);
framework.define_key("arrow_right-up", "arrow_right-up", moveRight, 0);
framework.define_key("arrow_right-repeat", "arrow_right-repeat", moveRight, 0);
framework.define_key("u", "u", moveUp, 0);
framework.define_key("u-up", "u-up", moveUp, 0);
framework.define_key("u-repeat", "u-repeat", moveUp, 0);
framework.define_key("d", "d", moveDown, 0);
framework.define_key("d-up", "d-up", moveDown, 0);
framework.define_key("d-repeat", "d-repeat", moveDown, 0);
framework.define_key("a", "a", rotateAC, 0);
framework.define_key("a-up", "a-up", rotateAC, 0);
framework.define_key("a-repeat", "a-repeat", rotateAC, 0);
framework.define_key("s", "s", rotateCW, 0);
framework.define_key("s-up", "s-up", rotateCW, 0);
framework.define_key("s-repeat", "s-repeat", rotateCW, 0);
framework.define_key("c", "c", connectToPStats, 0);
}
//====================================================
void setupCollisions() {
// Setup general collision detection for the camera.
CollisionNode* cSphereNode = new CollisionNode("Sphere");
cSphereNode->add_solid(new CollisionSphere(mainCamera.get_pos().get_x(),
mainCamera.get_pos().get_y(),
mainCamera.get_pos().get_z(),
1.0f));
NodePath cameraCollisionNodePath = mainCamera.attach_new_node(cSphereNode);
cameraCollisionNodePath.show();
// Setup the collision handler and pusher.
CollisionHandlerPusher* pusher = new CollisionHandlerPusher();
collTrav = new CollisionTraverser();
// Camera will be pushed if a collision is detected
pusher->add_collider(cameraCollisionNodePath, mainCamera);
collTrav->add_collider(cameraCollisionNodePath, pusher);
collTrav->show_collisions(window->get_render());
}
//====================================================
void setupTasks() {
// Add the task to handle camera collision.
taskMgr->add(new GenericAsyncTask("Checks for camera collisons", &CameraCollisionTask, (void*) NULL));
}
//====================================================