I must be doing something wrong again here. When I run the app i can aim way outside the shape (red square) and it will still collide.
Using
Create3DPhysicsKinematicBody( p1 )
SetObjectShapeConvexHull( p1 )
GetObject3DPhysicsContactXYZ() Still don't seem to make any sense either
// Project: Scene
// Created: 2018-10-23
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Duck test" )
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 color
r as float
g as float
b as float
endtype
type point
x as float
y as float
z as float
endtype
global gravity as integer = -1
Set3DPhysicsGravity( 0, gravity, 0 )
p1 = CreateObjectPlane(5, 5)
SetObjectPosition(p1, -5, 0, 0)
SetObjectColor(p1, 0, 0xff, 0, 0xff)
Create3DPhysicsKinematicBody( p1 )
SetObjectShapeConvexHull( p1 )
p2 = CreateObjectPlane(5, 5)
SetObjectPosition(p2, 5, 0, 0)
SetObjectColor(p2, 0xff, 0, 0, 0xff)
Create3DPhysicsKinematicBody( p2 )
SetObjectShapeConvexHull( p2 )
global hit as integer
hit = CreateObjectBox(.5, .5, .5)
SetObjectPosition(hit, 0, 0, 0)
SetObjectColor(hit, 0xff, 0, 0xff, 0xff)
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, p1)
if GetPointerPressed() = 1
shooting = TRUE
dir = GetObjectDirectionVector(aim, 1, 1)
Delete3DPhysicsBody(ball)
SetObjectPosition(ball, GetObjectX(aim), GetObjectY(aim), GetObjectZ(aim))
Create3DPhysicsDynamicBody(ball)
SetObjectShapeSphere(ball, 1)
SetObject3DPhysicsLinearVelocity(ball, dir.x, dir.y, dir.z,100)
endif
if shooting = TRUE
CheckContact(ball, p1, p2)
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
function CheckContact(ball as integer, p1, p2)
o as integer
s as string
if GetObject3DPhysicsFirstContact( ball ) = TRUE
repeat
o = GetObject3DPhysicsContactObjectB()
SetObjectPosition(hit, GetObject3DPhysicsContactX(), GetObject3DPhysicsContactY(), GetObject3DPhysicsContactZ())
until GetObject3DPhysicsNextContact() = FALSE
endif
endfunction
// 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