1

I'm doing 3d maze in opengl, 2d like collisions

Map is defined like 2dim array

 ---------x axis
|0 0 0
|0 1 0
|0 0 0
|
|z axis

that 1 is a cube

I divide that cube to 4 cubes

 .
 _ _
|_|_|
|_|_|

dot is my camera point with x, z coords now I go down to my cube. posX,Z is calculated like this but its not important, but so you know how it works, here

posZ -= (float) ((Math.cos(azimut * Math.PI / 180) * Math.cos(zenit * Math.PI / 180))) / 50;
posX += (float) ((Math.sin(azimut * Math.PI / 180) * Math.cos(zenit * Math.PI / 180))) / 50;

Its calculated every milisec or sth in display() method.. I cast it to int so i have the right coord for my array and save it to newX,Z

newposZ = (int) posZ;
newposX = (int) posX;

if (maze[newposZ][newposX] == 1) {

I check by azimuth where from I'm coming.. like this (this is the dot position in pic above going in top left cube azimuth is 180 if facing wall straight but i wanna check from 90 to 270..)

if (posX - newposX < 0.5 && posZ - newposZ < 0.5 && (azimut > 90 && azimut < 270) {
    posZ = newposZ;
}

And yea I change posZ which is lets say 1.1 to casted newposZ which is say 1 so everything is all right Camera hits the wall, but that azimuth checking is a bad idea, because in next step where I check coming to same minicube, but from left like this

if (posX - newposX < 0.5 && posZ - newposZ < 0.5 && (azimut > 0 && azimut > 180) {
    posX = newposX;
}

It's bad because at some angle (angle 90 to 180 because its same for both..) it sets both posX and posZ in the same time to newposX, newposZ.. one of that variables is fixed right but other shouldn't be if you know what I mean

It works ok if I check only from -45 to 45degree but i wanna from -90 do +90 anyone can help?

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
woko55
  • 11
  • 1
  • Easier way: if your start position is ok, and your new position is inside your cube, you've collided. The angle at which you encounter the cube boundary is only important for reflection/ bounce, which gets complicated. – 3Dave Apr 28 '13 at 21:46
  • but if I dont't use azimuth at all I dunno where from I'm coming in so if I'm coming lets say from dot do to left one both posX and Z changes to 1 and it moves me to corner [1,1] but it should fix just one variable, I should be in point like [1, 1.1] or sth.. – woko55 Apr 28 '13 at 22:04

0 Answers0