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

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5036
  • O Rly?
    • View Profile
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 <3 DGArray's :D

PC:
AMD Ryzen 7 1700 @3.9GHz, 16GB HyperX Fury 3000MHz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4158
  • On the shoulders of giants.
    • View Profile
    • My Apps
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.

 :good:
I came. I saw. I played.

Offline SnooPI

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

Offline erico

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

Offline UBERmonkeybot

  • Mr. Polyvector
  • ***
  • Posts: 243
    • View Profile
Re: Mousepickup a cube in a array
« Reply #4 on: 2016-Feb-12 »
That's great i was scratching my head about ray collisions.


Thanks

 =D

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5036
  • O Rly?
    • View Profile
Re: Mousepickup a cube in a array
« Reply #5 on: 2016-Feb-15 »
No problem, hope it helps :)
I <3 DGArray's :D

PC:
AMD Ryzen 7 1700 @3.9GHz, 16GB HyperX Fury 3000MHz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard