tModLoader Impact - Custom Player Collision Library

HS_Seraph

Terrarian
CubeCollisions.gif

CubeCollisions2.gif


Impact is a library mod which implements collision surfaces, which simulate player collision with tiles without the presence of any actual tiles. This allows the creation of moving platforms and walls, proper slopes, and complex shaped physical objects.

HOW TO ADD IMPACT TO YOUR MOD:

Impact Library is currently on the mod browser, and can be added as a StrongReference mod Dependency. The mod is also open source, so alternatively the code can be directly integrated into a mod's codebase, provided a few conditions are met (see Disclaimer).

HOW TO USE:

Collision Surfaces are objects which have their positions determined by two end points. To use collision surfaces, they must be defined and updated from another class which is updated ingame, such as an NPC or Projectile.

C#:
[/SIZE]
using Terraria;
using Terraria.ModLoader;


namespace CollisionLib { 
    public class ExampleCubePlatform : ModNPC
    {
        public override void SetStaticDefaults()
        {
            DisplayName.SetDefault("Cube Platform");
        }

        

        public CollisionSurface[] colliders = null;  // because this object has multiple colliders, it uses a list of collision surfaces to make updating them all easier
        public override void SetDefaults()
        {
            npc.width = 124;
            npc.height = 124;
            npc.lifeMax = 1;
            npc.immortal = true;
            npc.dontTakeDamage = true;
            npc.noGravity = true;
            npc.knockBackResist = 0;
            npc.aiStyle = -1;

        }

        public override bool CheckActive()
        {
            return true;
        }

        public override bool PreAI()
        {
            if (colliders == null || colliders.Length != 4) //initializes the collision surfaces if null, this happens before everything else so that the logic works starting the first frame
            {
                colliders = new CollisionSurface[] { 
                    new CollisionSurface(npc.TopLeft, npc.TopRight, new int[] { 1, 1, 1, 1 }),  // the first two vector2s are the endpoints, the int array is the data for collision styles
                    new CollisionSurface(npc.TopLeft, npc.BottomLeft, new int[] { 1, 1, 1, 1 }), 
                    new CollisionSurface(npc.TopRight, npc.BottomRight, new int[] { 1, 1, 1, 1 }),
                    new CollisionSurface(npc.BottomLeft, npc.BottomRight, new int[] { 1, 1, 1, 1 }) };
            }

        /*
         * CollisionStyles controls which sides of the player can collide with each surface,
         * index 0 = bottom collision
         * index 1 = top collision
         * index 2 = left collision
         * index 4 = right collision
         * 
         * value 0 = doesnt collide
         * value 1 = collides without the ability to drop through
         * value 2 = collides but the player can drop through, doesn't do anything different from value 1 unless the index is zero (Ie, the surface is set to collide with the bottom of the player)
         *
         */

            return base.PreAI();
        }


        public override void AI()
        {
            /* calls update and then sets new endpoints, to get collision surfaces to move you need to perform the calls in this order:
             * 
             *  Call Update() in a pre-update or mid-update method
             *  Change endpoint positions afterwards in the same method
             *  Call PostUpdate in a post-update method
             * 
             */

           


            if (colliders != null && colliders.Length == 4) // trying to update a collision surface which isn't defined will lead to a exception
            {

                colliders[0].Update();
                colliders[0].endPoints[0] = npc.Center + (npc.TopLeft - npc.Center).RotatedBy(npc.rotation);
                colliders[0].endPoints[1] = npc.Center + (npc.TopRight - npc.Center).RotatedBy(npc.rotation);

                colliders[1].Update();
                colliders[1].endPoints[0] = npc.Center + (npc.TopLeft - npc.Center).RotatedBy(npc.rotation);
                colliders[1].endPoints[1] = npc.Center + (npc.BottomLeft - npc.Center).RotatedBy(npc.rotation);

                colliders[2].Update();
                colliders[2].endPoints[0] = npc.Center + (npc.TopRight - npc.Center).RotatedBy(npc.rotation);
                colliders[2].endPoints[1] = npc.Center + (npc.BottomRight - npc.Center).RotatedBy(npc.rotation);

                colliders[3].Update();
                colliders[3].endPoints[0] = npc.Center + (npc.BottomLeft - npc.Center).RotatedBy(npc.rotation);
                colliders[3].endPoints[1] = npc.Center + (npc.BottomRight - npc.Center).RotatedBy(npc.rotation);


            }



        }

        public override void PostAI()
        {
            if (colliders != null)
            {
                foreach (CollisionSurface collider in colliders)
                {
                    collider.PostUpdate(); // calls postUpdate for every collision surface
                }
            }
        }

        

    }
}
[SIZE=5]



Disclaimer:
Passing off the work of others as your own is disrespectful to the original creator who allowed your use of their work and generally makes you a jerk. With this in mind, If you use Impact library in your mod without having it as a dependency, either the name of the library, or the name of myself (Seraph), the mod author, must be put somewhere (such as in the mod description) as credit to the original author. This is basically just common sense.


If you have any further questions, feel free to ask below, a download mirror for this mod can be found on this discord server: Join the Seraph's Mods Discord Server!
 
Not a modder at all, but its sooooo good that this kind of stuff it going to be widely available.
 
Not a modder at all, but its sooooo good that this kind of stuff it going to be widely available.
Yea, most implementations don't have support for slopes, rotation, or collisions with other parts of the player than the bottom, whereas mine does. I first used this in Extradimensional (an Ascension mod) but wanted to make it public for others to use now that that mod had a release out
 
Back
Top Bottom