Setting boundaries/collision points in Tao framework and C#

Posted on 2011-10-31
Last Modified: 2013-12-16
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

Question by:Kalicat
    LVL 12

    Expert Comment

    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.

    Author Comment

    I'm not exactly following your pattern of thought.  Is there another side of the box I'm not considering?
    LVL 12

    Accepted Solution

    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.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now