PacMan Wall Collision
PacMan Wall Collision
PacMan Wall Collision
PacMan Wall Collision
PacMan Wall Collision
PacMan Wall Collision PacMan Wall Collision PacMan Wall Collision PacMan Wall Collision PacMan Wall Collision PacMan Wall Collision PacMan Wall Collision PacMan Wall Collision
PacMan Wall Collision PacMan Wall Collision
PacMan Wall Collision
Go Back  Xtreme Visual Basic Talk > > > PacMan Wall Collision


Reply
 
Thread Tools Display Modes
  #1  
Old 05-28-2009, 08:40 PM
Photoshoper Photoshoper is offline
Newcomer
 
Join Date: May 2009
Posts: 2
Default PacMan Wall Collision


I'm having trouble with the code for my VB PacMan game my teacher wrote the right collision code but theres so much to it

If ((imgPacMan.Left + imgPacMan.Width >= imgWall(cntr).Left) And (imgPacMan.Left + imgPacMan.Width <= imgWall(cntr).Left + imgWall(cntr).Width)) And ((imgPacMan.Top <= imgWall(cntr).Top + imgWall(cntr).Height And imgPacMan.Top >= imgWall(cntr).Top) Or (imgPacMan.Top + imgPacMan.Height >= imgWall(cntr).Top And imgPacMan.Top + imgPacMan.Height <= imgWall(cntr).Top + imgWall(cntr).Height)) Then
'imgPacMan.Left = imgPacMan.Left - 100
End If

is there an easier way to do this because that code confuses me so much

oh and she just gave me the one for the right and told me to do the left, top, bottom by myself; not sure if this is the right thread my first post
Reply With Quote
  #2  
Old 05-28-2009, 11:25 PM
vb5prgrmrPacMan Wall Collision vb5prgrmr is offline
Senior Contributor

* Expert *
 
Join Date: Mar 2009
Posts: 1,087
Default

Welcome and yes this is the place.

Now, you can make your code easier to read and speed it up by declaring a few variables that holds the addition portion of your if statement...
Code:
Dim PacMansRight As Integer, PacMansLeft As Integer
Dim PacMansTop As Integer, PacMansBottom As Integer
PacMansRight = imgPacMan.Left + imgPacMan.Width
PacMansLeft = imgPacMan.Left
PacMansTop = imgPacMan.Top
PacMansBottom = imgPacMan.Top + imgPacMan.Height
(the other two not bolded added as a suggestion for ease of reading)
So then your if statement would look like...
Code:
If ((PacManRight >= imgWall(cntr).Left)...
Now from what I can see or guess about your code is that this long if statement is within a loop. So to speed up the loop even faster and to make it more readable lets declare some more variables...
Code:
Dim WallsLeft As Integer, WallsTop As Integer
Dim WallsRight As Integer, WallsBottom As Integer
Then inside your loop (example below is a for loop, adjust your code to fit).
Code:
Dim cntr As Integer
....
For cntr = 0 To imgWall.Count - 1
  WallsRight = imgWall(cntr).Left + imgWall(cntr).Width
  WallsBottom = imgWall(cntr).Top + imgWall(cntr).Height
  WallsLeft = imgWall(cntr).Left
  WallsTop = imgWall(cntr).Top
Then you can rewrite your if statement so it is easier to read...
Code:
If ((PacMansRigth >= WallsLeft) And (PacMansRight <= WallsRight)) _
And ((PacMansTop <= WallsBottom And PacMansTop >= WallsTop) _
Or (PacMansBottom >= WallsTop And PacMansBottom <= WallsBottom)) Then
But I have to wonder why your teacher made this if statement so (it must be something that I cannot work out in my head...).


Good Luck

Last edited by vb5prgrmr; 05-28-2009 at 11:27 PM. Reason: Readability
Reply With Quote
  #3  
Old 05-29-2009, 10:05 AM
Flamespewer Flamespewer is offline
Freshman
 
Join Date: Sep 2008
Posts: 35
Default

Basically the code she gave you would test collision between images. if your doing pacman, and you have time, i highly suggest you consider using a 2d-array to store the paths that are walkable and not walkable.

for example

lets say 1 is solid and 0 is not solid

Code:
1 1 1 1 1 1 1 1 1 
1 0 0 0 0 0 0 0 1
1 0 1 1 0 1 1 0 1
1 0 0 0 0 0 0 0 1
0 0 1 1 1 1 1 0 0 
1 0 1 1 1 1 1 0 1
1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1
so first put that into a 2d array and then when you move Pacman left check the number that is left of him like this
Code:
if MAP(row,col-1)<>1 then
    Pacman.left=pacman.left-32
    col=col-1
end if
you want pacman's position in the array to be expressed by row, and column variables so that you can easily check what is above, to the left, to the right, and below him.

this is fairly basic(although its sort of hard to explain) and is in my opinion a lot better for making a Pacman game than your collision detection. if you want to move the ghosts you can easily implement them in and they can use the same array to detect which ways they can not move.
Reply With Quote
  #4  
Old 05-29-2009, 01:54 PM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

heres a nice and originals collisions functions:
Code:
'Detect a precise collision by a position and size of 2 objects
Public Function CollisionPrecise(X1 As Long, Y1 As Long, Width1 As Long, Height1 As Long, X2 As Long, Y2 As Long, Width2 As Long, Height2 As Long) As Boolean
    If (X1 + Width1 >= X2 And X1 <= X2 + Width2) And (Y1 + Height1 >= Y2 And Y1 <= Y2 + Height2) Then
        CollisionPrecise = True
    Else
        CollisionPrecise = False
    End If
End Function

'Detect collision by 2 objects
Public Function Collision(Object1 As Object, Object2 As Object) As Boolean
    Dim X1 As Long
    Dim Y1 As Long
    Dim Width1 As Long
    Dim Height1 As Long
    Dim X2 As Long
    Dim Y2 As Long
    Dim Width2 As Long
    Dim Height2 As Long
    
    X1 = Object1.Left
    Y1 = Object1.Top
    Width1 = Object1.Width
    Height1 = Object1.Height
    X2 = Object2.Left
    Y2 = Object2.Top
    Width2 = Object2.Width
    Height2 = Object2.Height
    
    If (X1 + Width1 >= X2 And X1 <= X2 + Width2) And (Y1 + Height1 >= Y2 And Y1 <= Y2 + Height2) Then
        Collision = True
    Else
        Collision = False
    End If
End Function
they are very easy to use
but if you need help, just ask.
i hope help you

Last edited by Cambalinho_83; 05-29-2009 at 02:19 PM. Reason: Better code ;)
Reply With Quote
  #5  
Old 05-29-2009, 02:38 PM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

another advice: build boolean variables collisione for every directions....
why?
because when you collide with a wall, you go to opposite direction. but if you continue press the key, the pacman continue do the same.. isn't the right situation. but if you "block" the direction, the pacman only do these 1 time.
for example:
when you press left key, the pacman go to the left. but when catch the wall, the pacman go ot the righ(1 number of steps). and if you say tha LeftCollision=True, then until the LeftCollision<>False the pacman stay in same place(if you continue press the same key). when you move with others keys, you do LeftCollision=False).
i think that you catch what i mean.
i hope help you
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
PacMan Wall Collision
PacMan Wall Collision
PacMan Wall Collision PacMan Wall Collision
PacMan Wall Collision
PacMan Wall Collision
PacMan Wall Collision PacMan Wall Collision PacMan Wall Collision PacMan Wall Collision PacMan Wall Collision PacMan Wall Collision PacMan Wall Collision
PacMan Wall Collision
PacMan Wall Collision
 
PacMan Wall Collision
PacMan Wall Collision
 
-->