?
Solved

Setting boundaries/collision points in Tao framework and C#

Posted on 2011-10-31
3
Medium Priority
?
618 Views
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()
        {
            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

0
Comment
Question by:Kalicat
  • 2
3 Comments
 
LVL 12

Expert Comment

by:satsumo
ID: 37060377
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.
0
 

Author Comment

by:Kalicat
ID: 37060416
I'm not exactly following your pattern of thought.  Is there another side of the box I'm not considering?
0
 
LVL 12

Accepted Solution

by:
satsumo earned 2000 total points
ID: 37060431
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.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses

839 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