Skip to main content

AppGameKit Example: Physics 3D - Bouncing ball emulated

Please, read the EULAs and Licenses of the programs and websites listed before use and don’t register if you are not sure that you need the service offered!!!

Use this information at your own risk


// AppGameKit 2
// Example: Physics 3D - Bouncing ball emulated
// www.150games.com
// www.150juegos.com

// set window properties
SetWindowTitle( "Simple 3D Physics" )
SetWindowSize( 1024, 768, 0 )

// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetScissor(0, 0, 0, 0)

Create3DPhysicsWorld()
Set3DPhysicsGravity(0.0, -1.0, 0.0)

CreateObjectBox( 10, 20,10,10 )
SetObjectColor( 10,  0,255,0,  255)
Create3DPhysicsStaticBody( 10 )
SetObjectShapeBox( 10 )
//SetObject3DPhysicsMass(10, 1000.0) // by default mass of static is infinite
SetObject3DPhysicsFriction(10, 0.5)

CreateObjectSphere( 20, 16,16,16 )
SetObjectPosition( 20, 5.1,30,0 )
SetObjectColor( 20,  255,0,0,  255)
Create3DPhysicsDynamicBody( 20 )
SetObjectShapeSphere( 20 )
//SetObject3DPhysicsMass( 20, 1.0 ) // by default mass of Dynamic is 5.0
SetObject3DPhysicsFriction( 20, 0.5 )
//SetObject3DPhysicsMaxLinearVelocity( 20, 50.0 )
SetObject3DPhysicsCanSleep( 20, 0 )  // prevent stop physics if velocities are 0.000000

SetCameraPosition( 1, 0,30,-80 )
SetCameraLookAt( 1, 0,10,0, 0 )

Create3DPhysicsStaticPlane(0.0, 1.0, 0.0,   -10.0)

contactObjB as Integer
contactObjB = 0

x as Float
y as Float
z as Float
x = 0
y = 0
z = 0

LastVelocityY as Float
LastVelocityY = 0.0

do

    // move the camera with keys
    if ( GetKeyboardExists() = 1 )

   	 if ( GetRawKeyPressed( 27 ) = 1 )
   		 end
   	 endif

   	 if ( GetRawKeyPressed( 37 ) = 1 )
   		 Print("Left")
   		 //SetObject3DPhysicsLinearVelocity( 20, 1.0, 0.0, 0.0, 10.0 )
   		 SetObject3DPhysicsAngularVelocity( 20,  0.0, 0.0, 1.0,  60.0 )
   	 endif

   	 if ( GetRawKeyPressed( 39 ) = 1 )
   		 Print("Right")
   		 //SetObject3DPhysicsLinearVelocity( 20, -1.0, 0.0, 0.0, 10.0 )
   		 SetObject3DPhysicsAngularVelocity( 20,  0.0, 0.0, -1.0,  60.0 )
   	 endif

    endif


    Step3DPhysicsWorld()
    Sync()


    if (GetObject3DPhysicsLinearVelocityY(20) < 0.0)
   	 if (GetObject3DPhysicsLinearVelocityY(20) < LastVelocityY)
   		 LastVelocityY = GetObject3DPhysicsLinearVelocityY(20)
   	 endif
    endif

    if GetObject3DPhysicsFirstContact( 20 )

   	 contactObjB = GetObject3DPhysicsContactObjectB()
    	if (contactObjB <> -1)

        	x = GetObject3DPhysicsContactX()
        	y = GetObject3DPhysicsContactY()
        	z = GetObject3DPhysicsContactZ()

   		 SetObject3DPhysicsLinearVelocity( 20,  0.0, 1.0, 0.0,  Abs(LastVelocityY) - 0.8 )
   		 //LastVelocityY = 0.0
   		 LastVelocityY = LastVelocityY + 1.25

   		 if (GetObject3DPhysicsLinearVelocityY( 20 ) >= 50)
   			 SetObject3DPhysicsLinearVelocity( 20,  0.0, 1.0, 0.0,  50.0 )
   		 endif

   	 endif

    endif



    Print( ScreenFPS() )

    Print( "" )

    Print( "Friction: " + str(GetObject3DPhysicsFriction( 20 )) )
    Print( "Mass: " + str(GetObject3DPhysicsMass( 20 )) )
    Print( "Friction: " + str(GetObject3DPhysicsFriction( 10 )) )
    Print( "Mass: " + str(GetObject3DPhysicsMass( 10 )) )

    Print( "" )

    Print( "First Contact Obj ID:" + str( contactObjB ) )
    Print( "Contact Position:" + str( x ) + "," + str( y ) + "," + str( z ) )
    Print( "LastVelocityY:" + str(LastVelocityY) )

    Print( "" )

    Print( "Linear velocity X: " + str(GetObject3DPhysicsLinearVelocityX( 20 )) )
    Print( "Linear velocity Y: " + str(GetObject3DPhysicsLinearVelocityY( 20 )) )
    Print( "Linear velocity Z: " + str(GetObject3DPhysicsLinearVelocityZ( 20 )) )

    Print( "" )

    Print( "Angular velocity X: " + str(GetObject3DPhysicsAngularVelocityX( 20 )) )
    Print( "Angular velocity Y: " + str(GetObject3DPhysicsAngularVelocityY( 20 )) )
    Print( "Angular velocity Z: " + str(GetObject3DPhysicsAngularVelocityZ( 20 )) )

loop