Results 1 to 16 of 16

Thread: Rotating a graphic

  1. #1

    Thread Starter
    Lively Member jamieoboth's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    116

    Rotating a graphic

    I am making a game in which you play a tank. I was wondering, how do you get the graphic to rotate? I have looked at tip 104 in VBworld, but that wasn't any help at all. Please help!
    Ich widerstehe allem - nur nicht der Versuchung

    (I can resist anything but temptation)

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    This may help (from vbcode)

    VB Code:
    1. Const SRCCOPY = &HCC0020
    2. Const Pi = 3.14159265359
    3. Private Declare Function SetPixel Lib "GDI32" (ByVal hDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal crColor As Long) As Long
    4. Private Declare Function GetPixel Lib "GDI32" (ByVal hDC As Integer, ByVal X As Integer, ByVal Y As Integer) As Long
    5. Private Declare Function StretchBlt% Lib "GDI32" (ByVal hDC%, ByVal X%, ByVal Y%, ByVal nWidth%, ByVal nHeight%, ByVal hSrcDC%, ByVal XSrc%, ByVal YSrc%, ByVal nSrcWidth%, ByVal nSrcHeight%, ByVal dwRop&)
    6.  
    7. '  add three command buttons and two pictureboxes. Load a bitmap into picture1 in design
    8. '  mode. Set both box to the same size. Routines execute 3 times faster than routines
    9. '  found in Microsoft's Knowledge Base.
    10.  
    11.  
    12. 'Sub Form_Load ()
    13. Picture1.ScaleMode = 3
    14. Picture2.ScaleMode = 3
    15. 'End Sub
    16.  
    17. 'Sub Command1_Click ()
    18. 'flip horizontal
    19. picture2.Cls
    20. px% = picture1.ScaleWidth
    21. py% = picture1.ScaleHeight
    22. retval% = StretchBlt(picture2.hDC, px%, 0, -px%, py%, picture1.hDC, 0, 0, px%, py%, SRCCOPY)
    23. 'End Su
    24.  
    25. 'Sub Command2_Click ()
    26.                 'flip vertical
    27.                 picture2.Cls
    28.                 px% = picture1.ScaleWidth
    29.                 py% = picture1.ScaleHeight
    30.                 retval% = StretchBlt(picture2.hDC, 0, py%, px%, -py%, picture1.hDC, 0, 0, px%, py%, SRCCOPY)
    31.                'End Sub
    32.  
    33.                'Sub Command3_Click ()
    34.                 'rotate 45 degrees
    35.                 picture2.Cls
    36.                 Call bmp_rotate(picture1, picture2, 3.14 / 4)
    37.                'End Sub
    38.  
    39.  
    40. 'Sub bmp_rotate (pic1 As PictureBox, pic2 As PictureBox, ByVal theta!)
    41.   ' bmp_rotate(pic1, pic2, theta)
    42.   ' Rotate the image in a picture box.
    43.   '   pic1 is the picture box with the bitmap to rotate
    44.   '   pic2 is the picture box to receive the rotated bitmap
    45.   '   theta is the angle of rotation
    46.   Dim c1x As Integer, c1y As Integer
    47.   Dim c2x As Integer, c2y As Integer
    48.   Dim a As Single
    49.   Dim p1x As Integer, p1y As Integer
    50.   Dim p2x As Integer, p2y As Integer
    51.   Dim n As Integer, r   As Integer
    52.  
    53.   c1x = pic1.ScaleWidth \ 2
    54.   c1y = pic1.ScaleHeight \ 2
    55.   c2x = pic2.ScaleWidth \ 2
    56.   c2y = pic2.ScaleHeight \ 2
    57.   If c2x < c2y Then n = c2y Else n = c2x
    58.   n = n - 1
    59.   pic1hDC% = pic1.hDC
    60.   pic2hDC% = pic2.hDC
    61.  
    62.         For p2x = 0 To n
    63.             For p2y = 0 To n
    64.           If p2x = 0 Then a = Pi / 2 Else a = Atn(p2y / p2x)
    65.           r = Sqr(1& * p2x * p2x + 1& * p2y * p2y)
    66.         p1x = r * Cos(a + theta!)
    67.         p1y = r * Sin(a + theta!)
    68.         c0& = GetPixel(pic1hDC%, c1x + p1x, c1y + p1y)
    69.         c1& = GetPixel(pic1hDC%, c1x - p1x, c1y - p1y)
    70.         c2& = GetPixel(pic1hDC%, c1x + p1y, c1y - p1x)
    71.         c3& = GetPixel(pic1hDC%, c1x - p1y, c1y + p1x)
    72.         If c0& <> -1 Then xret& = SetPixel(pic2hDC%, c2x + p2x, c2y + p2y, c0&)
    73.         If c1& <> -1 Then xret& = SetPixel(pic2hDC%, c2x - p2x, c2y - p2y, c1&)
    74.         If c2& <> -1 Then xret& = SetPixel(pic2hDC%, c2x + p2y, c2y - p2x, c2&)
    75.         If c3& <> -1 Then xret& = SetPixel(pic2hDC%, c2x - p2y, c2y + p2x, c3&)
    76.       Next
    77.     t% = DoEvents()
    78.    Next
    79. 'End Sub
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  3. #3

    Thread Starter
    Lively Member jamieoboth's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    116
    No, not really. You know in the GTA series, where the vehicles are in a 3d environment, but the cars aren't actually 3d? well thats what Im doing (with a few changes, of course). I just need (in simpleton terms, cos I'm not too bright ) a piece of code that rotates the one graphic so that when a key is pressed (I know how to do that), it will rotate until the key is let go
    Thanks for your help anyway, but any other ideas?

    James
    Ich widerstehe allem - nur nicht der Versuchung

    (I can resist anything but temptation)

  4. #4
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    timer

    Use SLH's reply and attach it to a timer instead of command buttons
    Don't pay attention to this signature, it's contradictory.

  5. #5
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    timer

    Use SLH's reply and attach it to a timer, and have a variable increase (to make it spin faster or whatnot) when you press a key instead of command buttons (di I post this twice? stupid slow comp...)
    Don't pay attention to this signature, it's contradictory.

  6. #6
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Hello folks,

    I was doing a search for a sub for rotating a bitmap through any angle and ran into the nice code above. I've tested it with a timer control for values of theta starting at 0 and increasing in steps of 0.1 radians.
    The problem is, for some reason, it sometimes produces run-time error #6 (overflow). But the funny thing is, sometimes it works, sometimes it doesn't, without any changes in between.

    The line the overflow occurs most often at (i.e. gets highlighted) is

    c0& = GetPixel(pic1hdc&, c1x + p1x, c1y + p1y)

    Also at this line:

    If c0& <> -1 Then xret& = SetPixel(pic2hdc&, c2x + p2x, c2y + p2y, c0& )

    where the SetPixel part is highlighted.

    I've just tried it 5 times in a row and while the first, second and fifth gave error 6, the third and fourth were successful.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  7. #7
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Oops, the SetPixel and Getpixel declarations have integers instead of longs... no wonder I was getting overflows.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  8. #8
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Just wanted to drop by and tell you that it is much fater to rotate that pic at design time, then it is at runtime. So if you are looking for speed thats the way to go...

  9. #9
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by NoteMe
    Just wanted to drop by and tell you that it is much fater to rotate that pic at design time, then it is at runtime. So if you are looking for speed thats the way to go...
    Well, the thing is the end user will have to apply the rotation to a scanned image so I can't guess beforehand how much it will have to be rotated by. But thanks for the hint.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  10. #10
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    Very true NoteMe, way faster to do as design time.

    If you are lazy and don't want to rotate your pic and then save, 360 times....just use the above code and rotate it through every angle and save the new image into a bmp...You can save DC to bmp I know. Do that once, and bam you have 360 angles of your image.....

    Seriously it is super laggy to rotate an image, I was using code for a 2D top view shooter, to have the player face the angle of the line he was walking...requires a rotate each click, time_Delta went from 20ms to 220ms during the second it was rotating...
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  11. #11
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    Well you don't have to rotate it 360 times for it to look fluid, I suppose you could, but it shouldn't be necessary. Also, I remember someone made something that was floating around my computer a while ago that rotated and saved the images automatically for you.

  12. #12
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by dsheller
    Well you don't have to rotate it 360 times for it to look fluid, I suppose you could, but it shouldn't be necessary. Also, I remember someone made something that was floating around my computer a while ago that rotated and saved the images automatically for you.
    That was me and Cyborg...

  13. #13
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    Yah I designed my own one to, but the code came for the rotation came from that example you always use to show of DD Rotation from PSC...Not sure of the URL.

    I understand what the rotation code does, I just don't get cos/sin ****, I need to sit down and study that again or something, i was either high or skippin school during those lessons...
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  14. #14
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Actualy it was a friend of mine at an other forum that made that GTA DD rotation demo...I just improved the speed with using pre designed sprites...

  15. #15
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    I don't mind doing the rotation on the fly since my application is not about animation.

    For animation it's true that precomputed images make a difference. However I think the subroutine above would be faster (than it is now, not than precomputing the images) if a table of sin & cos were precalculated and then linear interpolation used.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  16. #16
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    That is true. Even the Quack 2 engine used a look up table for sin cos. And it is not hard to make. And if you know a bit of cach misses and stuff like that too, you can make that look up table even faster...

Posting Permissions

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



Click Here to Expand Forum to Full Width