Jump to content
  • Advertisement
Sign in to follow this  
blanky

Cube In Screen

This topic is 4745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
If I made a screen boundary, a square using lines, it would be easier to limit the cube within that square right? Thanks.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!