### Author Topic: Mousepickup a cube in a array  (Read 5926 times)

#### Schranz0r

• Prof. Inline
• Posts: 5116
• O Rly?
##### Mousepickup a cube in a array
« on: 2014-Apr-21 »
Hi,

this is a litte snippet on how to mousepick a cube from a array of cubes.
Some basic "Minecraft-Style"  picking.

TAGS(for the searchmaschine) :
mousepick, pick, picking, minecraft, 3D-picking, collisionray, ray

Code: (glbasic) [Select]
// --------------------------------- //
// Project: Picktest
// Start: Monday, April 21, 2014
// IDE Version: 12.096

// use systempointer
SYSTEMPOINTER TRUE

GLOBAL array[]
REDIM array[3][3][3]

// normal Cube
CreateCube(1,1,RGB(0x00, 0xff, 0x00))

// debug cube ( RED )
CreateCube(2,1.01,RGB(0xff, 0x00, 0x00))

// fill up
FOR x = 0 TO 2
FOR y = 0 TO 2
FOR z = 0 TO 2

array[x][y][z] = 1

NEXT
NEXT
NEXT

WHILE TRUE
LOCAL mx,my,b1,b2
LOCAL wx1#,wy1#,wz1#, wx2#,wy2#,wz2# // screen2world variables
LOCAL cx%,cy%,cz% // collision-position in array
LOCAL closest_coll# = 100 // set max coll distance
LOCAL coll // collision variable
MOUSESTATE mx,my,b1,b2

// make the window 3D and set up the Cam, position of the cam doesen't matter
X_MAKE3D 1,1000,45
X_CAMERA 5,5,-5, 0.7,0,1.5

// 2 times screen2world, second is Z = 1 to set the direction !
X_SCREEN2WORLD mx,my,0, wx1, wy1, wz1
X_SCREEN2WORLD mx,my,1, wx2, wy2, wz2

FOR x = 0 TO 2
FOR y = 0 TO 2
FOR z = 0 TO 2

IF array[x][y][z]

// move cube and draw
X_MOVEMENT x, y, z
X_DRAWOBJ 1, 0

// check coll
coll = X_COLLISIONRAY(1,0, wx1,wy1,wz1, wx2,wy2,wz2) // shoot up the RAY
IF coll
IF closest_coll > coll  // is closest coll larger then coll then set as closest
closest_coll = coll

// save position in array
cx = x
cy = y
cz = z
ENDIF

ENDIF

ENDIF

NEXT
NEXT
NEXT

// draw the debugcube
IF closest_coll < 100
X_MOVEMENT cx, cy, cz
X_DRAWOBJ 2, 0
ENDIF

// some 2D stuff
X_MAKE2D

PRINT  "X: "+mx+" Y: "+my,10,10
PRINT closest_coll,10,20

SHOWSCREEN
WEND
END

// ------------------------------------------------------------- //
// -=#  CREATECUBE  #=-
// -= by KittyHello =-
// ------------------------------------------------------------- //
FUNCTION CreateCube: num, sz, col
// Diese Variablen sind als LOCAL definiert:
// num, sz,
X_AUTONORMALS 1 // For a cube, hard edges
sz=sz/2
X_OBJSTART num
// Front Face
X_OBJADDVERTEX  sz, -sz,  sz, 1, 0, col
X_OBJADDVERTEX -sz, -sz,  sz, 0, 0, col
X_OBJADDVERTEX  sz,  sz,  sz, 1, 1, col
X_OBJADDVERTEX -sz,  sz,  sz, 0, 1, col
X_OBJNEWGROUP
// Back Face
X_OBJADDVERTEX -sz,  sz, -sz, 1, 1, col
X_OBJADDVERTEX -sz, -sz, -sz, 1, 0, col
X_OBJADDVERTEX  sz,  sz, -sz, 0, 1, col
X_OBJADDVERTEX  sz, -sz, -sz, 0, 0, col
X_OBJNEWGROUP
// Top Face
X_OBJADDVERTEX -sz,  sz,  sz, 0, 0, col
X_OBJADDVERTEX -sz,  sz, -sz, 0, 1, col
X_OBJADDVERTEX  sz,  sz,  sz, 1, 0, col
X_OBJADDVERTEX  sz,  sz, -sz, 1, 1, col
X_OBJNEWGROUP
// Bottom Face
X_OBJADDVERTEX  sz, -sz, -sz, 0, 1, col
X_OBJADDVERTEX -sz, -sz, -sz, 1, 1, col
X_OBJADDVERTEX  sz, -sz,  sz, 0, 0, col
X_OBJADDVERTEX -sz, -sz,  sz, 1, 0, col
X_OBJNEWGROUP
// Right face
X_OBJADDVERTEX  sz,  sz, -sz, 1, 1, col
X_OBJADDVERTEX  sz, -sz, -sz, 1, 0, col
X_OBJADDVERTEX  sz,  sz,  sz, 0, 1, col
X_OBJADDVERTEX  sz, -sz,  sz, 0, 0, col
X_OBJNEWGROUP
// Left Face
X_OBJADDVERTEX -sz, -sz,  sz, 1, 0, col
X_OBJADDVERTEX -sz, -sz, -sz, 0, 0, col
X_OBJADDVERTEX -sz,  sz,  sz, 1, 1, col
X_OBJADDVERTEX -sz,  sz, -sz, 0, 1, col
X_OBJNEWGROUP
X_OBJEND

ENDFUNCTION // sz
« Last Edit: 2014-Apr-21 by Schranz0r »
I DGArray's

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

#### Ian Price

• Prof. Inline
• Posts: 4176
• On the shoulders of giants.
##### Re: Mousepickup a cube in a array
« Reply #1 on: 2014-Apr-21 »
That's pretty cool and something I'd have needed help if I ever do anything 3D, as I'd love to re-create 3D Picross on the Nintendo DS.

I came. I saw. I played.

#### SnooPI

• Dr. Type
• Posts: 421
##### Re: Mousepickup a cube in a array
« Reply #2 on: 2014-Apr-21 »
interesting for a 3d map editor with 3D tiles

#### erico

• Community Developer
• Prof. Inline
• Posts: 4450
##### Re: Mousepickup a cube in a array
« Reply #3 on: 2014-Apr-22 »
Great Rubick stuff!

#### UBERmonkeybot

• Mr. Polyvector
• Posts: 243
##### Re: Mousepickup a cube in a array
« Reply #4 on: 2016-Feb-12 »

Thanks