I would like for my spaceship to face in the proper direction-VBForums
Results 1 to 3 of 3

Thread: I would like for my spaceship to face in the proper direction

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    1

    Question 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
    Name:  spaceship.gif
Views: 596
Size:  988 Bytes

    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

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,918

    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?

  3. #3
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,165

    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:
    1. Private Function Get_Radian(X1 As Single, Y1 As Single, X2 As Single, Y2 As Single) As Single
    2.  
    3.         Dim DX As Single, DY As Single
    4.         Dim Angle As Single
    5.  
    6.         DX = X2 - X1
    7.         DY = -Y2 - -Y1
    8.         Angle = 0
    9.  
    10.         If DX = 0 Then
    11.             If DY = 0 Then
    12.                 Angle = 0
    13.             ElseIf DY > 0 Then
    14.                 Angle = Convert.ToSingle(Math.PI) / 2
    15.             Else
    16.                 Angle = Convert.ToSingle(Math.PI) * 3 / 2
    17.             End If
    18.         ElseIf DY = 0 Then
    19.             If DX > 0 Then
    20.                 Angle = 0
    21.             Else
    22.                 Angle = Convert.ToSingle(Math.PI)
    23.             End If
    24.         Else
    25.             If DX < 0 Then
    26.                 Angle = Convert.ToSingle(Math.Atan2(DY, DX) + Math.PI)
    27.             ElseIf DY < 0 Then
    28.                 Angle = Convert.ToSingle(Math.Atan2(DY, DX) + 2 * Math.PI)
    29.             Else
    30.                 Angle = Convert.ToSingle(Math.Atan2(DY, DX))
    31.             End If
    32.         End If
    33.         Return Angle
    34.  
    35.     End Function
    36.  
    37.     Private Function Get_Degree(X1 As Single, Y1 As Single, X2 As Single, Y2 As Single) As Single
    38.  
    39.         Dim DX As Single, DY As Single
    40.         Dim Angle As Single
    41.  
    42.         DX = X2 - X1
    43.         DY = -Y2 - -Y1
    44.         Angle = 0
    45.  
    46.         If DX = 0 Then
    47.             If DY = 0 Then
    48.                 Angle = 0
    49.             ElseIf DY > 0 Then
    50.                 Angle = Convert.ToSingle(Math.PI) / 2
    51.             Else
    52.                 Angle = Convert.ToSingle(Math.PI) * 3 / 2
    53.             End If
    54.         ElseIf DY = 0 Then
    55.             If DX > 0 Then
    56.                 Angle = 0
    57.             Else
    58.                 Angle = Convert.ToSingle(Math.PI)
    59.             End If
    60.         Else
    61.             If DX < 0 Then
    62.                 Angle = Convert.ToSingle(Math.Atan2(DY, DX) + Math.PI)
    63.             ElseIf DY < 0 Then
    64.                 Angle = Convert.ToSingle(Math.Atan2(DY, DX) + 2 * Math.PI)
    65.             Else
    66.                 Angle = Convert.ToSingle(Math.Atan2(DY, DX))
    67.             End If
    68.         End If
    69.         Return Convert.ToSingle(Angle * 180 / Math.PI)
    70.  
    71.     End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.