// this example should work with AGK V1 and V2
// watch the ball fall and bounce off the pegs
// set a radius for the ball which will also be the radius for the sphere case
radius# = 5
// create a ball and color it
CreateObjectSphere(1,radius#*2,16,16)
SetObjectColor(1,255,255,0,255)
SetObjectCollisionMode(1,0) // turn collision off so it doesn't interfere with the sphere cast
// create an array of pegs on the xy plane
offset = 10
i = 1
for y = 0 to 5
for x = 0 to 5
inc i
CreateObjectCylinder(i,12,12,24)
SetObjectRotation(i,90,0,0)
SetObjectPosition(i,x*40 + offset,y*40,0)
next x
// this creates a diamond pattern by offseting each row of pegs
offset = offset * -1
next y
// save the number of pegs created (will be used later and I can be bothered to work it out in my head)
num_pegs = i-1
// create a directional light (to make the scene look better)
CreateLightDirectional(1,-1,-0.5,1,255,255,255)
// position and orientate the camera
SetCameraPosition(1,100,250,-350)
SetCameraLookAt(1,100,100,0,0)
// set the value for gravity
gravity# = 0.01
// main loop
do
// set the ball's initial position
pos_x# = random(50,150)+0.5
pos_y# = 230.0
// set the ball's velocities to zero
vel_x# = 0.0
vel_y# = 0.0
// position the ball
SetObjectPosition(1,pos_x#,pos_y#,0.0)
// ball falling loop
repeat
// save old position
old_pos_x# = GetObjectX(1)
old_pos_y# = GetObjectY(1)
// apply gravity to downward velocity
vel_y# = vel_y# - gravity#
// calculate new position of ball
pos_x# = pos_x# + vel_x#
pos_y# = pos_y# + vel_y#
// detect collision of ball against pegs
object_hit = ObjectSphereSlide(0,old_pos_x#,old_pos_y#,0.0,pos_x#,pos_y#,0.0,radius#)
// if ball hits a peg then have the ball bounce off it
if object_hit <> 0
// get new position (over write the current values
pos_x# = GetObjectRayCastSlideX(0)
pos_y# = GetObjectRayCastSlideY(0)
// get the new velocities based on the collision bounce
vel_x# = GetObjectRayCastBounceX(0)
vel_y# = GetObjectRayCastBounceY(0)
endif
// position the ball
SetObjectPosition(1,pos_x#,pos_y#,0.0)
sync()
until pos_y# <= -50
loop
Help make AGK better by submitting an example for this command! (All examples are subject to approval)