Rotating a picturebox by a set number of degrees

Gluttard

Active member
Joined
Jan 9, 2008
Messages
44
Programming Experience
Beginner
Hey all, I'm making a rocket ship to learn about certain aspects of Visual Basic coding, there is an image of a rocketship in a picturebox, the rocket.

I need to know how to rotate the picturebox by a set number of degrees.
Picturebox1.image.rotatefliptype does not work for me because that only goes 90, 180, and 270 degrees. I want it to be able to go 1 degree at a time.

Also, it needs to turn around the center, in other words, if I rotated it 180 degrees, the centerpoint would be in the same place but the rocket would be upside down.

Thanks.
 
I'm sorry to say this, but that link didn't help. Although I was able to understand the process through which the code rotated the image, I'm too much of a novice programmer to be able to diagnose the problems with the code. The Visual Basic 2008 express thing doesn't recognize PI or Sin, Cos, etc.

Is there maybe a code that just rotates the image around it's top left corner? I could settle with that for now and upgrade when I expand my knowledge.
 
Mathematics for rotation around centerpoint?

Hey there, I've written a little Rocketship program that you can fly around the screen, but it cannot rotate properly... Think of asteriods, it rotates like that and moves like that. Anyways, I've been directed to a couple different sites, but I don't know what math I have to use, Trigonometry?
Can someone explain to me how to do it? You don't have to give me the 'answer' to my problem, just an abstract explanation.
Here's the code, the only component is a timer:
PHP:
Public Class Form1
    Dim y As Integer = Screen.PrimaryScreen.Bounds.Height - 100
    Dim x As Integer = Screen.PrimaryScreen.Bounds.Width \ 2
    Dim angle As Integer = 0
    Dim directiony As Integer
    Dim ship As Image = My.Resources.RocketShipStill

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Enter Then
            Me.Close()
        End If
        If e.KeyCode = Keys.Up Then
            ship = My.Resources.RocketShipMove
            MoveTimer.Enabled = True
            directiony = 0
        End If
        If e.KeyCode = Keys.Left Then
            angle = angle + 1
            Me.Refresh()
        End If
    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        ship = My.Resources.RocketShipStill
        MoveTimer.Enabled = False
        Me.Refresh()
    End Sub


    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.RotateTransform(angle)
        e.Graphics.DrawImage(ship, x, y)
    End Sub

    Private Sub MoveTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MoveTimer.Tick
        If directiony = 0 Then
            y = y - 2
            Me.Refresh()
        End If
    End Sub
End Class
 
RotateTransform rotates around current world center, which is initially (0,0). If you then draw at (100,100) your image will rotate in a circle with that much offset from center. The offset you need to rotate around is center of image (ship), which is back half its dimensions in both directions.
VB.NET:
e.Graphics.DrawImage(ship, -ship.Width \ 2, -ship.Height \ 2)
With TranslateTransform method you can move the world center:
VB.NET:
e.Graphics.TranslateTransform(200, 200, Drawing2D.MatrixOrder.Append)
 
Ah yes, I ended up figuring it out but didn't post it.
I actually used a similar method, I made a function called RotateAround.
This translates the image to 0, 0 minus half the x dimension and y dimension and then rotates it. Then it is translated back to its previous position.
 
Back
Top