Inherit from CollisionNode

Hi!
for building a game, I think to create a class inherited by CollisionNode to manage the units’ informations. Than I attach a Model to my CollisionNode.

class MyObject : public CollisionNode {
    // ...
    UnitInformation* info;
public:
    MyObject(string str) : CollisionNode(str) {
    //...
    }
    UnitInformation* get_info() {
        return info;
    }
};

//...
MyObject myObject;
myObject.add_solid(new CollisionSphere(0, 0, 0, 1));
NodePath node(myObject);
model.reparentTo(node);

is this the right technique to structure a large game?

thanks for all!!

I’m not sure that I would do it this way, but you could choose this approach.

Note that if you inherit from CollisionNode, you also inherit from ReferenceCount, and you thus inherit all of the rules that go along with this. In particular, you can’t do:

MyObject myObject; 

And you must instead do:

PT(MyObject) myObject = new MyObject; 

You are also inheriting from TypedObject which means you should probably also follow Panda’s TypedObject convention, which means defining get_class_type(), init_type(), get_type(), and force_init_type(), as you see virtually every Panda class doing. Also ensure that you call your own class’s init_type() method sometime at startup (as Panda generally does in the config_blah.cxx file).

David

And if i don’t override those methods? What’s happen?
In your opinion, what’s the best solution?

Thank for your reply!

If you don’t manage the TypedObject interface, then myObject->get_type() will return CollisionNode instead of MyObject. You won’t be able to ask node->is_of_type(MyObject::get_class_type()). You won’t be able to use DCAST(MyObject, node) to perform a type-safe cast to MyObject.

This probably won’t bother you though, if you don’t actually care to use those interfaces. Nothing within Panda will care.

I cannot possible answer your second question; there is no “best” solution for such a broad question. My personal preference would generally be to create the game structures in my own code, and have pointers into the PandaNodes they reference, rather than tying it so intimately into the scene graph; but there are pros and cons of both approaches, and subclassing is sometimes the right answer.

David

thanks a lot…
I try do what do you tell me but when i run my code, the program fall in segmentation fault. This is my code:

#include <pandaFramework.h>
#include <pandaSystem.h>
#include <genericAsyncTask.h>
#include <collisionNode.h>

PandaFramework framework;

class MyObject : public CollisionNode {
    int a;
    static TypeHandle _type_handle;
public:
    MyObject(string str, int a) : CollisionNode(str) {
        NodePath model = framework.get_window(0)->load_model(framework.get_models(), "smiley");
        model.set_pos(0, 0, 0);
        model.reparent_to(NodePath(this));
        this->a = a;
    }
    
    int& get_a() {
        return a;
    }
    
    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();
    }
    
};

TypeHandle MyObject::_type_handle;

int main(int argc, char** argv) {
	framework.open_framework(argc, argv);
	WindowFramework *window = framework.open_window();
	
  NodePath path = NodePath(new MyObject("myObject", 0));
  path.set_pos(0, 10, 0);
  path.reparent_to(window->get_render());
	window->get_camera_group().look_at(path);

	framework.main_loop();
	framework.close_framework();
	return 0;
}

the problem is when i write:

model.reparent_to(NodePath(this));

but i don’t know how reparent model to myObject in MyObject’s constructor…

Can you help me??

Replace that line with this:

add_child(model.node());

Note that you need to call MyObject::init_type() at static init time (or in any case, before you start using your object) to initialise the type system properly.

thanks!!
but now nothing appears on the screen!
i print all node with render.ls() method. this is the output:

PandaNode render S:(CullFaceAttrib RescaleNormalAttrib ShadeModelAttrib)
  PandaNode camera_group
    Camera camera ( PerspectiveLens )
      PerspectiveLens fov = 39.3201 30
  none myObject (0 solids) T:(pos 0 10 0) (hidden)
    ModelRoot smiley.egg
      GeomNode  (1 geoms: S:(TextureAttrib))

I resolve this inserting path.show() line in main function. But… why do i have to add this line? Why is this imply?

thanks for your helps!

CollisionNodes are hidden by default. You’ll need to call show_through() on the child node if you want to make it show up anyway. Alternatively, you can call show() on the node itself, but this’ll make the debug visualisation for the solids show up too.

thanks!!!
And are there consequence on the performance?

I don’t see how calling show_through() could noticeably affect your performance.

oki thanks!!!

What’s the different between PT(MyObject) and MyObject* ? and DCAST(MyObject, node) and dynamic_cast<MyObject*>(node)?

PT() is an automatic pointer for objects that inherit from ReferenceCount. It automatically increases the reference count when you create one and it decreases it when it goes out of scope, making sure that objects automatically get cleaned up when there are no references to them anymore. You should use them to refer to reference counted objects (including PandaNode derivatives). Be sure not to attempt to delete objects that inherit from ReferenceCount.

DCAST is a macro for objects that inherit from TypedObject that performs safe typecasting.

oh yea!
Thank for your explanations!!!

But i don’t understand why is there not a manual section in which there’s explanation this!!!
:smiley:

These interfaces are thoroughly explained in the Panda source code files of the respective interfaces.

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!!

As David already said, you should use this:

PT(MyObject) obj = new MyObject("hi");

I’m not sure if that is the problem here though, because you haven’t elaborated on “doesn’t work”. What doesn’t work? Does it crash? Does it show an error?

the problem isn’t resolved if i use PT instead of *.

There’re no crash. this’s not work: if i click on the sphere, in the terminal is printed 0. if i use CollisionNode instead of MyObject, in the terminal is printed 1…
why?

I also don’t see you initialise the type properly using init_type().

Do I call MyObject::init_type() in main function?

int main(...) {
   ...
   MyObject::init_type();
   ...
}

If i do it, the problem’s not resolved!

Excuse me … I don’t understand what you tell me

PS: ok, i understand but it doesn’t work… i try to move MyObject::init_type method at the beginning of the main and than of open_framework call, but the problem’s not resolved… where am i wrong?
PS2: and is not a register problem: i print obj->get_type().get_name() and it is “MyObject”. and obj->get_type().get_parent_class(0).get_name() is “CollisionNode”.