473,322 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Hit testing lines are not working!

Hello all..

Am working on an Air Hockey game... have an table loaded into a
picture box. The borders of the table are slightly slanted. Am using
hit testing lines with GDI+ to manipulate the puck moving around. I
want the puck is bounce when it hits a border (specified by the
hitlines).

Retreieved some info on hit testing lines from Bob Powell's GDI+ FAQ
(very useful!) but i'm fairly new at the idea of hit testing and am
wondering what exactly is wrong with this code.

Sorry about the abundance of code but my code isn't exactly safe
enough to post a sample (so basically i'm saying, it's fairly badly
written code). I thought it might be easier if people just tried to
run this code themselves and let me know of the results.

Anu suggestions will be much appreciated!

Thanks in advance!
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AirHockey
{
/// <summary>
/// Summary description for Table.
/// </summary>
public class frmTable : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox picTable;
private System.Windows.Forms.Label lblPlayer2Score;
private System.Windows.Forms.Label lblPlayer1Score;
private System.Windows.Forms.Label lblPlayer2Name;
private System.Windows.Forms.Label lblPlayer1Name;
Timer gameClock;
int puck_x;
int puck_y;
int x_vel = -5;
int y_vel = -5;

private LineArray Lines=new LineArray();
Line lineLeft;
Line lineRight;
Line lineTopLeft;
Line lineTopRight;
Line lineBottomLeft;
Line lineBottomRight;
bool hitLinesDrawn = false;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public frmTable()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
///

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(frmTable)) ;
this.picTable = new System.Windows.Forms.PictureBox();
this.lblPlayer2Score = new System.Windows.Forms.Label();
this.lblPlayer1Score = new System.Windows.Forms.Label();
this.lblPlayer2Name = new System.Windows.Forms.Label();
this.lblPlayer1Name = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// picTable
//
this.picTable.BackColor = System.Drawing.Color.Transparent;
this.picTable.Image =
((System.Drawing.Image)(resources.GetObject("picTa ble.Image")));
this.picTable.Location = new System.Drawing.Point(64, -24);
this.picTable.Name = "picTable";
this.picTable.Size = new System.Drawing.Size(848, 720);
this.picTable.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.CenterImag e;
this.picTable.TabIndex = 0;
this.picTable.TabStop = false;
this.picTable.Paint += new
System.Windows.Forms.PaintEventHandler(this.picTab le_Paint);
this.picTable.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTab le_MouseMove);
//
// lblPlayer2Score
//
this.lblPlayer2Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer2Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer2Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer2Score.Cursor = System.Windows.Forms.Cursors.Default;
this.lblPlayer2Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer2Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer2Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer2Score.Location = new System.Drawing.Point(16, 56);
this.lblPlayer2Score.Name = "lblPlayer2Score";
this.lblPlayer2Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer2Score.TabIndex = 1;
this.lblPlayer2Score.Text = "00";
this.lblPlayer2Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Score
//
this.lblPlayer1Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer1Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer1Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer1Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer1Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer1Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer1Score.Location = new System.Drawing.Point(816, 544);
this.lblPlayer1Score.Name = "lblPlayer1Score";
this.lblPlayer1Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer1Score.TabIndex = 2;
this.lblPlayer1Score.Text = "00";
this.lblPlayer1Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer2Name
//
this.lblPlayer2Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer2Name.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.lblPlayer2Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer2Name.Location = new System.Drawing.Point(16, 24);
this.lblPlayer2Name.Name = "lblPlayer2Name";
this.lblPlayer2Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer2Name.TabIndex = 3;
this.lblPlayer2Name.Text = "Player2Name";
this.lblPlayer2Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Name
//
this.lblPlayer1Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer1Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer1Name.Location = new System.Drawing.Point(816, 512);
this.lblPlayer1Name.Name = "lblPlayer1Name";
this.lblPlayer1Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer1Name.TabIndex = 4;
this.lblPlayer1Name.Text = "Player1Name";
this.lblPlayer1Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.TabIndex = 5;
this.label1.Text = "label1";
//
// frmTable
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackgroundImage =
((System.Drawing.Image)(resources.GetObject("$this .BackgroundImage")));
this.ClientSize = new System.Drawing.Size(973, 647);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblPlayer1Name);
this.Controls.Add(this.lblPlayer2Name);
this.Controls.Add(this.lblPlayer1Score);
this.Controls.Add(this.lblPlayer2Score);
this.Controls.Add(this.picTable);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "frmTable";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScree n;
this.Text = "Air Hockey";
this.Load += new System.EventHandler(this.frmTable_Load);
this.Closed += new System.EventHandler(this.frmTable_Closed);
this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTab le_MouseMove);
this.ResumeLayout(false);

}
#endregion

private void frmTable_Closed(object sender, System.EventArgs e)
{
frmMainMenu MainMenu = new frmMainMenu();
MainMenu.Show();
}

private void gameLoop(Object sender, EventArgs e)
{
puck_x += x_vel;
puck_y += y_vel;

//Called so the form will repaint itself.
Invalidate();
}

private void gameDraw(Graphics g)
{
// image of puck is loaded and drawn
Image image = Image.FromFile("C:/Documents and Settings/All
Users.WINDOWS/Documents/Brian's Documents/AirHockey/Puck.png");
g.DrawImage(image, puck_x, puck_y, 35, 35);

foreach(Line l in Lines)
{
l.DrawLine(g, Color.FromArgb(192,192,192));
}
}

public void setName(string pName)
{
lblPlayer1Name.Text = pName;
}

private void frmTable_Load(object sender, System.EventArgs e)
{
gameClock = new Timer();

//Start the game clock to control game speed
gameClock.Interval = 15;
gameClock.Tick += new EventHandler(gameLoop);
gameClock.Start();

puck_x = 200;
puck_y = 200;
}

private void picTable_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
label1.Text = e.X + " " + e.Y;

Graphics g=CreateGraphics();

if(hitLinesDrawn != true)
{
DrawHitLines(g);
}
else
{
foreach(Line l in Lines)
{
if(l.IsInLine(new Point(e.X,e.Y)))
l.DrawLine(g,Color.Red);
else
l.DrawLine(g,Color.Black);
}
}
}

private void picTable_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
gameDraw(e.Graphics);
}

private void DrawHitLines(Graphics g)
{
// set of hit lines for sides of table
lineLeft = new Line();
lineLeft.StartPoint=new Point( 164, 77 );
lineLeft.EndPoint=new Point( 84, 594 );
lineLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineLeft);

lineRight = new Line();
lineRight.StartPoint=new Point( 631, 77 );
lineRight.EndPoint=new Point( 712, 594 );
lineRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineRight);

// set of hit lines for top of table
lineTopLeft = new Line();
lineTopLeft.StartPoint=new Point( 166, 76 );
lineTopLeft.EndPoint=new Point( 340, 76 );
lineTopLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopLeft);

lineTopRight = new Line();
lineTopRight.StartPoint=new Point( 630, 76 );
lineTopRight.EndPoint=new Point( 455, 76 );
lineTopRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopRight);

// set of hit lines for top of table
lineBottomLeft = new Line();
lineBottomLeft.StartPoint=new Point( 86, 595 );
lineBottomLeft.EndPoint=new Point( 328, 595 );
lineBottomLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineBottomLeft);

lineBottomRight = new Line();
lineBottomRight.StartPoint=new Point( 711, 595 );
lineBottomRight.EndPoint=new Point( 467, 595 );
lineBottomRight.DrawLine(g, Color.Red);
Lines.Add(lineBottomRight);

hitLinesDrawn = true;
}
}

public class Line
{
public Point StartPoint;
public Point EndPoint;
public int PenWidth;

public void DrawLine(Graphics g,Color c)
{
Pen p=new Pen(c,PenWidth);
g.DrawLine(p,StartPoint,EndPoint);
p.Dispose();
}

public bool IsInLine(Point pnt)
{
Pen p = new Pen(Color.Black,PenWidth);
GraphicsPath pth=new GraphicsPath();
pth.AddLine(StartPoint,EndPoint);
pth.Widen(p);
p.Dispose();

if(pth.IsVisible(pnt))
{
return true;
}
else
{
return false;
}
}
}

public class LineArray : CollectionBase
{
public void Add(Line l)
{
List.Add(l);
}

public Line this[int index]
{
get
{
return (Line)List[index];
}
set
{
List[index]=value;
}
}
}
}
Nov 16 '05 #1
5 2702
Hello Brian.
The line hit-testing is working just fine. The problem lies in the fact that
you are drawing in the mousemove event and then immediately overwriting it
when the Paint comes along..

Now what did I tell my readers about using CreateGraphics??? ;-) I don't
beleive you fell foul of the #1 FAQ question!

You need to set a flag somewhere to show that the line has been detected and
then take notice of the flag when you redraw the table.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Brian" <br************@gmail.com> wrote in message
news:c2*************************@posting.google.co m...
Hello all..

Am working on an Air Hockey game... have an table loaded into a
picture box. The borders of the table are slightly slanted. Am using
hit testing lines with GDI+ to manipulate the puck moving around. I
want the puck is bounce when it hits a border (specified by the
hitlines).

Retreieved some info on hit testing lines from Bob Powell's GDI+ FAQ
(very useful!) but i'm fairly new at the idea of hit testing and am
wondering what exactly is wrong with this code.

Sorry about the abundance of code but my code isn't exactly safe
enough to post a sample (so basically i'm saying, it's fairly badly
written code). I thought it might be easier if people just tried to
run this code themselves and let me know of the results.

Anu suggestions will be much appreciated!

Thanks in advance!
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AirHockey
{
/// <summary>
/// Summary description for Table.
/// </summary>
public class frmTable : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox picTable;
private System.Windows.Forms.Label lblPlayer2Score;
private System.Windows.Forms.Label lblPlayer1Score;
private System.Windows.Forms.Label lblPlayer2Name;
private System.Windows.Forms.Label lblPlayer1Name;
Timer gameClock;
int puck_x;
int puck_y;
int x_vel = -5;
int y_vel = -5;

private LineArray Lines=new LineArray();
Line lineLeft;
Line lineRight;
Line lineTopLeft;
Line lineTopRight;
Line lineBottomLeft;
Line lineBottomRight;
bool hitLinesDrawn = false;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public frmTable()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
///

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(frmTable)) ;
this.picTable = new System.Windows.Forms.PictureBox();
this.lblPlayer2Score = new System.Windows.Forms.Label();
this.lblPlayer1Score = new System.Windows.Forms.Label();
this.lblPlayer2Name = new System.Windows.Forms.Label();
this.lblPlayer1Name = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// picTable
//
this.picTable.BackColor = System.Drawing.Color.Transparent;
this.picTable.Image =
((System.Drawing.Image)(resources.GetObject("picTa ble.Image")));
this.picTable.Location = new System.Drawing.Point(64, -24);
this.picTable.Name = "picTable";
this.picTable.Size = new System.Drawing.Size(848, 720);
this.picTable.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.CenterImag e;
this.picTable.TabIndex = 0;
this.picTable.TabStop = false;
this.picTable.Paint += new
System.Windows.Forms.PaintEventHandler(this.picTab le_Paint);
this.picTable.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTab le_MouseMove);
//
// lblPlayer2Score
//
this.lblPlayer2Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer2Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer2Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer2Score.Cursor = System.Windows.Forms.Cursors.Default;
this.lblPlayer2Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer2Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer2Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer2Score.Location = new System.Drawing.Point(16, 56);
this.lblPlayer2Score.Name = "lblPlayer2Score";
this.lblPlayer2Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer2Score.TabIndex = 1;
this.lblPlayer2Score.Text = "00";
this.lblPlayer2Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Score
//
this.lblPlayer1Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer1Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer1Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer1Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer1Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer1Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer1Score.Location = new System.Drawing.Point(816, 544);
this.lblPlayer1Score.Name = "lblPlayer1Score";
this.lblPlayer1Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer1Score.TabIndex = 2;
this.lblPlayer1Score.Text = "00";
this.lblPlayer1Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer2Name
//
this.lblPlayer2Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer2Name.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.lblPlayer2Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer2Name.Location = new System.Drawing.Point(16, 24);
this.lblPlayer2Name.Name = "lblPlayer2Name";
this.lblPlayer2Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer2Name.TabIndex = 3;
this.lblPlayer2Name.Text = "Player2Name";
this.lblPlayer2Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Name
//
this.lblPlayer1Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer1Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer1Name.Location = new System.Drawing.Point(816, 512);
this.lblPlayer1Name.Name = "lblPlayer1Name";
this.lblPlayer1Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer1Name.TabIndex = 4;
this.lblPlayer1Name.Text = "Player1Name";
this.lblPlayer1Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.TabIndex = 5;
this.label1.Text = "label1";
//
// frmTable
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackgroundImage =
((System.Drawing.Image)(resources.GetObject("$this .BackgroundImage")));
this.ClientSize = new System.Drawing.Size(973, 647);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblPlayer1Name);
this.Controls.Add(this.lblPlayer2Name);
this.Controls.Add(this.lblPlayer1Score);
this.Controls.Add(this.lblPlayer2Score);
this.Controls.Add(this.picTable);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "frmTable";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScree n;
this.Text = "Air Hockey";
this.Load += new System.EventHandler(this.frmTable_Load);
this.Closed += new System.EventHandler(this.frmTable_Closed);
this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTab le_MouseMove);
this.ResumeLayout(false);

}
#endregion

private void frmTable_Closed(object sender, System.EventArgs e)
{
frmMainMenu MainMenu = new frmMainMenu();
MainMenu.Show();
}

private void gameLoop(Object sender, EventArgs e)
{
puck_x += x_vel;
puck_y += y_vel;

//Called so the form will repaint itself.
Invalidate();
}

private void gameDraw(Graphics g)
{
// image of puck is loaded and drawn
Image image = Image.FromFile("C:/Documents and Settings/All
Users.WINDOWS/Documents/Brian's Documents/AirHockey/Puck.png");
g.DrawImage(image, puck_x, puck_y, 35, 35);

foreach(Line l in Lines)
{
l.DrawLine(g, Color.FromArgb(192,192,192));
}
}

public void setName(string pName)
{
lblPlayer1Name.Text = pName;
}

private void frmTable_Load(object sender, System.EventArgs e)
{
gameClock = new Timer();

//Start the game clock to control game speed
gameClock.Interval = 15;
gameClock.Tick += new EventHandler(gameLoop);
gameClock.Start();

puck_x = 200;
puck_y = 200;
}

private void picTable_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
label1.Text = e.X + " " + e.Y;

Graphics g=CreateGraphics();

if(hitLinesDrawn != true)
{
DrawHitLines(g);
}
else
{
foreach(Line l in Lines)
{
if(l.IsInLine(new Point(e.X,e.Y)))
l.DrawLine(g,Color.Red);
else
l.DrawLine(g,Color.Black);
}
}
}

private void picTable_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
gameDraw(e.Graphics);
}

private void DrawHitLines(Graphics g)
{
// set of hit lines for sides of table
lineLeft = new Line();
lineLeft.StartPoint=new Point( 164, 77 );
lineLeft.EndPoint=new Point( 84, 594 );
lineLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineLeft);

lineRight = new Line();
lineRight.StartPoint=new Point( 631, 77 );
lineRight.EndPoint=new Point( 712, 594 );
lineRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineRight);

// set of hit lines for top of table
lineTopLeft = new Line();
lineTopLeft.StartPoint=new Point( 166, 76 );
lineTopLeft.EndPoint=new Point( 340, 76 );
lineTopLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopLeft);

lineTopRight = new Line();
lineTopRight.StartPoint=new Point( 630, 76 );
lineTopRight.EndPoint=new Point( 455, 76 );
lineTopRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopRight);

// set of hit lines for top of table
lineBottomLeft = new Line();
lineBottomLeft.StartPoint=new Point( 86, 595 );
lineBottomLeft.EndPoint=new Point( 328, 595 );
lineBottomLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineBottomLeft);

lineBottomRight = new Line();
lineBottomRight.StartPoint=new Point( 711, 595 );
lineBottomRight.EndPoint=new Point( 467, 595 );
lineBottomRight.DrawLine(g, Color.Red);
Lines.Add(lineBottomRight);

hitLinesDrawn = true;
}
}

public class Line
{
public Point StartPoint;
public Point EndPoint;
public int PenWidth;

public void DrawLine(Graphics g,Color c)
{
Pen p=new Pen(c,PenWidth);
g.DrawLine(p,StartPoint,EndPoint);
p.Dispose();
}

public bool IsInLine(Point pnt)
{
Pen p = new Pen(Color.Black,PenWidth);
GraphicsPath pth=new GraphicsPath();
pth.AddLine(StartPoint,EndPoint);
pth.Widen(p);
p.Dispose();

if(pth.IsVisible(pnt))
{
return true;
}
else
{
return false;
}
}
}

public class LineArray : CollectionBase
{
public void Add(Line l)
{
List.Add(l);
}

public Line this[int index]
{
get
{
return (Line)List[index];
}
set
{
List[index]=value;
}
}
}
}

Nov 16 '05 #2
Hello Brian.
The line hit-testing is working just fine. The problem lies in the fact that
you are drawing in the mousemove event and then immediately overwriting it
when the Paint comes along..

Now what did I tell my readers about using CreateGraphics??? ;-) I don't
beleive you fell foul of the #1 FAQ question!

You need to set a flag somewhere to show that the line has been detected and
then take notice of the flag when you redraw the table.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Brian" <br************@gmail.com> wrote in message
news:c2*************************@posting.google.co m...
Hello all..

Am working on an Air Hockey game... have an table loaded into a
picture box. The borders of the table are slightly slanted. Am using
hit testing lines with GDI+ to manipulate the puck moving around. I
want the puck is bounce when it hits a border (specified by the
hitlines).

Retreieved some info on hit testing lines from Bob Powell's GDI+ FAQ
(very useful!) but i'm fairly new at the idea of hit testing and am
wondering what exactly is wrong with this code.

Sorry about the abundance of code but my code isn't exactly safe
enough to post a sample (so basically i'm saying, it's fairly badly
written code). I thought it might be easier if people just tried to
run this code themselves and let me know of the results.

Anu suggestions will be much appreciated!

Thanks in advance!
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AirHockey
{
/// <summary>
/// Summary description for Table.
/// </summary>
public class frmTable : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox picTable;
private System.Windows.Forms.Label lblPlayer2Score;
private System.Windows.Forms.Label lblPlayer1Score;
private System.Windows.Forms.Label lblPlayer2Name;
private System.Windows.Forms.Label lblPlayer1Name;
Timer gameClock;
int puck_x;
int puck_y;
int x_vel = -5;
int y_vel = -5;

private LineArray Lines=new LineArray();
Line lineLeft;
Line lineRight;
Line lineTopLeft;
Line lineTopRight;
Line lineBottomLeft;
Line lineBottomRight;
bool hitLinesDrawn = false;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public frmTable()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
///

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(frmTable)) ;
this.picTable = new System.Windows.Forms.PictureBox();
this.lblPlayer2Score = new System.Windows.Forms.Label();
this.lblPlayer1Score = new System.Windows.Forms.Label();
this.lblPlayer2Name = new System.Windows.Forms.Label();
this.lblPlayer1Name = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// picTable
//
this.picTable.BackColor = System.Drawing.Color.Transparent;
this.picTable.Image =
((System.Drawing.Image)(resources.GetObject("picTa ble.Image")));
this.picTable.Location = new System.Drawing.Point(64, -24);
this.picTable.Name = "picTable";
this.picTable.Size = new System.Drawing.Size(848, 720);
this.picTable.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.CenterImag e;
this.picTable.TabIndex = 0;
this.picTable.TabStop = false;
this.picTable.Paint += new
System.Windows.Forms.PaintEventHandler(this.picTab le_Paint);
this.picTable.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTab le_MouseMove);
//
// lblPlayer2Score
//
this.lblPlayer2Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer2Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer2Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer2Score.Cursor = System.Windows.Forms.Cursors.Default;
this.lblPlayer2Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer2Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer2Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer2Score.Location = new System.Drawing.Point(16, 56);
this.lblPlayer2Score.Name = "lblPlayer2Score";
this.lblPlayer2Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer2Score.TabIndex = 1;
this.lblPlayer2Score.Text = "00";
this.lblPlayer2Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Score
//
this.lblPlayer1Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer1Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer1Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer1Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer1Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer1Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer1Score.Location = new System.Drawing.Point(816, 544);
this.lblPlayer1Score.Name = "lblPlayer1Score";
this.lblPlayer1Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer1Score.TabIndex = 2;
this.lblPlayer1Score.Text = "00";
this.lblPlayer1Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer2Name
//
this.lblPlayer2Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer2Name.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.lblPlayer2Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer2Name.Location = new System.Drawing.Point(16, 24);
this.lblPlayer2Name.Name = "lblPlayer2Name";
this.lblPlayer2Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer2Name.TabIndex = 3;
this.lblPlayer2Name.Text = "Player2Name";
this.lblPlayer2Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Name
//
this.lblPlayer1Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer1Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer1Name.Location = new System.Drawing.Point(816, 512);
this.lblPlayer1Name.Name = "lblPlayer1Name";
this.lblPlayer1Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer1Name.TabIndex = 4;
this.lblPlayer1Name.Text = "Player1Name";
this.lblPlayer1Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.TabIndex = 5;
this.label1.Text = "label1";
//
// frmTable
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackgroundImage =
((System.Drawing.Image)(resources.GetObject("$this .BackgroundImage")));
this.ClientSize = new System.Drawing.Size(973, 647);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblPlayer1Name);
this.Controls.Add(this.lblPlayer2Name);
this.Controls.Add(this.lblPlayer1Score);
this.Controls.Add(this.lblPlayer2Score);
this.Controls.Add(this.picTable);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "frmTable";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScree n;
this.Text = "Air Hockey";
this.Load += new System.EventHandler(this.frmTable_Load);
this.Closed += new System.EventHandler(this.frmTable_Closed);
this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTab le_MouseMove);
this.ResumeLayout(false);

}
#endregion

private void frmTable_Closed(object sender, System.EventArgs e)
{
frmMainMenu MainMenu = new frmMainMenu();
MainMenu.Show();
}

private void gameLoop(Object sender, EventArgs e)
{
puck_x += x_vel;
puck_y += y_vel;

//Called so the form will repaint itself.
Invalidate();
}

private void gameDraw(Graphics g)
{
// image of puck is loaded and drawn
Image image = Image.FromFile("C:/Documents and Settings/All
Users.WINDOWS/Documents/Brian's Documents/AirHockey/Puck.png");
g.DrawImage(image, puck_x, puck_y, 35, 35);

foreach(Line l in Lines)
{
l.DrawLine(g, Color.FromArgb(192,192,192));
}
}

public void setName(string pName)
{
lblPlayer1Name.Text = pName;
}

private void frmTable_Load(object sender, System.EventArgs e)
{
gameClock = new Timer();

//Start the game clock to control game speed
gameClock.Interval = 15;
gameClock.Tick += new EventHandler(gameLoop);
gameClock.Start();

puck_x = 200;
puck_y = 200;
}

private void picTable_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
label1.Text = e.X + " " + e.Y;

Graphics g=CreateGraphics();

if(hitLinesDrawn != true)
{
DrawHitLines(g);
}
else
{
foreach(Line l in Lines)
{
if(l.IsInLine(new Point(e.X,e.Y)))
l.DrawLine(g,Color.Red);
else
l.DrawLine(g,Color.Black);
}
}
}

private void picTable_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
gameDraw(e.Graphics);
}

private void DrawHitLines(Graphics g)
{
// set of hit lines for sides of table
lineLeft = new Line();
lineLeft.StartPoint=new Point( 164, 77 );
lineLeft.EndPoint=new Point( 84, 594 );
lineLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineLeft);

lineRight = new Line();
lineRight.StartPoint=new Point( 631, 77 );
lineRight.EndPoint=new Point( 712, 594 );
lineRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineRight);

// set of hit lines for top of table
lineTopLeft = new Line();
lineTopLeft.StartPoint=new Point( 166, 76 );
lineTopLeft.EndPoint=new Point( 340, 76 );
lineTopLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopLeft);

lineTopRight = new Line();
lineTopRight.StartPoint=new Point( 630, 76 );
lineTopRight.EndPoint=new Point( 455, 76 );
lineTopRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopRight);

// set of hit lines for top of table
lineBottomLeft = new Line();
lineBottomLeft.StartPoint=new Point( 86, 595 );
lineBottomLeft.EndPoint=new Point( 328, 595 );
lineBottomLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineBottomLeft);

lineBottomRight = new Line();
lineBottomRight.StartPoint=new Point( 711, 595 );
lineBottomRight.EndPoint=new Point( 467, 595 );
lineBottomRight.DrawLine(g, Color.Red);
Lines.Add(lineBottomRight);

hitLinesDrawn = true;
}
}

public class Line
{
public Point StartPoint;
public Point EndPoint;
public int PenWidth;

public void DrawLine(Graphics g,Color c)
{
Pen p=new Pen(c,PenWidth);
g.DrawLine(p,StartPoint,EndPoint);
p.Dispose();
}

public bool IsInLine(Point pnt)
{
Pen p = new Pen(Color.Black,PenWidth);
GraphicsPath pth=new GraphicsPath();
pth.AddLine(StartPoint,EndPoint);
pth.Widen(p);
p.Dispose();

if(pth.IsVisible(pnt))
{
return true;
}
else
{
return false;
}
}
}

public class LineArray : CollectionBase
{
public void Add(Line l)
{
List.Add(l);
}

public Line this[int index]
{
get
{
return (Line)List[index];
}
set
{
List[index]=value;
}
}
}
}

Nov 16 '05 #3
Below my signature is my slightly modified version of your game.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
---------------------------------------------------------------------
using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace AirHockey

{

/// <summary>

/// Summary description for Table.

/// </summary>

public class frmTable : System.Windows.Forms.Form

{

private System.Windows.Forms.PictureBox picTable;

private System.Windows.Forms.Label lblPlayer2Score;

private System.Windows.Forms.Label lblPlayer1Score;

private System.Windows.Forms.Label lblPlayer2Name;

private System.Windows.Forms.Label lblPlayer1Name;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.MainMenu frmMainMenu;

Timer gameClock;

int puck_x;

int puck_y;

int x_vel = -5;

int y_vel = -5;

private LineArray Lines=new LineArray();

Line lineLeft;

Line lineRight;

Line lineTopLeft;

Line lineTopRight;

Line lineBottomLeft;

Line lineBottomRight;

bool hitLinesDrawn = false;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public frmTable()

{

InitializeComponent();

this.CreateLines();

}

public static void Main()

{

Application.Run(new frmTable());

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

///

private void InitializeComponent()

{

System.Resources.ResourceManager resources = new

System.Resources.ResourceManager(typeof(frmTable)) ;

this.picTable = new System.Windows.Forms.PictureBox();

this.lblPlayer2Score = new System.Windows.Forms.Label();

this.lblPlayer1Score = new System.Windows.Forms.Label();

this.lblPlayer2Name = new System.Windows.Forms.Label();

this.lblPlayer1Name = new System.Windows.Forms.Label();

this.label1 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// picTable

//

this.picTable.BackColor = System.Drawing.Color.Transparent;

this.picTable.Image =

((System.Drawing.Image)(resources.GetObject("picTa ble.Image")));

this.picTable.Location = new System.Drawing.Point(64, -24);

this.picTable.Name = "picTable";

this.picTable.Size = new System.Drawing.Size(848, 720);

this.picTable.SizeMode =

System.Windows.Forms.PictureBoxSizeMode.CenterImag e;

this.picTable.TabIndex = 0;

this.picTable.TabStop = false;

this.picTable.Paint += new

System.Windows.Forms.PaintEventHandler(this.picTab le_Paint);

this.picTable.MouseMove += new

System.Windows.Forms.MouseEventHandler(this.picTab le_MouseMove);

//

// lblPlayer2Score

//

this.lblPlayer2Score.Anchor =

System.Windows.Forms.AnchorStyles.None;

this.lblPlayer2Score.BackColor = System.Drawing.Color.Black;

this.lblPlayer2Score.BorderStyle =

System.Windows.Forms.BorderStyle.Fixed3D;

this.lblPlayer2Score.Cursor = System.Windows.Forms.Cursors.Default;

this.lblPlayer2Score.FlatStyle =

System.Windows.Forms.FlatStyle.System;

this.lblPlayer2Score.Font = new System.Drawing.Font("OCR A Extended", 48F,
System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.lblPlayer2Score.ForeColor = System.Drawing.Color.Red;

this.lblPlayer2Score.ImageAlign =

System.Drawing.ContentAlignment.TopLeft;

this.lblPlayer2Score.Location = new System.Drawing.Point(16, 56);

this.lblPlayer2Score.Name = "lblPlayer2Score";

this.lblPlayer2Score.Size = new System.Drawing.Size(144, 72);

this.lblPlayer2Score.TabIndex = 1;

this.lblPlayer2Score.Text = "00";

this.lblPlayer2Score.TextAlign =

System.Drawing.ContentAlignment.MiddleCenter;

//

// lblPlayer1Score

//

this.lblPlayer1Score.Anchor =

System.Windows.Forms.AnchorStyles.None;

this.lblPlayer1Score.BackColor = System.Drawing.Color.Black;

this.lblPlayer1Score.BorderStyle =

System.Windows.Forms.BorderStyle.Fixed3D;

this.lblPlayer1Score.FlatStyle =

System.Windows.Forms.FlatStyle.System;

this.lblPlayer1Score.Font = new System.Drawing.Font("OCR AExtended", 48F,
System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.lblPlayer1Score.ForeColor = System.Drawing.Color.Red;

this.lblPlayer1Score.ImageAlign =

System.Drawing.ContentAlignment.TopLeft;

this.lblPlayer1Score.Location = new System.Drawing.Point(816, 544);

this.lblPlayer1Score.Name = "lblPlayer1Score";

this.lblPlayer1Score.Size = new System.Drawing.Size(144, 72);

this.lblPlayer1Score.TabIndex = 2;

this.lblPlayer1Score.Text = "00";

this.lblPlayer1Score.TextAlign =

System.Drawing.ContentAlignment.MiddleCenter;

//

// lblPlayer2Name

//

this.lblPlayer2Name.BackColor = System.Drawing.Color.Transparent;

this.lblPlayer2Name.FlatStyle =

System.Windows.Forms.FlatStyle.Flat;

this.lblPlayer2Name.Font = new System.Drawing.Font("Microsoft Sans Serif",
15.75F, System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.lblPlayer2Name.ForeColor = System.Drawing.Color.Yellow;

this.lblPlayer2Name.Location = new System.Drawing.Point(16, 24);

this.lblPlayer2Name.Name = "lblPlayer2Name";

this.lblPlayer2Name.Size = new System.Drawing.Size(144, 32);

this.lblPlayer2Name.TabIndex = 3;

this.lblPlayer2Name.Text = "Player2Name";

this.lblPlayer2Name.TextAlign =

System.Drawing.ContentAlignment.MiddleCenter;

//

// lblPlayer1Name

//

this.lblPlayer1Name.BackColor = System.Drawing.Color.Transparent;

this.lblPlayer1Name.Font = new System.Drawing.Font("Microsoft Sans Serif",
15.75F, System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.lblPlayer1Name.ForeColor = System.Drawing.Color.Yellow;

this.lblPlayer1Name.Location = new System.Drawing.Point(816, 512);

this.lblPlayer1Name.Name = "lblPlayer1Name";

this.lblPlayer1Name.Size = new System.Drawing.Size(144, 32);

this.lblPlayer1Name.TabIndex = 4;

this.lblPlayer1Name.Text = "Player1Name";

this.lblPlayer1Name.TextAlign =

System.Drawing.ContentAlignment.MiddleCenter;

//

// label1

//

this.label1.Location = new System.Drawing.Point(0, 0);

this.label1.Name = "label1";

this.label1.TabIndex = 5;

this.label1.Text = "label1";

//

// frmTable

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.BackgroundImage =

((System.Drawing.Image)(resources.GetObject("$this .BackgroundImage")));

this.ClientSize = new System.Drawing.Size(973, 647);

this.Controls.Add(this.label1);

this.Controls.Add(this.lblPlayer1Name);

this.Controls.Add(this.lblPlayer2Name);

this.Controls.Add(this.lblPlayer1Score);

this.Controls.Add(this.lblPlayer2Score);

this.Controls.Add(this.picTable);

this.Cursor = System.Windows.Forms.Cursors.Default;

this.FormBorderStyle =

System.Windows.Forms.FormBorderStyle.Fixed3D;

this.Name = "frmTable";

this.StartPosition =

System.Windows.Forms.FormStartPosition.CenterScree n;

this.Text = "Air Hockey";

this.Load += new System.EventHandler(this.frmTable_Load);

this.Closed += new System.EventHandler(this.frmTable_Closed);

this.MouseMove += new

System.Windows.Forms.MouseEventHandler(this.picTab le_MouseMove);

this.ResumeLayout(false);

}

#endregion

private void frmTable_Closed(object sender, System.EventArgs e)

{

frmMainMenu = new MainMenu();

//MainMenu.Show();

}

private void gameLoop(Object sender, EventArgs e)

{

puck_x += x_vel;

puck_y += y_vel;

//Called so the form will repaint itself.

Invalidate();

}

private void gameDraw(Graphics g)

{

// image of puck is loaded and drawn

Image image =
Image.FromStream(this.GetType().Assembly.GetManife stResourceStream("airhocky
..Bitmap1.bmp"));

g.DrawImage(image, puck_x, puck_y, 35, 35);

foreach(Line l in Lines)

{

l.DrawLine(g, Color.FromArgb(192,192,192));

}

}

public void setName(string pName)

{

lblPlayer1Name.Text = pName;

}

private void frmTable_Load(object sender, System.EventArgs e)

{

gameClock = new Timer();

//Start the game clock to control game speed

gameClock.Interval = 20;

gameClock.Tick += new EventHandler(gameLoop);

gameClock.Start();

puck_x = 200;

puck_y = 200;

}

//modified a little bit by Bob.

private void picTable_MouseMove(object sender,

System.Windows.Forms.MouseEventArgs e)

{

label1.Text = e.X + " " + e.Y;

Graphics g=CreateGraphics();

foreach(Line l in Lines)

l.IsInLine(new Point(e.X,e.Y));

}

private void picTable_Paint(object sender,

System.Windows.Forms.PaintEventArgs e)

{

gameDraw(e.Graphics);

}

//Added by Bob to create the lines

private void CreateLines()

{

// set of hit lines for sides of table

lineLeft = new Line();

lineLeft.StartPoint=new Point( 164, 77 );

lineLeft.EndPoint=new Point( 84, 594 );

Lines.Add(lineLeft);

lineRight = new Line();

lineRight.StartPoint=new Point( 631, 77 );

lineRight.EndPoint=new Point( 712, 594 );

Lines.Add(lineRight);

// set of hit lines for top of table

lineTopLeft = new Line();

lineTopLeft.StartPoint=new Point( 166, 76 );

lineTopLeft.EndPoint=new Point( 340, 76 );

Lines.Add(lineTopLeft);

lineTopRight = new Line();

lineTopRight.StartPoint=new Point( 630, 76 );

lineTopRight.EndPoint=new Point( 455, 76 );

Lines.Add(lineTopRight);

// set of hit lines for top of table

lineBottomLeft = new Line();

lineBottomLeft.StartPoint=new Point( 86, 595 );

lineBottomLeft.EndPoint=new Point( 328, 595 );

Lines.Add(lineBottomLeft);

lineBottomRight = new Line();

lineBottomRight.StartPoint=new Point( 711, 595 );

lineBottomRight.EndPoint=new Point( 467, 595 );

Lines.Add(lineBottomRight);

}

//heavily modified by Bob

private void DrawHitLines(Graphics g)

{

foreach(Line l in Lines)

l.DrawLine(g,Color.Gray);

}

}

public class Line

{

public Point StartPoint;

public Point EndPoint;

public int PenWidth;

bool hit;

public void DrawLine(Graphics g,Color c)

{

Pen p=new Pen(c,PenWidth);

if(hit)

p.Color=Color.Red;

g.DrawLine(p,StartPoint,EndPoint);

p.Dispose();

}

public bool IsInLine(Point pnt)

{

Pen p = new Pen(Color.Black,PenWidth);

GraphicsPath pth=new GraphicsPath();

pth.AddLine(StartPoint,EndPoint);

pth.Widen(p);

p.Dispose();

if(pth.IsVisible(pnt))

{

this.hit=true;

return true;

}

else

{

this.hit=false;

return false;

}

}

}

public class LineArray : CollectionBase

{

public void Add(Line l)

{

List.Add(l);

}

public Line this[int index]

{

get

{

return (Line)List[index];

}

set

{

List[index]=value;

}

}

}

}

---------------------------------------------------------------------
"Brian" <br************@gmail.com> wrote in message
news:c2*************************@posting.google.co m...
Hello all..

Am working on an Air Hockey game... have an table loaded into a
picture box. The borders of the table are slightly slanted. Am using
hit testing lines with GDI+ to manipulate the puck moving around. I
want the puck is bounce when it hits a border (specified by the
hitlines).

Retreieved some info on hit testing lines from Bob Powell's GDI+ FAQ
(very useful!) but i'm fairly new at the idea of hit testing and am
wondering what exactly is wrong with this code.

Sorry about the abundance of code but my code isn't exactly safe
enough to post a sample (so basically i'm saying, it's fairly badly
written code). I thought it might be easier if people just tried to
run this code themselves and let me know of the results.

Anu suggestions will be much appreciated!

Thanks in advance!
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AirHockey
{
/// <summary>
/// Summary description for Table.
/// </summary>
public class frmTable : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox picTable;
private System.Windows.Forms.Label lblPlayer2Score;
private System.Windows.Forms.Label lblPlayer1Score;
private System.Windows.Forms.Label lblPlayer2Name;
private System.Windows.Forms.Label lblPlayer1Name;
Timer gameClock;
int puck_x;
int puck_y;
int x_vel = -5;
int y_vel = -5;

private LineArray Lines=new LineArray();
Line lineLeft;
Line lineRight;
Line lineTopLeft;
Line lineTopRight;
Line lineBottomLeft;
Line lineBottomRight;
bool hitLinesDrawn = false;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public frmTable()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
///

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(frmTable)) ;
this.picTable = new System.Windows.Forms.PictureBox();
this.lblPlayer2Score = new System.Windows.Forms.Label();
this.lblPlayer1Score = new System.Windows.Forms.Label();
this.lblPlayer2Name = new System.Windows.Forms.Label();
this.lblPlayer1Name = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// picTable
//
this.picTable.BackColor = System.Drawing.Color.Transparent;
this.picTable.Image =
((System.Drawing.Image)(resources.GetObject("picTa ble.Image")));
this.picTable.Location = new System.Drawing.Point(64, -24);
this.picTable.Name = "picTable";
this.picTable.Size = new System.Drawing.Size(848, 720);
this.picTable.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.CenterImag e;
this.picTable.TabIndex = 0;
this.picTable.TabStop = false;
this.picTable.Paint += new
System.Windows.Forms.PaintEventHandler(this.picTab le_Paint);
this.picTable.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTab le_MouseMove);
//
// lblPlayer2Score
//
this.lblPlayer2Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer2Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer2Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer2Score.Cursor = System.Windows.Forms.Cursors.Default;
this.lblPlayer2Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer2Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer2Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer2Score.Location = new System.Drawing.Point(16, 56);
this.lblPlayer2Score.Name = "lblPlayer2Score";
this.lblPlayer2Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer2Score.TabIndex = 1;
this.lblPlayer2Score.Text = "00";
this.lblPlayer2Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Score
//
this.lblPlayer1Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer1Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer1Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer1Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer1Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer1Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer1Score.Location = new System.Drawing.Point(816, 544);
this.lblPlayer1Score.Name = "lblPlayer1Score";
this.lblPlayer1Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer1Score.TabIndex = 2;
this.lblPlayer1Score.Text = "00";
this.lblPlayer1Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer2Name
//
this.lblPlayer2Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer2Name.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.lblPlayer2Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer2Name.Location = new System.Drawing.Point(16, 24);
this.lblPlayer2Name.Name = "lblPlayer2Name";
this.lblPlayer2Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer2Name.TabIndex = 3;
this.lblPlayer2Name.Text = "Player2Name";
this.lblPlayer2Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Name
//
this.lblPlayer1Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer1Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer1Name.Location = new System.Drawing.Point(816, 512);
this.lblPlayer1Name.Name = "lblPlayer1Name";
this.lblPlayer1Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer1Name.TabIndex = 4;
this.lblPlayer1Name.Text = "Player1Name";
this.lblPlayer1Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.TabIndex = 5;
this.label1.Text = "label1";
//
// frmTable
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackgroundImage =
((System.Drawing.Image)(resources.GetObject("$this .BackgroundImage")));
this.ClientSize = new System.Drawing.Size(973, 647);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblPlayer1Name);
this.Controls.Add(this.lblPlayer2Name);
this.Controls.Add(this.lblPlayer1Score);
this.Controls.Add(this.lblPlayer2Score);
this.Controls.Add(this.picTable);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "frmTable";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScree n;
this.Text = "Air Hockey";
this.Load += new System.EventHandler(this.frmTable_Load);
this.Closed += new System.EventHandler(this.frmTable_Closed);
this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTab le_MouseMove);
this.ResumeLayout(false);

}
#endregion

private void frmTable_Closed(object sender, System.EventArgs e)
{
frmMainMenu MainMenu = new frmMainMenu();
MainMenu.Show();
}

private void gameLoop(Object sender, EventArgs e)
{
puck_x += x_vel;
puck_y += y_vel;

//Called so the form will repaint itself.
Invalidate();
}

private void gameDraw(Graphics g)
{
// image of puck is loaded and drawn
Image image = Image.FromFile("C:/Documents and Settings/All
Users.WINDOWS/Documents/Brian's Documents/AirHockey/Puck.png");
g.DrawImage(image, puck_x, puck_y, 35, 35);

foreach(Line l in Lines)
{
l.DrawLine(g, Color.FromArgb(192,192,192));
}
}

public void setName(string pName)
{
lblPlayer1Name.Text = pName;
}

private void frmTable_Load(object sender, System.EventArgs e)
{
gameClock = new Timer();

//Start the game clock to control game speed
gameClock.Interval = 15;
gameClock.Tick += new EventHandler(gameLoop);
gameClock.Start();

puck_x = 200;
puck_y = 200;
}

private void picTable_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
label1.Text = e.X + " " + e.Y;

Graphics g=CreateGraphics();

if(hitLinesDrawn != true)
{
DrawHitLines(g);
}
else
{
foreach(Line l in Lines)
{
if(l.IsInLine(new Point(e.X,e.Y)))
l.DrawLine(g,Color.Red);
else
l.DrawLine(g,Color.Black);
}
}
}

private void picTable_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
gameDraw(e.Graphics);
}

private void DrawHitLines(Graphics g)
{
// set of hit lines for sides of table
lineLeft = new Line();
lineLeft.StartPoint=new Point( 164, 77 );
lineLeft.EndPoint=new Point( 84, 594 );
lineLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineLeft);

lineRight = new Line();
lineRight.StartPoint=new Point( 631, 77 );
lineRight.EndPoint=new Point( 712, 594 );
lineRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineRight);

// set of hit lines for top of table
lineTopLeft = new Line();
lineTopLeft.StartPoint=new Point( 166, 76 );
lineTopLeft.EndPoint=new Point( 340, 76 );
lineTopLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopLeft);

lineTopRight = new Line();
lineTopRight.StartPoint=new Point( 630, 76 );
lineTopRight.EndPoint=new Point( 455, 76 );
lineTopRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopRight);

// set of hit lines for top of table
lineBottomLeft = new Line();
lineBottomLeft.StartPoint=new Point( 86, 595 );
lineBottomLeft.EndPoint=new Point( 328, 595 );
lineBottomLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineBottomLeft);

lineBottomRight = new Line();
lineBottomRight.StartPoint=new Point( 711, 595 );
lineBottomRight.EndPoint=new Point( 467, 595 );
lineBottomRight.DrawLine(g, Color.Red);
Lines.Add(lineBottomRight);

hitLinesDrawn = true;
}
}

public class Line
{
public Point StartPoint;
public Point EndPoint;
public int PenWidth;

public void DrawLine(Graphics g,Color c)
{
Pen p=new Pen(c,PenWidth);
g.DrawLine(p,StartPoint,EndPoint);
p.Dispose();
}

public bool IsInLine(Point pnt)
{
Pen p = new Pen(Color.Black,PenWidth);
GraphicsPath pth=new GraphicsPath();
pth.AddLine(StartPoint,EndPoint);
pth.Widen(p);
p.Dispose();

if(pth.IsVisible(pnt))
{
return true;
}
else
{
return false;
}
}
}

public class LineArray : CollectionBase
{
public void Add(Line l)
{
List.Add(l);
}

public Line this[int index]
{
get
{
return (Line)List[index];
}
set
{
List[index]=value;
}
}
}
}

Nov 16 '05 #4
Thank you very much Bob!

I have a feeling it was to do with the painting i was doing in the
MouseMove event. Also, painting on a Picture Box can be quite awkard.

Thanks again. Much appreciated!

By the way, your GDI+ FAQ is definantely the most useful resource i've
used, with regards to C#.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
As i want the puck bounce off the walls, i'd want to have the condition to
check each of the lines in the Array in the gameLoop() method.

But the hit testing involves drawing of points using the position of the
mouse (e.X and e.Y) which is only allowed in the MouseMove method.

How do i get the position of the mouse outside of a MouseMove event handler?

private void gameLoop(Object sender, EventArgs e)
{
puck_x += x_vel;
puck_y += y_vel;

//Called so the form will repaint itself.
Invalidate();
}

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Peter Hansen | last post by:
The term "mock filesystem" refers to code allowing unit or acceptance tests to create, read and write, and manipulate in other ways "virtual" files, without any actual disk access. Everything is...
1
by: Mark Jerde | last post by:
I'm working on a testing application. It will run on multiple computers. Each computer has the same "suite" of tests. The test operator can choose which tests to run. It is expected that each...
0
by: Brian | last post by:
Hello all.. Am working on an Air Hockey game... have an table loaded into a picture box. The borders of the table are slightly slanted. Am using hit testing lines with GDI+ to manipulate the...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
16
by: Joel Byrd | last post by:
I am having this ridiculous problem of trying to set the innerHTML of a div node. It works in all other browsers, but internet explorer is giving me an "Unknown runtime error". This is actually...
6
by: blackice | last post by:
hello all ..., first i would thank all the people who are in this forums cause they are very helpful ..., By the way i had some posts hers about comment out the named.conf file the script is...
24
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do...
4
by: David | last post by:
Hi list. Do test-driven development or behaviour-driven development advocate how to do higher-level testing than unit testing? types of testing: unit integration system
2
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.