BASIC

Author Topic: 3D Camera Collision Problem  (Read 1642 times)

Offline a6n0rma1

  • Mc. Print
  • *
  • Posts: 5
    • View Profile
    • Tenebra studios' homepage
3D Camera Collision Problem
« on: 2013-Jun-13 »
I'm making a silly little game, based on the 3dmaze sample. It creates a random maze, populates it with sprites and 3dmodels and let's you walk through the dungeon. I changed the deafult camera to enable moving with w,a,s,d and mouse looking on x and y axes. I added kanonet's super lighting lib and his quick math library. Everything is working fine, except i can't make a proper camera collision.

Code: GLBasic [Select]
x_old = Cam_x//store current x camera value
z_old = Cam_z//store current z camera value

IF X_COLLISION(0,0,sz/4,Cam_x,sz/2,Cam_z)//check if the camera collides with any of the walls. walls have id 0
        Cam_z = z_old
        Cam_x = x_old
        collision = 1
ELSE
        collision = 0
ENDIF

IF KEY(17)// forward 'w'
                INC Cam_z,qSIN(phi_xz)*Cam_Speed
                INC Cam_x,qCOS(phi_xz)*Cam_Speed
        ENDIF
        IF KEY(31)// back 's'
                DEC Cam_z,qSIN(phi_xz)*Cam_Speed
                DEC Cam_x,qCOS(phi_xz)*Cam_Speed
        ENDIF
        IF KEY(30)// left 'a'
                INC Cam_z,qSIN(phi_xz-90)*(Cam_Speed-0.2)
                INC Cam_x,qCOS(phi_xz-90)*(Cam_Speed-0.2)
        ENDIF
        IF KEY(32)// right 'd'
                INC Cam_z,qSIN(phi_xz+90)*(Cam_Speed-0.2)
                INC Cam_x,qCOS(phi_xz+90)*(Cam_Speed-0.2)
        ENDIF
 

I then print the collision value. Whenever i am close to a wall it get's to 1 as it should, but Cam_x and Cam_z don't get constrained to the values they had before the collision occured and i simply pass through the wall. I must be doing something wrong, but i can't figure out what. Any ideas/help would be appreciated. I tried using the 3D entity system for collision checking. I created a sphere and placed it to where the camera is. I am doing an EntityCollisionSlide(ball, 0, Cam_x, Cam_y, Cam_z) and then Cam_z = z_old  and   Cam_x = x_old, but still to no avail. The camera, keeps passing through the walls.
« Last Edit: 2013-Jun-13 by a6n0rma1 »
I am rubber...Your are glue...
Oh! That is so cliche...
Do i look THAT much like your fiancee?
No! You look like a monkey in a negligee...
Hand over yer treasure!

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: 3D Camera Collision Problem
« Reply #1 on: 2013-Jun-14 »
Nice to hear that my libs are useful for you (even when the lighting things is more a tech demo then a lib and I hope it wont be needed in future). Hope you show us your game when its ready.

Your problem here is very simple, assuming this part of code is really exactly how you use it in your game. You set x_old and y_old to the new positions, before you check for collision, cause of that, when you find a collision _old and Cam_ are already identical, so you can not set Cam_ back to _old.
Solution:
Code: GLBasic [Select]
IF X_COLLISION(0,0,sz/4,Cam_x,sz/2,Cam_z)//check if the camera collides with any of the walls. walls have id 0
        Cam_z = z_old
        Cam_x = x_old
        collision = 1
ELSE
        collision = 0
        x_old = Cam_x//store current x camera value
        z_old = Cam_z//store current z camera value
ENDIF
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline a6n0rma1

  • Mc. Print
  • *
  • Posts: 5
    • View Profile
    • Tenebra studios' homepage
Re: 3D Camera Collision Problem
« Reply #2 on: 2013-Jun-14 »
It worked! I feel so retarded now. I tried everything except from the simplest and most evident solution! Thanks a ton.
I am rubber...Your are glue...
Oh! That is so cliche...
Do i look THAT much like your fiancee?
No! You look like a monkey in a negligee...
Hand over yer treasure!