Hi guys,
Now that 2.015b is out, it's time to get on with the extra example creation. Here's a preview showing some of what you can expect from the new 3D section. The code is somewhat inelegant but easy enough for newbies to pick up. As you all know I'm not a fan of absolute resolutions, but this example will do for a first introduction into 3D and Physics. I hope you enjoy.
3D Physics collisions and hit detection.
// set window properties
SetSyncRate( 0, 1 )
SetWindowTitle( "3D Physics collisions and hit detection" )
SetWindowSize( 1024, 768, 0 )
SetRandomSeed( 12 )
SetRandomSeed2( 18 )
Create3DPhysicsWorld()
Set3DPhysicsGravity( 0, -3, 0 )
Dim Sphere[ 99 ]
Dim Text[ 99 ]
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetCameraPosition( 1, -150, 50, 0 )
SetCameraLookAt( 1, 0, 0, 0, 0 )
Box = CreateObjectBox( 100, 10, 100 )
SetObjectRotation( Box, 1.0, 1.0, 1.0 )
SetObjectcolor( Box, random( 32, 255 ), random( 32, 255 ), random( 32, 255 ), 255 )
Create3DPhysicsKinematicBody( Box )
SetObject3DPhysicsRestitution( Box, 0.75 )
SetObjectShapeBox( Box )
SetObjectPosition( Box, 0, -50, 0 )
Print( "Floor generated" ) : Sync()
For i = 0 to 99
Sphere[ i ] = CreateObjectSphere( 5, 16, 16 )
SetObjectPosition( Sphere[ i ], random2( -10, 10 ), random2( 50, 150 ), random( -10, 10 ) )
Create3DPhysicsDynamicBody( Sphere[ i ] )
SetObjectcolor( Sphere[ i ], random( 32, 255 ), random( 32, 255 ), random( 32, 255 ), 255 )
SetObjectShapeSphere( Sphere[ i ] )
SetObject3DPhysicsRollingFriction( Sphere[ i ], 0 )
SetObject3DPhysicsRestitution( Sphere[ i ], 0.95 )
Print( "Object " + str( i ) + " of 99 created" )
Sync()
Text[ i ] = CreateText( "" )
SetTextSize( Text[ i ], 12 )
SetTextColor( Text[ i ], 255, 255, 255, 255 )
Next i
SetSyncRate( 60, 1 )
do
For i = 0 to 99
If GetObjectY( Sphere[ i ] ) < -150
Delete3DPhysicsBody( Sphere[ i ] )
SetObjectPosition( Sphere[ i ], random2( -10, 10 ), random2( 50, 150 ), random( -10, 10 ) )
Create3DPhysicsDynamicBody( Sphere[ i ] )
SetObjectShapeSphere( Sphere[ i ] )
SetObject3DPhysicsRollingFriction( Sphere[ i ], 0 )
SetObject3DPhysicsRestitution( Sphere[ i ], 1.0 )
EndIf
Next i
For i = 0 to 99
If GetObject3DPhysicsFirstContact( Sphere[ i ] ) = 1
SetTextString( Text[ i ], "Hit" )
SetTextColor( Text[ i ], 255, 0, 0, 128 )
SetTextPosition( Text[ i ], GetScreenXFrom3D( GetObjectX( Sphere[ i ] ), GetObjectY( Sphere[ i ] ), GetObjectZ( Sphere[ i ] ) ), GetScreenYFrom3D( GetObjectX( Sphere[ i ] ), GetObjectY( Sphere[ i ] ), GetObjectZ( Sphere[ i ] ) ) )
GetObject3DPhysicsNextContact()
Else
SetTextString( Text[ i ], "No hit" )
SetTextColor( Text[ i ], 0, 255, 0, 128 )
SetTextPosition( Text[ i ], GetScreenXFrom3D( GetObjectX( Sphere[ i ] ), GetObjectY( Sphere[ i ] ), GetObjectZ( Sphere[ i ] ) ), GetScreenYFrom3D( GetObjectX( Sphere[ i ] ), GetObjectY( Sphere[ i ] ), GetObjectZ( Sphere[ i ] ) ) )
EndIf
Next i
Print( ScreenFPS() )
Step3DPhysicsWorld()
Sync()
loop