Game Development Community

Per frame collision polys

by Dan Pascal · in Torque X 2D · 12/03/2007 (8:28 pm) · 10 replies

Is there a way to create a different collsion poly for each frame of an animated sprite?

#1
12/03/2007 (8:43 pm)
By default, no. In this thread there's some discussion about how someone might go about doing it.
#2
12/03/2007 (9:02 pm)
Thanks- I didn't read deep enough...

I'm going to try your suggestion.
#3
12/03/2007 (9:07 pm)
... 3. Copy the template's collision poly into the t2dAnimatedSprite's collision component.

could you show me what that would look like?

thanks
#4
12/03/2007 (11:15 pm)
I tried some experiments, and I've got some partial success, but it's not working completely.
public T2DAnimatedSprite AnimatedSprite
        {
            get { return Owner as T2DAnimatedSprite; }
        }

        public T2DSceneObject Source0
        {
            get { return _source[0]; }
            set { _source[0] = value; }
        }

        public T2DSceneObject Source1
        {
            get { return _source[1]; }
            set { _source[1] = value; }
        }
public void UpdateCollisionPoly(int frame)
        {
            // figure out which cell of the image map is being displayed.
            List<string> cellString = new List<string>(AnimatedSprite.AnimationData.AnimationFrames.Split(' '));
            int cell = int.Parse(cellString[frame]);

            T2DCollisionComponent myCollision = SceneObject.Components.FindComponent<T2DCollisionComponent>();
            T2DCollisionComponent sourceCollision = _source[cell].Components.FindComponent<T2DCollisionComponent>();
            if ((myCollision != null) && (sourceCollision != null))
            {
                // clear old images.
                while (myCollision.Images.Count > 0)
                {
                    myCollision.RemoveImage(myCollision.Images[0]);
                }

                // add new image.
                for (int i = 0; i < sourceCollision.Images.Count; i++)
                {
                    myCollision.InstallImage(sourceCollision.Images[ i]);
                }
            }

            myCollision.RenderCollisionBounds = true;
        }
protected override bool _OnRegister(TorqueObject owner)
        {
            if (!base._OnRegister(owner) || !(owner is T2DSceneObject))
                return false;

            // only attach to T2DAnimatedSprite
            if (!(owner is T2DAnimatedSprite))
                return false;

            // todo: perform initialization for the component

            // todo: look up interfaces exposed by other components
            // E.g., 
            // _theirInterface = Owner.Components.GetInterface<ValueInterface<float>>("float", "their interface name");            

            AnimatedSprite.OnFrameChange = UpdateCollisionPoly;

            return true;
        }
As of right now it never seems to stop detecting a collision with an object, even though the polygon changes so that there shouldn't be any contact anymore. I'm getting too tired to try to debug further tonight. I may be missing something simple, or there may be subtleties that make this harder to do than I had hoped.

(Amusing note: the forum was interpreting my array index of "i" as the tag to start italicizing...)
#5
12/03/2007 (11:48 pm)
Actually, maybe it is working. I was using T2DTriggerComponent to try to test things, but it seems to be tracking its own set of collision images separate from T2DCollisionComponent. When I did the same removeImage/installImage thing for the T2DTriggerComponent on my object, things seemed to work.
#6
12/04/2007 (6:26 am)
Thanks Dan!
I'll chew on this today.
I'm surprised that this is new. You'd think that per frame collison would be necessary for any game...
#7
12/04/2007 (8:21 pm)
Dan,
I was trying to figure out why the use of arrays, since there is only going to be a maximum of 1 image at all times.

Is that just the way to distinguish between 0 and 1?
#8
12/04/2007 (9:55 pm)
Do you mean the _source[] arrays? Those are to hold the templates that have the different collision polygons for the different frames. I only set it up for 2 for testing purposes, but presumably you'd want one per cell of your sprite sheet.

Do you mean why are the Images arrays? If so, that's because Torque X lets you have multiple collision polys on a single object (so you can get concave shapes by combining multiple convex shapes), even though the editor doesn't support that yet.
#9
12/05/2007 (8:32 am)
Ahh...
ok

I was confused at why you'd have to clear all images and then add new images each time.
But that is because torque does not differentiate between the indexed images, it just adds them all togther to make it's calculation.... as opposed to "on this frame, use this indexed image", (which would be great no?)

I haven't got it working yet but I'll post a video when I do.

The blank scene objects that I'm using for the extra poly images, do they need to be templates?

thanks
#10
12/05/2007 (8:40 am)
They don't need to be, but I would probably do it so that they aren't actually objects in the scene -- there's no need for the physics, etc., systems to care about these dummy objects, so I would just make them templates, since they're only being used to store this data, not interact with anything else in the scene.