Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Object collision in vb.net – C#.net.

Author: Shehzad Hemani
Download Source Code : 1073_source.zip

In this simple article you will learn that how we can collide two objects on a form in VB and C# .net.

 To make collision of two objects first of all we need two objects on the form. Drag two picture boxes on the form, set the pictures in those boxes. One is your object that you want to collide with the target and other picture box is your target.

Now take the integer as rate of change for the location of the object. You will have to move the object and the target will remain stationary. You can move the object by changing its location on the form.

You will have to use the timer control to move object or to change the object location. If the location of the object gets negative or gets out of the bounds of the form rate of change become negative so that it can move backward. In this article we will represent the collision by changing the background color. When the bounds of the object intersect the bounds of the target change the form background color to show the collision. To check the bounds intersection we have a function of picturebox class named intersectswith (). This function takes the object of rectangle class and returns the Boolean value. If the bounds intersects this function returns true else it returns false.

Now write the following code on TIMER TICK event:

C#

        private void timer1_Tick(object sender, EventArgs e)

        {

            int newX, newY;

            newX = PIC_OBJECT.Location.X + dx;

            newY = PIC_OBJECT.Location.Y + dx;

            if (newX > this.Width - PIC_OBJECT.Width)

            {

                dx = -dx;

            }

            if (newX < 0)

            {

                dx = -dx;

            }

            if (PIC_OBJECT.Bounds.IntersectsWith(pic_Target.Bounds))

            {

                this.BackColor = Color.Black;

            }

            else

            {

                this.BackColor = Color.White;

            }

            PIC_OBJECT.Location = new Point(newX, newY);

        }            

VB

Private Sub timer1_Tick(ByVal sender As ObjectByVal As EventArgs)
        
Dim newY As Integer
        Dim 
newX As Integer
        
newX (PIC_OBJECT.Location.X + dx)
        newY 
(PIC_OBJECT.Location.Y + dx)
        
If (newX  _
                    > (
Me.Width - PIC_OBJECT.Width)) Then
            
dx (dx * -1)
        
End If
        If 
(newX < 0Then
            
dx (dx * -1)
        
End If
        If 
PIC_OBJECT.Bounds.IntersectsWith(pic_Target.Bounds) Then
            Me
.BackColor Color.Black
        
Else
            Me
.BackColor Color.White
        
End If
        
PIC_OBJECT.Location = New Point(newX, newY)
    
End Sub

This is simple code to collide two objects.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP Image APPLICATION";

            Point p = new Point(160, 240);

            PIC_OBJECT.Image = obj;

            pic_Target.BackColor = Color.DarkGoldenrod;

            pic_Target.Location = p;

            p = new Point(24,136);

            PIC_OBJECT.Location = p;

            timer1.Interval = 50;

            timer1.Start();

        }

VB

Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)
        
Me.Text "DEVASP Image APPLICATION"
        
Dim As Point = New Point(160240)
        PIC_OBJECT.Image 
obj
        pic_Target.BackColor 
Color.DarkGoldenrod
        pic_Target.Location 
p
        p 
= New Point(24136)
        PIC_OBJECT.Location 
p
        timer1.Interval 
50
        
timer1.Start
    
End Sub

This simple article tells that how we can collide two objects on a form in VB and C# .net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Edge sharpening in an image using laplacian filter in VB.net - C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net