Link to home
Start Free TrialLog in
Avatar of Kalicat
Kalicat

asked on

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()
        {
            InitializeComponent();
            this.simpleOpenGlControl1.InitializeContexts();

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

            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glDepthMask(Gl.GL_TRUE);

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

            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Glu.gluPerspective(45.0f, (double)width / (double)height, 0.01f, 5000.0f);
            Gl.glEnable(Gl.GL_CULL_FACE);
            Gl.glCullFace(Gl.GL_BACK);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            xrot++;//rotate++;
            yrot++;
            zrot++;
            xtrans += 0.5f;
            ytrans += 0.5f;

            this.simpleOpenGlControl1.Invalidate();
        }

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

            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();

            if (xtrans >= xmax - 2 || ytrans >= ymax - 2)
                Gl.glTranslatef(0, 0, -10);
            else 
                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.glBegin(Gl.GL_LINE_LOOP);

            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);

            Gl.glEnd();
        }
    }
}

Open in new window

Avatar of Member_2_5069294
Member_2_5069294

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.
Avatar of Kalicat

ASKER

I'm not exactly following your pattern of thought.  Is there another side of the box I'm not considering?
ASKER CERTIFIED SOLUTION
Avatar of Member_2_5069294
Member_2_5069294

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial