View/Model without multiple objects or funky picking events... oh my! Just some hacks.

0 favourites
  • 2 posts
From the Asset Store
40 Funky tanks PNG transparent in different colours - 2048x2048
  • How to swap collision polygons without interrupting an animation

    TLDR: I use unofficial "Getters" to the construct3 CollisionEngine, in order to directly set collision polygons and bypass the sprite plugin changeframe/animation behavior. This allows me to control and check collisions from a behavior, and reduces alot of the overhead to changing frames through an ace. Since I bypass the change frame entirely, it doesn't disrupt any currently playing animations. It only affects collision polys on the object.

    The problem : Compound collider objects require picking loops, containers(no families), hierarchies (with picking loops for animation), or similar solutions

    The issue isn't obvious until you try to scale, extend, or have performance issues, then it's painful.

    When it comes to managing complex/compound objects, do you ever get annoyed with picking, iterating hierarchies, or dealing with containers, just to be able to have multiple collision polygons for an object? Picking loops often require a "foreach" loop, which can be expensive at scale. Hierarchies do a lot of additional lifting but will still likely require a loop if you need to separate view and model. And containers don't work with families.

    Sometimes, all of this isn't a big deal for unique objects, or those in lower quantities. But the more you have, the more difficult scaling will be, and extending any solution through abstraction will also require a lot of repetition.

    You can avoid the above if you create an animation on the object and simply switch frames for different collider polygons, BUT that comes at the cost of not being able to run animations automatically, and will require additional systems for tracking the animation state. Swapping frames around can also cause any triggers set up to repeat.

    The solution : WorldInfo polygon swapping.

    There are a number of ways to approach this, but currently, the way I do it is as follows:

    You will need a distinct objectType in the editor to be able to create animations and create collision polygons for various frames. It has no other purpose than to serve as a place to edit your collision polygons.

    Then you will need the object you want to have the super ability to change colliders without changing animation frames or interrupting the animation playing.

    Last... you have to create a behavior or script an equivalent tool editor side to then automate the process of changing polys. I chose to use a behavior.

    The behavior

    The behavior is going to have an action with 3 parameters to pass in an objectType(restricted to sprites), an animation string name, and the number of frames in the animation to collect.

    Here is a sample bit of code that sets up the behavior: (obj, animName, frameCount)

    const pickedInstance = obj.GetFirstPicked();		
    frameCount = Math.min(frameCount, this._stateCount);		
    const animation = pickedInstance.GetSdkInstance()._objectClass.GetAnimationByName(animName);
    for(let i = 0; i < frameCount; i++) {
    	this._stateFrames[i] = animation.GetFrameAt( i );
    }

    I haven't done any error checks here, so you have to be sure there is at least one object instance you are referencing, and that the animation name exists on the object and there are enough frames in it.

    Basically, this just creates a reference in an array on the behavior to the animation frames that have the collision polygons you want to use

    Later, via actions, or in tick(), you can do something like this to use it:

    const wi = this.GetWorldInfo();
    const ce = this.GetRuntime().GetCollisionEngine();
    
    //you can iterate through all states with a for loop, or pick individual frames
    //Keep all related variables in equal length arrays and you can easily iterate
    //Use solids, build a candidate list, or test against whatever
    wi.SetSourceCollisionPoly(this._stateFrames[i].GetCollisionPoly());
    wi.SetBboxChanged();
    if (ce.TestOverlapSolid(this._inst)) {
    	this._state[i] = 1;
    }
    else this._state[i] = 0;

    All in all, you can use the index however you please, I have variables on each object type that are the keys : ie _stateUp = 3, You are also free to add as many states/frames as you wish.

    For me, I also loaded the behavior with PushOut functions for solids and arbitrary object types, a suite of vector math tools, raycast tools, and some advanced physics/velocity/integration ACES.

    Using aces editor side, Creating a goomba is as simple as defining initializing the behavior with a 2 frame state check.

    EveryTick:

    ---Goomba UpdateStates

    Goomba State(left) or Goomba State(right)

    ---Goomba InvertXVelocity

    EveryTick:

    ---Goomba HandlePhysics //Integrate velocity and pushout of collision if applicable

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I should add, depending on the setup, you may need to wi.setwidth set height, change image point, etc...

    If you aren't familiar with any of that, you should probably dig around and make sure you know the basics.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)