Cube In Screen

Started by
4 comments, last by blanky 18 years, 7 months ago
Hey guys, I'm making a game in which I want a cube to randomly move around the screen, however I have a problem with it leaving the screen. I'd like to know if there's a way to know how many units are on the screen, from left to right and bottom to top, maybe my way is a bit too corny, is there a better way to do this? I think it borders on collision detection, thanks guys!
Advertisement
Yes, it's sort of collision detection. I'm guessing that you're using perspective projection, i.e. gluPerspective() or glFrustum(). Does your cube rotate as it moves, or just translate? Does it move toward and away from the camera, or just side to side and up and down? Does your camera move or remain stationary? In any case, it sounds like you want to basically restrict its motion to within the camera frustum. The specifics of how to do that depend at least in part on the answers to the preceding questions.
I'd do this:


Restrict cube movement in an area; 0 to EdgeX, 0 to EdgeY, 0 to EdgeZ etc...

Then you simply set up the camera so that the cube is always visible, or simply track the cubes movement...


if you want you could draw some quads where the borders are, and you're all set! :D
"Game Maker For Life, probably never professional thou." =)
Thanks guys. I translated (the camera?) -6 units back, that's all I did (no rotation or anything) the cube moves up down left right, and yes is perspective projection. I guess I could do what the second guy said, but could you please elaborate on this? I'm not quite sure on how to do it, and when I move the cube, say, up, I add the size of the cube to the y position and then when it goes back to the render function it draws the cube at that position, thanks again guys!
It seems like you could find the bounds of the frustum in the plane that the cube moves, and then restrict the cube to those bounds. To find those bounds, you need the field of view, the aspect ratio, and a little trig. Or, you could use glFrustum() to set up the frustum directly.

Maybe there's a simpler way of doing it that I'm not thinking of, though...
If I made a screen boundary, a square using lines, it would be easier to limit the cube within that square right? Thanks.

This topic is closed to new replies.

Advertisement