Main sections
X_COLLISIONRAY()
scal# = X_COLLISIONRAY(num#, frame#, x#, y#, z#, dirx#, diry#, dirz#)
Checks if the 3D object num# collides with a ray through x#,y#,z# heading in direction dir#.
If the object and ray collide, scal# will be the distance from point x#,y#,z# heading in direction dir#.
If the object and ray do not collide then scal# = 0.
scal# < 0 will only be returned if no collision in positive direction is found.
If there is a collision in both the positive and negative directions then the distance to the positive result will be returned, regardless of which collision is closer.
In the debug-mode lines will be drawn:
- green = collision, scal# > 0
- yellow = collision, scal# < 0 (backwards)
- red = no collision at all
// --------------------------------- //
// Project:
// Start: Saturday, July 20, 2002
// First we need an object. Here: a simple paper plane
// We assume Z is down, as most 3d programs will export
// Also we assume the x axis (positive) is the direction
// the plane will fly forwards, so in a right hand system
// y will be left side of plane.
// Triangle Strips will be drawn:
//
// 1---2---5
// \ / \ /|
// 3---4 |
// \|
// 6
LOADSPRITE "News.bmp", 0
col = RGB(0xff, 0xff, 0xff)
X_OBJSTART 0 // PlaneID = 0
// Right wing: side updown
X_OBJADDVERTEX -7, 5, 0, 1.0, 1.0, col // Outer point
X_OBJADDVERTEX -7, 1, 0, 1.0, 0.5, col // Inner edge
X_OBJADDVERTEX 7, 0, 0, 0.0, 1.0, col // Top
// Right body:
X_OBJADDVERTEX -7, 0, 3, 0.0, 1.0, col
X_OBJNEWGROUP
// Left wing:
X_OBJADDVERTEX -7, -5, 0, 0.0, 0.0, col
X_OBJADDVERTEX -7, -1, 0, 0.0, 0.5, col
X_OBJADDVERTEX 7, 0, 0, 1.0, 0.0, col
// Left body:
X_OBJADDVERTEX -7, 0, 3, 0.0, 1.0, col
X_OBJEND
X_SAVEOBJ "plane.ddd", 0
X_LOADOBJ "plane.ddd", 0
LIMITFPS -1
WHILE TRUE
MOUSESTATE mx, my, mb1, mb2
DRAWRECT mx-8,my-8,16,16, RGB(255,0,0)
X_MAKE3D 1, 1000, 45 // Turn to 3D Viewport
X_CAMERA 200*SIN(mx*360/640), my*2-100, 200*COS(mx), 0,0,0 // Rotate CAMERA with mouse
dtime = GETTIMER()
psi=psi+(dtime/64); IF psi>=360 THEN psi=0
FOR phi = 0 TO 360 STEP 30
X_DRAWAXES 0,0,0 // Draw global axes (red=x, green=y, blue=z)
alpha = (phi+psi)*4
LOCAL px=200*SIN(phi+psi),py=SIN(alpha)*20,pz=100*COS(phi+psi)
X_MOVEMENT px,py,pz
X_SCALING 2,2,2
X_ROTATION 90,1,0,0 // Make plane body point down global y
X_ROTATION 45*COS(alpha),0,0,1 // make plane look up/down with mouse
X_ROTATION phi+psi, 0,1,0 // Rtoation due to position in circle
X_SETTEXTURE 0, -1
X_DRAWOBJ 0, 0 // Draw Object#0. Frame# is always 0 with user def. objects
// PICKING PART!
LOCAL x,y,z, x2,y2,z2
X_SCREEN2WORLD mx, my, 0, x, y, z
X_SCREEN2WORLD mx, my, -1, x2,y2,z2
IF X_COLLISIONRAY(0,0, x,y,z, x2-x, y2-y, z2-z)<>0
X_PRINT "PICK", px,py,pz, 0
ENDIF
NEXT
fps = ((1000/dtime)+fps)/2
delay=delay+dtime
IF delay>1000 // 1/2 sec
delay=0
showfps=fps
ENDIF
X_MAKE2D
PRINT "FPS: " +INTEGER(showfps), 100, 100 // + dtime, 0,0
SHOWSCREEN
WEND