VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Image Collision

by Mathieu Tremblay (1 Submission)
Category: Miscellaneous
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

Verify if two images will hit if you do move one of the image of X left and X right. Very useful when programming actions, sports or rpg games.

Inputs
MovingImage = the image to move moveLeft = the Left movement of the MovingImage moveTop = the Top movement of the MovingImage StaticImage = the image you don't want to hit
Code Returns
Return if the two images would hit ( True or False ).
Side Effects
The MovingImage will automaticly move if you don't erase the two folling lines of code: MovingImage.Left = MovingLeft MovingImage.Top = MovingTop

Rate Image Collision

Public Function CollisionMovingImage(MovingImage As Variant, moveLeft As Integer, moveTop As Integer, Optional StaticImage As Variant) As Boolean

On Error GoTo ErrHandler:
'If one of the parameters is not found or
'some error happen in the function, it will
'then exit.
  
  Dim MovingLeft, MovingRight, MovingTop, MovingBottom As Integer
  'The Moving variables are used to get infos about the
  'MovingImage.
  MovingLeft = MovingImage.Left + moveLeft
  MovingRight = (MovingImage.Left + moveLeft) + MovingImage.Width
  MovingTop = MovingImage.Top + moveTop
  MovingBottom = (MovingImage.Top + moveTop) + MovingImage.Height
  
  Dim okLeft, okTop As Boolean
  ' okLeft is use to see if the MovingImage has a point
  ' of its width in commun with the StaticImage. The
  ' okTop is used to see if it happens with the height.
  okLeft = True
  okTop = True
  'They are set to true by default to allow the moving
  'of the MovingImage if there is no StaticImage.
  
  
  If IsMissing(StaticImage) = False Then
  'Execute the verification only if the
  'StaticImage argument is used.
  
    Dim StaticLeft, StaticRight, StaticTop, StaticBottom As String
    'The Static variables are used to get infos about
    'the StaticImage.
    StaticLeft = StaticImage.Left
    StaticRight = StaticImage.Left + StaticImage.Width
    StaticTop = StaticImage.Top
    StaticBottom = StaticImage.Top + StaticImage.Height
  
    Dim i As Integer
    'Verify if the MovingImage has a point
    'of its width in commun with the StaticImage.
    For i = StaticLeft To StaticRight
      If (MovingLeft = i) Or (MovingRight = i) Then
        okLeft = False
      End If
    Next i
    
    'Verify if the MovingImage has a point of
    'its height in commun with the StaticImage.
    For i = StaticTop To StaticBottom
      If (MovingBottom = i) Or (MovingTop = i) Then
        okTop = False
      End If
    Next i
        
    'Don't move the MovingPicture if there
    'would be a collision.
    If okTop = False And okLeft = False Then
      'Return true because the two objects
      'would have a commun point.
      CollisionMovingImage = True
      GoTo ErrHandler:
    End If
    
  End If
  
  'Move the MovingImage...
  'You could remove the two following lines if you
  'wanted the function to only tell you if there would
  'be a collision or no.
  MovingImage.Left = MovingLeft
  MovingImage.Top = MovingTop
  'Return false because there have been no collision
  CollisionMovingImage = False
  
ErrHandler:

End Function

Download this snippet    Add to My Saved Code

Image Collision Comments

No comments have been posted about Image Collision. Why not be the first to post a comment about Image Collision.

Post your comment

Subject:
Message:
0/1000 characters