(AGK version: 108.24)
Submitted: 2014-12-16 17:33:26
// this example should work in AGK V1 and AGK V2

// this demo show simple sliding collision

// use the wasd keys or the on screen joystick to move the sphere
// the sphere will hit and slide against the red blocks

// set a radius for the sphere which will also be used for the sphere cast
radius# = 5.0

// make a sphere, colour it, turns its collision off and position it
CreateObjectSphere(1,radius#*2,12,12)
SetObjectCollisionMode(1,0) // this is to stop it interfering with the collision with the map
SetObjectColor(1,0,255,0,255)
SetObjectPosition(1,-50,radius#,-50)

// make three blocks to act as a map
for i = 2 to 4
        CreateObjectBox(i,25,5,25)
        SetObjectColor(i,255,0,0,255)
        SetObjectRotation(i,0,random(0,89),0)
        SetObjectPosition(i,0,2.5,(i-3)*50)
next i

// create a plane, turn its collision off, rotate it and position it under the middle box to act as a ground
// this is purely cosmetic
CreateObjectPlane(5,200,200)
SetObjectCollisionMode(5,0) // this is to stop it interfering with the collision with the map
SetObjectRotation(5,90,0,0)
SetObjectPosition(5,GetObjectX(3),0,GetObjectY(3))


// create a directional light
CreateLightDirectional(1,-1,-1,0,255,255,255)

// position and orientate camera
SetCameraPosition(1,0,200,-100)
SetCameraLookAt(1,0,0,0,0)

// main loop
do

        // save the old position of the sphere
        old_x# = GetObjectX(1)
        old_y# = GetObjectY(1)
        old_z# = GetObjectZ(1)

        // get player input
        joystick_x# = GetJoyStickX()
        joystick_y# = GetJoyStickY()*-1

        // move the green sphere based on the player input
        MoveObjectLocalX(1,joystick_x#)
        MoveObjectLocalZ(1,joystick_y#)

        // get the new position of the sphere
        new_x# = GetObjectX(1)
        new_y# = GetObjectY(1)
        new_z# = GetObjectZ(1)

        // sphere cast between the old and new positions of the sphere
        object_hit = ObjectSphereSlide(0,old_x#,old_y#,old_z#,new_x#,new_y#,new_z#,radius#)

        // the sphere has collide with a box then calculate sphere new position to give sliding collision
        // we do not need to know the Y component of the collision as the sphere only moves on the xz plane
        if object_hit <> 0
                SetObjectPosition(1, GetObjectRayCastSlideX(0), new_y#, GetObjectRayCastSlideZ(0) )
        endif

        sync()

loop
                

Help make AGK better by submitting an example for this command!
(All examples are subject to approval)
Login to post an example of your own.