PDA

View Full Version : Help Collision Detection between forms



bruno_rs
07-01-2017, 10:25 AM
Hey guys. I need some help.

I need to implement a collision between 2 objects (quads). I have no idea how to do this.

My source code:



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

namespace SharpGLWinformsApplication6
{
public partial class SharpGLForm : Form
{

public SharpGLForm()
{
InitializeComponent();
}

public double dx = 0;
public double dy = 0;
public float angulo = 0;
public double radiano = 0;

private void openGLControl_OpenGLDraw(object sender, RenderEventArgs e)
{
// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;

// Clear the color and depth buffer.
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

// Load the identity matrix.
gl.LoadIdentity();

gl.PushMatrix();

// Draw
gl.Translate(dx, dy, 0.0f);
gl.Translate(20, 10, 0);
gl.Rotate(angulo, 0, 0, 1);
gl.Translate(-20, -10, 0);


gl.Begin(OpenGL.GL_QUADS);
gl.Color(1.0f, 1.0f, 1.0f);
gl.Vertex(20.0f, 20.0f);
gl.Vertex(10.0f, 20.0f);
gl.Vertex(10.0f, 10.0f);
gl.Vertex(20.0f, 10.0f);
gl.End();

gl.PopMatrix();

gl.Begin(OpenGL.GL_QUADS);
gl.Color(0.0f, 1.0f, 0.0f);
gl.Vertex(90.0f, 90.0f);
gl.Vertex(50.0f, 90.0f);
gl.Vertex(50.0f, 50.0f);
gl.Vertex(90.0f, 50.0f);
gl.End();
}


private void openGLControl_OpenGLInitialized(object sender, EventArgs e)
{
// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;

// Set the clear color.
gl.ClearColor(0, 0, 0, 0);
}

private void openGLControl_Resized(object sender, EventArgs e)
{

// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;

// Set the projection matrix.
gl.MatrixMode(OpenGL.GL_PROJECTION);

// Load the identity.
gl.LoadIdentity();

gl.Ortho2D(0, 100, 0, 100);

// Set the modelview matrix.
gl.MatrixMode(OpenGL.GL_MODELVIEW);
}


private void SharpGLForm_KeyDown(object sender, KeyEventArgs e)
{

if (e.KeyCode == Keys.D)
{
angulo--;
}

if (e.KeyCode == Keys.A)
{
angulo++;
}

if (e.KeyCode == Keys.W)
{
MovimentoFrente();
}

if (e.KeyCode == Keys.S)
{
MovimentoTras();
}


}


private void MovimentoFrente()
{
radiano = Math.PI * angulo / 180;
dx += Math.Cos(radiano);
dy += Math.Sin(radiano);

//MessageBox.Show(dx.ToString());
if (dx >= 79)
{
dx = 79;
}

if (dy >= 89)
{
dy = 89;
}

if (dx <= -19)
{
dx = -19;
}

if (dy <= -9)
{
dy = -9;
}
}

private void MovimentoTras()
{
radiano = Math.PI * angulo / 180;
dx -= Math.Cos(radiano);
dy -= Math.Sin(radiano);

if (dx >= 69)
{
dx = 69;
}

if (dy >= 79)
{
dy = 79;
}

if (dx <= -9)
{
dx = -9;
}

if (dy <= 1)
{
dy = 1;
}

}

}
}



Thanks!