Setting boundaries/collision points in Tao framework and C#

Good evening,

I'm attempting to create a simple screen saver of sorts using Tao framework and C#.  I've managed to create the square, have it rotate on its origin point as well as translate it.  The issue I'm having is that I'm not able to make it detect the window's boundaries.  The square eventually floats past the edge of the screen and disappears.

Line 27: holds the variables that indicate the max value for both x and y.

I tried placing the boundary check over in line 49, however the square continued to drift outside the window.

Lines 60-63 show what a friend suggested.  However this has also proved unsuccessful.
I would like to keep the square bouncing inside the window, while I understand in theory how it should work its translating it to the code that's giving me issues.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tao.OpenGl;
using Tao.Platform;

namespace Shooting_Stars
    public partial class Form1 : Form
        int width = 800;
        int height = 600;
        double xrot, yrot, zrot; //rotate;
        float xtrans, ytrans;
        int xmax = 786, ymax = 562;

        public Form1()

            Gl.glClearColor(0, 0, 0, 0);


            Gl.glViewport(0, 0, width, height);

            Glu.gluPerspective(45.0f, (double)width / (double)height, 0.01f, 5000.0f);

        private void timer1_Tick(object sender, EventArgs e)
            xtrans += 0.5f;
            ytrans += 0.5f;


        private void simpleOpenGlControl1_Paint(object sender, PaintEventArgs e)
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);


            if (xtrans >= xmax - 2 || ytrans >= ymax - 2)
                Gl.glTranslatef(0, 0, -10);
                Gl.glTranslatef(xtrans, ytrans, -10);

            Gl.glRotated(xrot += 0.5, 1, 0, 0);
            Gl.glRotated(yrot += 0.3, 0, 1, 1);
            Gl.glRotated(zrot += 0.2, 0, 0, 1);


            Gl.glColor3d(1, 0, 0); //red
            Gl.glVertex3d(-1, 1, 0);
            Gl.glColor3d(0, 0.8, 0); // green
            Gl.glVertex3d(-1, -1, 0);
            Gl.glColor3d(0, 0, 0.5); // blue
            Gl.glVertex3d(1, -1, 0);
            Gl.glColor3d(1, 1, 1); // white
            Gl.glVertex3d(1, 1, 0);


Open in new window

Who is Participating?
satsumoSoftware DeveloperCommented:
Yup, the sides where xtrans and ytrans are less than zero.  The view has both left and right, top and bottom edges.  Given that your'e using a perspective view, zero is actually the center of the screen.  

You could use this code

if (xtrans >= xmax - 2 || ytrans >= ymax - 2 ||  xtrans <= -xmax + 2 || ytrans <= -ymax + 2)

Open in new window

That would catch the other sides of the box, however xmax and ymax won't be the correct values because of the perspective.  It depends how far the bouncing box is from the camera.

It might help you to understand the problem if you try rendering the sides of the box that you are detecting against.
satsumoSoftware DeveloperCommented:
Your using a perspective projection so the edges of the view are a frustum rather than a simple box.  You could either use an orthographic projection and collide with the box, or detect collision with the frustum by calculating and colliding with its clip planes.

if (xtrans >= xmax - 2 || ytrans >= ymax - 2)

Open in new window

This code checks for collision with only two sides of a box, and that box does not relate to the size of the view.
KalicatAuthor Commented:
I'm not exactly following your pattern of thought.  Is there another side of the box I'm not considering?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.