Verse Library verse

01 Fragment

Calculates projectile bounce directions using surface normals, ideal for ricochet weapons or ball physics.

verse-library/reflections/01-fragment.verse

using { /Verse.org/SpatialMath } // We need the math toolbox for vectors

# This is our main program. Think of it as the "Game Rule" for this island.
program BounceProgram:
    # When the game starts, we set up our variables.
    OnBegin<override>()<suspends>:
        # Let's pretend a player just shot a projectile.
        # 'IncomingDirection' is where the bullet was going.
        # Let's say it's moving straight forward: (0, 0, -1)
        IncomingDirection: vector3 = Vector3(0, 0, -1)
        
        # Now, imagine it hits a wall on the right side of the map.
        # The wall's 'Normal' points to the right: (1, 0, 0)
        # This is the Surface Normal we talked about.
        WallNormal: vector3 = Vector3(1, 0, 0)
        
        # HERE IS THE MAGIC LINE:
        # We ask Verse: "Given this direction and this wall, where do I bounce?"
        NewBounceDirection: vector3 = ReflectVector(IncomingDirection, WallNormal)
        
        # Let's print this to the console to see what happened.
        # You can see this in the debug log in UEFN.
        Print("Incoming: ", IncomingDirection)
        Print("Wall Normal: ", WallNormal)
        Print("Bounce Off: ", NewBounceDirection)
        
        # If you run this, you'll see the new direction is (-1, 0, -1).
        # Wait, that looks diagonal. Why?
        # Because ReflectVector reflects across the normal.
        # If you hit a wall head-on (0,0,-1) with a normal of (1,0,0),

Sign in free to read the full source 🌴

This Verse Library file is members-only. Create a free account to view the complete source and download the .verse file.

Sign in with Discord

Comments

    Sign in to vote, comment, or suggest an edit. Sign in