Results 1 to 2 of 2

Thread: Could someone please help with this Rotating RECT [Resolved]

  1. #1

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Resolved Could someone please help with this Rotating RECT [Resolved]

    Can someone please help me with the code below? I'm trying to rotate a RECT structure (Sprite(iSpriteIndex1).dstRect) and then create a new RECT (S1) that will be just the right size to contain the rotated RECT. I thought I had it but I'm getting some strange results and I can't figure out why.
    VB Code:
    1. Dim Angle As Single
    2. Dim XCenter As Single
    3. Dim YCenter As Single
    4. Dim BL As POINTAPI
    5. Dim TL As POINTAPI
    6. Dim BR As POINTAPI
    7. Dim TR As POINTAPI
    8.  
    9.  
    10. If Sprite(iSpriteIndex1).Angle = 0 Then
    11.     S1 = Sprite(iSpriteIndex1).dstRect
    12. Else
    13.     TempRect = Sprite(iSpriteIndex1).dstRect
    14.     XCenter = TempRect.Left + ((TempRect.Right - TempRect.Left - 1) / 2)
    15.     YCenter = TempRect.Top + ((TempRect.Bottom - TempRect.Top - 1) / 2)
    16.     Angle = Sprite(iSpriteIndex1).Angle + (Pi / 2)
    17.     If Angle >= 2 * Pi Then Angle = Angle - ((2 * Pi) * CInt(Angle / (2 * Pi)))
    18.     If Angle < 4.714 And Angle > 4.71 Then
    19.         Angle = 3 * Pi / 2
    20.     ElseIf Angle < 3.143 And Angle > 3.14 Then
    21.         Angle = Pi
    22.     ElseIf Angle < 6.284 And Angle > 6.282 Then
    23.         Angle = 2 * Pi
    24.     End If
    25.    
    26.     'Bottom Left
    27.     BL.x = XCenter + (TempRect.Left - XCenter) * Sin(Angle) + (TempRect.Bottom - YCenter) * Cos(Angle)
    28.     BL.y = YCenter + (TempRect.Bottom - YCenter) * Sin(Angle) - (TempRect.Left - XCenter) * Cos(Angle)
    29.     'Top Left
    30.     TL.x = XCenter + (TempRect.Left - XCenter) * Sin(Angle) + (TempRect.Top - YCenter) * Cos(Angle)
    31.     TL.y = YCenter + (TempRect.Top - YCenter) * Sin(Angle) - (TempRect.Left - XCenter) * Cos(Angle)
    32.  
    33.     'Bottom Right
    34.     BR.x = XCenter + (TempRect.Right - XCenter) * Sin(Angle) + (TempRect.Bottom - YCenter) * Cos(Angle)
    35.     BR.y = YCenter + (TempRect.Bottom - YCenter) * Sin(Angle) - (TempRect.Right - XCenter) * Cos(Angle)
    36.     'Top Right
    37.     TR.x = XCenter + (TempRect.Right - XCenter) * Sin(Angle) + (TempRect.Top - YCenter) * Cos(Angle)
    38.     TR.y = YCenter + (TempRect.Top - YCenter) * Sin(Angle) - (TempRect.Right - XCenter) * Cos(Angle)
    39.     'Set S1...
    40.     SetRect R1, TL.x, TL.y, BL.x, BL.y
    41.     SetRect R2, BL.x, BL.y, BR.x, BR.y
    42.     SetRect R3, BR.x, BR.y, TR.x, TR.y
    43.     SetRect R4, TR.x, TR.y, TL.x, TL.y
    44.     UnionRect S1, R1, R2
    45.     UnionRect S1, R3, S1
    46.     UnionRect S1, R4, S1
    47.     'S1 is now the RECT for the area needed to paint the first sprite
    48. End If

    I'd greatly appreciate any help anyone has.
    Last edited by Electroman; Sep 20th, 2004 at 09:11 AM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  2. #2

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    I sorted it out with this code instead:
    VB Code:
    1. If Sprite(iSpriteIndex1).Angle = 0 Then
    2.     S1 = Sprite(iSpriteIndex1).dstRect
    3. Else
    4.     TempRect = Sprite(iSpriteIndex1).dstRect
    5.     XCenter = TempRect.Left + ((TempRect.Right - TempRect.Left - 1) / 2)
    6.     YCenter = TempRect.Top + ((TempRect.Bottom - TempRect.Top - 1) / 2)
    7.     Angle = Sprite(iSpriteIndex1).Angle + (Pi / 2)
    8.     If Angle >= 2 * Pi Then Angle = Angle - ((2 * Pi) * CInt(Angle / (2 * Pi)))
    9.     If Angle < 4.714 And Angle > 4.71 Then
    10.         Angle = 3 * Pi / 2
    11.     ElseIf Angle < 3.143 And Angle > 3.14 Then
    12.         Angle = Pi
    13.     ElseIf Angle < 6.284 And Angle > 6.282 Then
    14.         Angle = 2 * Pi
    15.     End If
    16.    
    17.     'Bottom Left
    18.     BL.x = XCenter + (TempRect.Left - XCenter) * Sin(Angle) + (TempRect.Bottom - YCenter) * Cos(Angle)
    19.     BL.y = YCenter + (TempRect.Bottom - YCenter) * Sin(Angle) - (TempRect.Left - XCenter) * Cos(Angle)
    20.     'Top Left
    21.     TL.x = XCenter + (TempRect.Left - XCenter) * Sin(Angle) + (TempRect.Top - YCenter) * Cos(Angle)
    22.     TL.y = YCenter + (TempRect.Top - YCenter) * Sin(Angle) - (TempRect.Left - XCenter) * Cos(Angle)
    23.     'Bottom Right
    24.     BR.x = XCenter + (TempRect.Right - XCenter) * Sin(Angle) + (TempRect.Bottom - YCenter) * Cos(Angle)
    25.     BR.y = YCenter + (TempRect.Bottom - YCenter) * Sin(Angle) - (TempRect.Right - XCenter) * Cos(Angle)
    26.     'Top Right
    27.     TR.x = XCenter + (TempRect.Right - XCenter) * Sin(Angle) + (TempRect.Top - YCenter) * Cos(Angle)
    28.     TR.y = YCenter + (TempRect.Top - YCenter) * Sin(Angle) - (TempRect.Right - XCenter) * Cos(Angle)
    29.     'Set S1...
    30.     'R1 is made from TL and BR...
    31.     If TL.y < BR.y Then
    32.         R1.Top = TL.y
    33.         R1.Bottom = BR.y
    34.     Else
    35.         R1.Top = BR.y
    36.         R1.Bottom = TL.y
    37.     End If
    38.     If TL.x < BR.x Then
    39.         R1.Left = TL.x
    40.         R1.Right = BR.x
    41.     Else
    42.         R1.Left = BR.x
    43.         R1.Right = TL.x
    44.     End If
    45.    
    46.     'R2 is made from BL and TR...
    47.     If TR.y < BL.y Then
    48.         R2.Top = TR.y
    49.         R2.Bottom = BL.y
    50.     Else
    51.         R2.Top = BL.y
    52.         R2.Bottom = TR.y
    53.     End If
    54.     If BL.x < TR.x Then
    55.         R2.Left = BL.x
    56.         R2.Right = TR.x
    57.     Else
    58.         R2.Left = TR.x
    59.         R2.Right = BL.x
    60.     End If
    61.    
    62.     'S1 is made from R1 and R2...
    63.     If R1.Top < R2.Top Then
    64.         S1.Top = R1.Top
    65.     Else
    66.         S1.Top = R2.Top
    67.     End If
    68.    
    69.     If R1.Left < R2.Left Then
    70.         S1.Left = R1.Left
    71.     Else
    72.         S1.Left = R2.Left
    73.     End If
    74.    
    75.     If R1.Bottom > R2.Bottom Then
    76.         S1.Bottom = R1.Bottom
    77.     Else
    78.         S1.Bottom = R2.Bottom
    79.     End If
    80.    
    81.     If R1.Right > R2.Right Then
    82.         S1.Right = R1.Right
    83.     Else
    84.         S1.Right = R2.Right
    85.     End If
    86.    
    87.     'S1 is now the RECT for the area needed to paint the first sprite
    88. End If
    It seems it was something to do with the fact that Left was sometimes greater than Right and when that happens the Union API doesn't work all that well. Well the main thing this is part of is still throwing errors out so looks like I still got quite a bit of work left on it.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

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