(Might work in DBC)
` Setup
Sync On : Sync Rate 60
Set Display Mode 1440, 900, 32
Disable Escapekey
` Main Loop
Repeat
` Square
Size = 500
Ang = Ang + 1
RotateSquare( Screen Width() / 2, Screen Height() / 2, Ang, Size )
` Check if in square
Text 10, 10, Str$( InSquare( Screen Width() / 2, Screen Height() / 2, Ang, Size, MouseX(), MouseY() ) )
` Update
Sync
Cls
` Exit on escape key
Until EscapeKey()
End
` Check if point is in square
Function InSquare( SqX, SqY, Ang as Float, Size, PtX, PtY )
` In square
Local In as Boolean
` Get distance and angle between point and center of square
Local Dist as Float
Local PAng as Float
Dist = Sqrt( (PtX - SqX) ^ 2 + (PtY - SqY) ^ 2 )
PAng = Atanfull( PtX - SqX, PtY - SqY )
` Assume new positions for point
Local NX as Integer
Local NY as Integer
NX = SqX + ( Sin( PAng - Ang ) * Dist )
NY = SqY + ( Cos( PAng - Ang ) * Dist )
` Check if new point is inside unrotated square
If (NX >= SqX - (Size / 2))
If (NY >= SqY - (Size / 2))
If (NX <= SqX + (Size / 2))
If (NY <= SqY + (Size / 2))
In = 1
Endif
Endif
Endif
Endif
EndFunction In
` Rotate Square --
function RotateSquare( PosX as integer, PosY as integer, Ang as integer, Size as integer )
` Get distance to vertex
local distv as float
`distv = sqrt( Size * 2 )
distv = sqrt( (Size / 2) ^ 2 + (Size / 2) ^ 2 )
` Draw
line PosX + (sin( Ang + 45 ) * distv), PosY + (cos( Ang + 45 ) * distv), PosX + (sin( Ang + 135 ) * distv), PosY + (cos( Ang + 135 ) * distv)
line PosX + (sin( Ang + 135 ) * distv), PosY + (cos( Ang + 135 ) * distv), PosX + (sin( Ang + 225 ) * distv), PosY + (cos( Ang + 225 ) * distv)
line PosX + (sin( Ang + 225 ) * distv), PosY + (cos( Ang + 225 ) * distv), PosX + (sin( Ang + 315 ) * distv), PosY + (cos( Ang + 315 ) * distv)
line PosX + (sin( Ang + 315 ) * distv), PosY + (cos( Ang + 315 ) * distv), PosX + (sin( Ang + 45 ) * distv), PosY + (cos( Ang + 45 ) * distv)
endfunction
[Edit]
And in 3D:
` Setup
Sync On : Sync Rate 60
Set Display Mode 1440, 900, 32 : Hide Mouse
Backdrop On : Color Backdrop 0 : Set Ambient Light 60
Autocam Off : Set Camera Range 1, 0x7fffffff
Disable Escapekey
` Box
Make Object Cube 1, 500
Ghost Object On 1
Color Object 1, Rgb(0, 255, 0)
Set Object Ambience 1, rgb(0, 255, 0)
Set Object 1, 1, 0, 0
Set Object Smoothing 1, 100
` Player
Make Object Cube 2, 10
Position Object 2, 0, 0, -1000
` Main Loop
Repeat
` Object
XRotate Object 1, Wrapvalue( Object Angle X(1) + 0.5 )
` Control Player
Move Object 2, ( UpKey() - DownKey() ) * 3
`Turn Object Left 2, ( Leftkey() - Rightkey() ) * 3
YRotate Object 2, Wrapvalue( Object Angle Y(2) - (( Leftkey() - Rightkey() ) * 3) )
` Camera
ca# = curveangle( object angle y(2), ca#, 20.0 )
cx# = object position x(2) - ( sin(ca#) * 60 )
cy# = object position y(2) + 30
cz# = object position z(2) - ( cos(ca#) * 60 )
position camera cx#, cy#, cz#
point camera object position x(2), object position y(2), object position z(2)
` Check if in box
Text 10, 10, Str$( PointInBox( 0, 0, 0, Object Angle X(1), Object Angle Y(1), 500, Object Position X(2), Object Position Y(2), Object Position Z(2) ) )
` Update
Sync
` Exit of escapekey
Until Escapekey()
End
` Check if inside box
Function PointInBox( PosX as Float, PosY as Float, PosZ as Float, AngX as Float, AngY as Float, Size as Float, PtX as Float, PtY as Float, PtZ as Float )
` In box
Local In as Boolean
` Get Distance and angle between point and center of box
Local Dist as Float
Local PAngX as Float
Local PAngY as Float
Dist = Sqrt( (PtX - PosX) ^ 2 + (PtY - PosY) ^ 2 + (PtZ - PosZ) ^ 2 )
PAngX = Atanfull( PtY - PosY, PtZ - PosZ )
PAngY = Atanfull( PtX - PosX, PtZ - PosZ )
` New assumed positions
Local NX as Float
Local NY as Float
Local NZ as Float
NX = PosX + ( Sin( PAngY - AngY ) * Cos( PAngX - AngX ) * Dist )
NY = PosY - ( Sin( PAngX - AngX ) * Dist )
NZ = PosZ + ( Cos( PAngY - AngY ) * Cos( PAngX - AngX ) * Dist )
` Check if new positions are inside unrotated box
If ( (NX >= PosX - (Size / 2)) && (NX <= PosX + (Size / 2)) )
If ( (NY >= PosY - (Size / 2)) && (NY <= PosY + (Size / 2)) )
If ( (NZ >= PosZ - (Size / 2)) && (NZ <= PosZ + (Size / 2)) )
In = 1
Endif
Endif
Endif
EndFunction In
The way this works is, it calculates a new position for the point assuming the angle of your box is 0, then does a simple "If x > something and x < something" etc.
Easy, but.. thinking outside the box
"It's like floating a boat on a liquid that I don't know, but I'm quite happy to drink it if I'm thirsty enough" - Me being a good programmer but sucking at computers