// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Convex hull" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
SetPrintSize(30)
create3DPhysicsWorld()
#constant TRUE 1
#constant FALSE 0
type point
x as float
y as float
z as float
endtype
global gravity as integer = -1
Set3DPhysicsGravity( 0, gravity, 0 )
p = CreateObjectPlane(5, 5)
SetObjectPosition(p, 0, 0, 0)
SetObjectColor(p, 0, 0xff, 0, 0xff)
Create3DPhysicsKinematicBody( p )
SetObjectShapeConvexHull( p )
aim = CreateObjectBox(1, 1, 2)
SetObjectPosition(aim, 0, 0, -15)
ball = CreateObjectSphere(1, 8, 8)
SetObjectColor(ball, 0xff, 0, 0, 0xff)
SetCameraPosition(1, 0, 15, -30)
SetObjectLookAt(aim, 0, 0, 0, 0)
dir as point
shooting as integer
do
SetAimPosition(aim, p)
if GetPointerPressed() = 1
shooting = TRUE
dir = GetObjectDirectionVector(aim, 1, 1)
Delete3DPhysicsBody(ball)
SetObjectPosition(ball, GetObjectX(aim), GetObjectY(aim), GetObjectZ(aim))
Create3DPhysicsDynamicBody(ball)
SetObjectShapeSphere(ball)
SetObject3DPhysicsLinearVelocity(ball, dir.x, dir.y, dir.z,100)
endif
print(" Click to shoot")
Step3DPhysicsWorld()
Sync()
loop
function SetAimPosition(aim as integer, p1 as integer)
world as point
depth as float
current as point
cam as point
last as point
i as integer
o as integer
a as float
l as integer
depth = Distance3D(GetObjectPos(p1), GetCameraPos(1))
world.x = Get3DVectorXFromScreen( GetPointerX(), GetPointerY() ) * Depth + GetCameraX(1)
world.y = Get3DVectorYFromScreen( GetPointerX(), GetPointerY() ) * Depth + GetCameraY(1)
world.z = Get3DVectorZFromScreen( GetPointerX(), GetPointerY() ) * Depth + GetCameraZ(1)
SetObjectLookAt(aim, world.x, world.y, world.z, 0)
endfunction
function GetCameraPos(id as integer)
pos as point
pos.x = GetCameraX(id)
pos.y = GetCameraY(id)
pos.z = GetCameraZ(id)
endfunction pos
function GetObjectPos(id as integer)
pos as point
pos.x = GetObjectX(id)
pos.y = GetObjectY(id)
pos.z = GetObjectZ(id)
endfunction pos
function Distance3D(fp as point, tp as point)
dist as float
dist = sqrt((fp.x - tp.x)^2 + (fp.y - tp.y)^2 + (fp.z - tp.z)^2 )
endfunction dist
// GetObjectDirectionVector()
//
// Fills empty vector with a normalized direction vector.
//
// 1 = FWD, 2 = REV, 3 = UP, 4 = DOWN, 5 = RIGHT, 6 = LEFT
//
Function GetObjectDirectionVector( objID as integer, direction as integer, speed as float)
vector as point
tempObj as integer
tempObj = CreateObjectBox( 1.0, 1.0, 1.0 )
SetObjectPosition( tempObj, GetObjectWorldX( objID ), GetObjectWorldY( objID ), GetObjectWorldZ( objID ) )
SetObjectRotation( tempObj, GetobjectWorldAngleX( objID ), GetobjectWorldAngleY(objID), GetobjectWorldAngleZ( objID ) )
select direction
case 1 :
MoveObjectLocalZ( tempObj, speed )
endcase
case 2 :
MoveObjectLocalZ( tempObj, -speed)
endcase
case 3 :
MoveObjectLocalY( tempObj, speed )
endcase
case 4 :
MoveObjectLocalY( tempObj, -speed )
endcase
case 5 :
MoveObjectLocalX( tempObj, speed )
endcase
case 6 :
MoveObjectLocalX( tempObj, -speed )
endcase
endselect
vector.x = GetObjectWorldX( tempObj ) - GetObjectWorldX( objID )
vector.y = GetObjectWorldY( tempObj ) - GetObjectWorldY( objID )
vector.z = GetObjectWorldZ( tempObj ) - GetObjectWorldZ( objID )
DeleteObject(tempObj)
EndFunction vector