www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: fireside on December 19, 2010, 12:05:51 am

Title: cloning an extended Object3D
Post by: fireside on December 19, 2010, 12:05:51 am
Maybe this is obvious, but I'm not much of a programmer.  How do I clone an extended Object3D class?  The clone() function returns an Object3D.
Title: Re: cloning an extended Object3D
Post by: paulscode on December 19, 2010, 12:56:32 am
I'm pretty sure this would have to be done by overriding the clone() method and do everything the current clone() method does plus inheriting any additional properties that you've added to your extended class.  I personally prefer to wrap Object3D in my new classes rather than extending it (makes things like this easier to do without having the jPCT sourcecode to reference).
Title: Re: cloning an extended Object3D
Post by: fireside on December 19, 2010, 05:41:14 am
What kind of hit would I take if I make new instances for moving characters?
edit: I guess I'll just put about 20 in a scene and see how it works.  If not, I'll try to figure out a wrapper.  The problem is I extended everything from an actor class, so there are two extensions above Object3D.
BTW, if anyone can post a brief summary on how they clone characters with animations in a class and still get access from the world, I'd appreciate it.
Title: Re: cloning an extended Object3D
Post by: paulscode on December 19, 2010, 08:17:45 pm
I haven't personally done any cloning with animations yet, so I'll leave that question to someone else who has.  For the basic Object3D wrapped into a class, I use a simple infrastructure like this:

Code: [Select]
public class SuperObject3D
{
    private Object3D basicObject3D = null;
    // Other variables, properties, etc ..

    public SuperObject3D( Object3D obj /*args for other properties*/ )
    {
        basicObject3D = obj;
        // Set other variables, properties, etc ...
    }

    public SuperObject3D cloneObject()
    {
        return ( new SuperObject3D( basicObject3D.cloneObject() /*other args*/ ) );
    }
}

And then I extend that for various subcategories of objects.  I would think that you could use the same infrastructure for animations -- doesn't cloneObject() also clone the Object3D's animations as well?
Title: Re: cloning an extended Object3D
Post by: EgonOlsen on December 19, 2010, 08:40:01 pm
You can simply use the Object3D(<Object3D>)-constructors for this. I'm doing this the Robombs sources, so you can have a look there if you want to.
Title: Re: cloning an extended Object3D
Post by: fireside on December 19, 2010, 11:54:28 pm
I'm currently using the Object3D(Object3D) for my extended class constructors, but I don't understand how to clone my extended class, since Object3d.cloneObject() returns an Object3D, so if I do an extendedClass ec = extendedClassInstance.cloneObect(), I get imcompatible types because it returns an Object3D.
I looked at robombs source but it's over my head.
Would I send the cloned Object3D back into the constructor of my extended class, but then it seems it would no longer need the animations added which come after the constructor?
Title: Re: cloning an extended Object3D
Post by: fireside on December 20, 2010, 05:04:07 am
It works to send a cloned object in the constructor as long as I clear translation for some reason.  I only load the animation on the first instance and all of them are animating.  If I don't clear translation first I get odd behavior on the first move.
Title: Re: cloning an extended Object3D
Post by: raft on December 20, 2010, 07:34:08 pm
..but I don't understand how to clone my extended class, since Object3d.cloneObject() returns an Object3D, so if I do an extendedClass ec = extendedClassInstance.cloneObect(), I get imcompatible types because it returns an Object3D.
not true. since java 5, java supports covariant return types. that means when you override a method, your return type can be a sub type of original return type.

hence below is perfectly legal:
Code: [Select]
public class Extended extends Object3D {
  public Extended clone() {
     //..
  }
}

but dont go too fancy about coveriant return types. indeed i had a bug report (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6970851) about this
Title: Re: cloning an extended Object3D
Post by: fireside on December 20, 2010, 10:11:48 pm
Thanks, I'll do more studying on that.  I hadn't heard of it.