1 Attachment(s)
I would like for my spaceship to face in the proper direction
This is what I have so far. If you click anywhere, the spaceship will rotate 90 degrees. What I am trying to do is a little confusing. I want the player to be able to click anywhere around the spaceship.
If the player clicks above and to the right, then I want the spaceship to face up or right. If it faces up or right, then the facing direction depends on what was further in the click (x or y). If the player clicked further right then up, then the ship will face right, if the player clicked further up then right, then the ship should face up.
How can I do this?
Spaceship image
Attachment 146551
Code:
Public Class Form1
Dim bm As New Bitmap("spaceship.gif")
Dim spaceshipx As Integer = 250
Dim spaceshipy As Integer = 250
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawImage(bm, spaceshipx - 16, spaceshipy - 16)
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
bm.RotateFlip(RotateFlipType.Rotate90FlipNone)
PictureBox1.Invalidate()
End Sub
End Class
Re: I would like for my spaceship to face in the proper direction
Just calculate the difference between e.X and spaceshipx (deltaX) and the difference between spaceshipy and e.Y (deltaY).
I reversed the y calculation because y decreases as you go up.
If deltaX is greater than deltaY then face right, otherwise face up.
What do you want to do if they click on the left, or below the ship?
Re: I would like for my spaceship to face in the proper direction
I have an idea. When you click around the ship, it will calculate the angle of where you clicked and the center position of the ship and the ship will face that angle! I've done it many times before. Heres some code to put you on the right path:
vb.net Code:
Private Function Get_Radian(X1 As Single, Y1 As Single, X2 As Single, Y2 As Single) As Single
Dim DX As Single, DY As Single
Dim Angle As Single
DX = X2 - X1
DY = -Y2 - -Y1
Angle = 0
If DX = 0 Then
If DY = 0 Then
Angle = 0
ElseIf DY > 0 Then
Angle = Convert.ToSingle(Math.PI) / 2
Else
Angle = Convert.ToSingle(Math.PI) * 3 / 2
End If
ElseIf DY = 0 Then
If DX > 0 Then
Angle = 0
Else
Angle = Convert.ToSingle(Math.PI)
End If
Else
If DX < 0 Then
Angle = Convert.ToSingle(Math.Atan2(DY, DX) + Math.PI)
ElseIf DY < 0 Then
Angle = Convert.ToSingle(Math.Atan2(DY, DX) + 2 * Math.PI)
Else
Angle = Convert.ToSingle(Math.Atan2(DY, DX))
End If
End If
Return Angle
End Function
Private Function Get_Degree(X1 As Single, Y1 As Single, X2 As Single, Y2 As Single) As Single
Dim DX As Single, DY As Single
Dim Angle As Single
DX = X2 - X1
DY = -Y2 - -Y1
Angle = 0
If DX = 0 Then
If DY = 0 Then
Angle = 0
ElseIf DY > 0 Then
Angle = Convert.ToSingle(Math.PI) / 2
Else
Angle = Convert.ToSingle(Math.PI) * 3 / 2
End If
ElseIf DY = 0 Then
If DX > 0 Then
Angle = 0
Else
Angle = Convert.ToSingle(Math.PI)
End If
Else
If DX < 0 Then
Angle = Convert.ToSingle(Math.Atan2(DY, DX) + Math.PI)
ElseIf DY < 0 Then
Angle = Convert.ToSingle(Math.Atan2(DY, DX) + 2 * Math.PI)
Else
Angle = Convert.ToSingle(Math.Atan2(DY, DX))
End If
End If
Return Convert.ToSingle(Angle * 180 / Math.PI)
End Function