Code:
Private Sub shoot(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick
Dim num1 As Decimal = 0
Dim num2 As Decimal = 0
Dim divnum As Decimal = 0
Dim hypotenuse As Decimal = 0
If e.Button = Windows.Forms.MouseButtons.Left Then
bullet(1).Visible = True
Do
divnum = divnum + 0.01
num1 = (e.Y - ship.Top + 9) / divnum
num2 = (e.X - ship.Left + 9) / divnum
hypotenuse = ((num1 ^ 2) + (num2 ^ 2)) ^ (1 / 2)
Loop Until hypotenuse < 3.1 And hypotenuse > 2.9
bullety(1) = num1
bulletx(1) = num2
end if
end sub
WHAT IM DOING: The location of the character (ship) forms a triangle with the location of the mouse, so i'm using Pythagorean theorem to calculate the hypotenuse of that triangle. I need every shot to have the same hypotenuse (so it goes at the same speed every time) so i use a do loop and divide the x and y coords by increments of .1 until the hypotenuse is close to 3. I then use the lengths of the sides of the triangle that i just acquired as rise over run for the bullet that is shot from the character (bullety(1) and bulletx(1)). sorry if u dont understand- it's hard to explain.
PROBLEM: i've tried tons of ways to achieve my goal, but now i'm stumped and the method i previously explained is the closest i can get. it kind of works, but it's inaccurate. It's hard to find the correct slope to give my bullet because i'm using decimals, so its nearly impossible to get exactly a hypotenuse of 3. I tried rounding but it's still too inaccurate. A solution would be largely appreciated!!!!!
if you want to try running the program to see what i mean i've included a .exe file. i will give the full code on request