i try to inherit from CollisionNode but my code doesn’t work!
#include "pandaFramework.h"
#include "pandaSystem.h"
#include <collisionNode.h>
#include <collisionSphere.h>
#include <collisionRay.h>
#include <collisionTraverser.h>
#include <collisionHandlerQueue.h>
PandaFramework framework;
CollisionTraverser trav;
CollisionHandlerQueue* queue;
CollisionRay* ray;
class MyObject : public CollisionNode {
public:
MyObject(string name) : CollisionNode(name) {
}
static TypeHandle get_class_type() {
return _type_handle;
}
static void init_type() {
CollisionNode::init_type();
register_type(_type_handle, "MyObject", CollisionNode::get_class_type());
}
virtual TypeHandle get_type() const {
return get_class_type();
}
virtual TypeHandle force_init_type() {
init_type();
return get_class_type();
}
private:
static TypeHandle _type_handle;
};
TypeHandle MyObject::_type_handle;
void clickOnObject(const Event* ev, void* data) {
WindowFramework* win = framework.get_window(0);
float x = (2 * win->get_graphics_window()->get_pointer(0).get_x()) / ((float)win->get_graphics_window()->get_x_size())- 1;
float y = (2 * win->get_graphics_window()->get_pointer(0).get_y()) / ((float)win->get_graphics_window()->get_y_size())- 1;
ray->set_from_lens(framework.get_window(0)->get_camera(0), x, y);
trav.traverse(framework.get_window(0)->get_render());
cout << queue->get_num_entries() << endl;
}
int main(int argc, char *argv[]) {
framework.open_framework(argc, argv);
framework.set_window_title("My Panda3D Window");
WindowFramework *window = framework.open_window();
framework.get_window(0)->enable_keyboard();
MyObject* obj = new MyObject("hi");
obj->add_solid(new CollisionSphere(0, 0, 0, 2));
obj->set_into_collide_mask(BitMask32::all_on());
NodePath path(obj);
path.reparent_to(window->get_render());
path.set_pos(-10, -10, -10);
path.show();
window->get_camera_group().look_at(path);
queue = new CollisionHandlerQueue();
CollisionNode* cnode= new CollisionNode("ray");
ray = new CollisionRay();
cnode->add_solid(ray);
NodePath cpath(cnode);
cpath.reparent_to(window->get_camera_group());
trav.add_collider(cpath, queue);
framework.define_key("mouse1", "click", clickOnObject, NULL);
framework.main_loop();
framework.close_framework();
return (0);
}
If i replace this line
MyObject* obj = new MyObject("hi");
with this
CollisionNode* obj = new CollisionNode("hi");
it work!!
why??
thanks!!