Published on

10,667 Points

This article is filled with multiple code samples and explanations for mathematical calculations. They are as follows:

1. General tips

2. Quadratic formula

3. Object collision

4. Projectile path

**General Tips**

Here are some of my tips for doing mathematical formulas in VB.NET. First of all, you should have a variable for every variable in the formula. Also, that new variable should be in a form that can hold only valid values. For instance, a formula that uses a decimal value may be declared as a double, but a formula that can only use whole numbers should be declared integer because errors could occur if declared double. Also you should add error handling for areas that the variable can hold values that are invalid such as if it has to be a nonzero whole number you could use this line to remove zeros.

Also most equations can be copied straight into the code. As long as the answer is isolated on one side of the equation. Such as the standard form for a linear graph y = ax + b. In this case you can do this.

This way it is simple to use a formula that is already in the correct form. If it is not in the correct form then it is harder. in this case you have to break it up or use some algebra to get it in the correct form.

**Quadratic Formula**

The quadratic formula is used to find the x intercepts in an equation that takes the following form ax² + bx + c = y. The x intercept is the value of x when y = 0. Also, being a non-linear function, it has 2 intercepts. The formula for finding the x intercepts is as follows.

Notice that x is isolated on one side. Thus declare the variables and paste in the formula it can be done as follows.

As you can see it is as simple as declaring the variables, then pasting in the formula to the right spot.

**Object Collision**

This is something you will most likely run into if you make a game. You would think it would be easy to tell if two objects overlap but it is not as easy as you think. There is no method that I know of that checks if any points overlap. To do this there are 2 ways one is to get an array of all the points in the rectangle the object is occupying and see if any are the same this may be the best way if there are multiple objects to check. The other way that I have demonstrated is to check if any of the four sides may possibly overlap if all sides can overlap then at least one point is overlapping. For instance if the left side of one object is to the right of the right side of another object they do not overlap but if it is to the left of the right side then it may overlap. This code uses that logic on all sides to determine if it is overlapping at any point. My code is as follows.

It is a bit long but it works fairly quick since it can tell if it is impossible to collide at each if statement.

**Projectile Path**

I have to give the credit of the formula and the idea of this section to courtenayt and the question here. This is for finding the x and y coordinates of a projectile at regular time intervals, also useful in game building. This seemed easy at first until it came to recording the coordinates at each second the formula for this is fairly complicated but is explained in the question linked to above. It is basically a matter of running the formula with all the parameters and recording the x, and y values at each second. For this instance I created a structure to hold the data but many other forms would work. This is the finished code. Note how the do statement loops increasing the time value at each loop. This is what eventually brings the projectile to ground or a y value of 0. also the math.sin/ math.cos statments require the angle to be in radians, but the parameter is expresed in degrees thus requiring a conversion. this is all done below.

Again a lengthy piece of code but most of this code is to set up the data recording and to retrieve the parameters. Also note how an if statment was added to the set statment of the angle property. since a projectile is usaly sent slightly upwards in front of the sender I added this condition so that the parameter is never out of the correct range and if it is then the value is set to 45 as a default value.

**Conclusion**

Finally I have shown you my tricks to math in VB.NET, and some more complex formulas that are useful in both games and real world calculations. Math is hard but if you know the formula the computer can do most of it for you. If you tell it to.

**References**

Unless otherwise stated all work here is of my own thoughts and unrelated to any other source

Quadratic formula verified by, and image copied from Wikipedia at http://en.wikipedia.org/wiki/Quadratic_equation

Projectile path formula received from the author of the question here

1. General tips

2. Quadratic formula

3. Object collision

4. Projectile path

Here are some of my tips for doing mathematical formulas in VB.NET. First of all, you should have a variable for every variable in the formula. Also, that new variable should be in a form that can hold only valid values. For instance, a formula that uses a decimal value may be declared as a double, but a formula that can only use whole numbers should be declared integer because errors could occur if declared double. Also you should add error handling for areas that the variable can hold values that are invalid such as if it has to be a nonzero whole number you could use this line to remove zeros.

```
Dim x as integer = input
if x <> 0 then
'continue
else
'request new input
end if
```

Also most equations can be copied straight into the code. As long as the answer is isolated on one side of the equation. Such as the standard form for a linear graph y = ax + b. In this case you can do this.

```
Dim x as integer = input
Dim a as integer = input
Dim b as integer = input
Dim y as integer = ax + b
```

This way it is simple to use a formula that is already in the correct form. If it is not in the correct form then it is harder. in this case you have to break it up or use some algebra to get it in the correct form.

The quadratic formula is used to find the x intercepts in an equation that takes the following form ax² + bx + c = y. The x intercept is the value of x when y = 0. Also, being a non-linear function, it has 2 intercepts. The formula for finding the x intercepts is as follows.

Notice that x is isolated on one side. Thus declare the variables and paste in the formula it can be done as follows.

```
Dim a As Double = TextBox1.Text
Dim b As Double = TextBox2.Text
Dim c As Double = TextBox3.Text
Dim x1 as Double = (-b + math.sqrt(b ^ 2 - 4 * a * c)) / (2 * a)
Dim x2 as Double = (-b - math.sqrt(b ^ 2 - 4 * a * c)) / (2 * a)
```

As you can see it is as simple as declaring the variables, then pasting in the formula to the right spot.

This is something you will most likely run into if you make a game. You would think it would be easy to tell if two objects overlap but it is not as easy as you think. There is no method that I know of that checks if any points overlap. To do this there are 2 ways one is to get an array of all the points in the rectangle the object is occupying and see if any are the same this may be the best way if there are multiple objects to check. The other way that I have demonstrated is to check if any of the four sides may possibly overlap if all sides can overlap then at least one point is overlapping. For instance if the left side of one object is to the right of the right side of another object they do not overlap but if it is to the left of the right side then it may overlap. This code uses that logic on all sides to determine if it is overlapping at any point. My code is as follows.

```
Public function collision(object1 as control, object2 as control) as Boolean
if object1.Left >= object2.Left + object2.Width Then
return false
Else
If object1.Left + object1.Width <= object2.Left Then
return false
Else
If object1.Top >= object2.Top + object2.Height Then
return false
Else
If object1.Top + object1.Height <= object2.Top Then
return false
Else
return true
End If
End If
End If
End If
End Sub
```

It is a bit long but it works fairly quick since it can tell if it is impossible to collide at each if statement.

I have to give the credit of the formula and the idea of this section to courtenayt and the question here. This is for finding the x and y coordinates of a projectile at regular time intervals, also useful in game building. This seemed easy at first until it came to recording the coordinates at each second the formula for this is fairly complicated but is explained in the question linked to above. It is basically a matter of running the formula with all the parameters and recording the x, and y values at each second. For this instance I created a structure to hold the data but many other forms would work. This is the finished code. Note how the do statement loops increasing the time value at each loop. This is what eventually brings the projectile to ground or a y value of 0. also the math.sin/ math.cos statments require the angle to be in radians, but the parameter is expresed in degrees thus requiring a conversion. this is all done below.

```
Public Structure locations_per_sec
Dim time As Integer
Dim XLoc As Double
Dim YLoc As Double
End Structure
Private a As Double
Private Vp As Double
Private Vh As Double
Private Vv As Double
Private H As Double
Public Property angle() As Double
Get
Return a
End Get
Set(ByVal value As Double)
If -1 < value And value < 91 Then
a = value
Else : a = 45
End If
End Set
End Property
Public Property speed() As Double
Get
Return Vp
End Get
Set(ByVal value As Double)
Vp = value
End Set
End Property
Public Function location_times(Optional ByVal Height As Double = 5.0) As List(Of locations_per_sec)
Dim z As New List(Of locations_per_sec)(0)
Dim time As Integer = 0
Dim XLoc As Double = 0
Dim YLoc As Double = 0
h = Height
Dim sec As New locations_per_sec
angle = angle * math.pi / 180
Vh = Vp * Math.Cos(angle)
Vv = Vp * Math.Sin(angle)
Do Until YLoc < 0
time += 1
XLoc = Vh * time
YLoc = H + Vv * time - 0.5 * 32 * time ^ 2
sec.time = time
sec.XLoc = XLoc
sec.YLoc = YLoc
z.Add(sec)
Loop
Return z
End Function
```

Again a lengthy piece of code but most of this code is to set up the data recording and to retrieve the parameters. Also note how an if statment was added to the set statment of the angle property. since a projectile is usaly sent slightly upwards in front of the sender I added this condition so that the parameter is never out of the correct range and if it is then the value is set to 45 as a default value.

Finally I have shown you my tricks to math in VB.NET, and some more complex formulas that are useful in both games and real world calculations. Math is hard but if you know the formula the computer can do most of it for you. If you tell it to.

Unless otherwise stated all work here is of my own thoughts and unrelated to any other source

Quadratic formula verified by, and image copied from Wikipedia at http://en.wikipedia.org/wi

Projectile path formula received from the author of the question here

Ask questions about what you read

If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.