Note: You must be registered in order to post a reply.
|
Dizzee |
Posted - May 18 2006 : 4:55:25 PM Ahh, I'm stuck on my game again!! Ive tried everything that I can think of, and I dont get some of the collision detection tutorials!
Ok, so I want to have a collision detection when my bullets hit the cop cars, and something to recgonize when a cop car hits another car, so I can avoid them from hitting!
Help please!!
Here is the game so far: [url=http://www.exoload.com/view.php?file=http://exoload.net/uploads/506/1147989224.rar&title=/uploads/506/1147989224.rar][/url]
oh and another thing, for some reason you have to hit the start buttons twice!! me and my teacher could not figure this out!! |
Dizzee |
Posted - May 19 2006 : 7:28:23 PM Thanks for all your help! But I ended up figure out the collision on my own:
If bullet(j).Top >= squad(b).cYloc And bullet(j).Top + bullet(j).Height <= squad(b).cYloc + 51 And bullet(j).Visible = True Then If bullet(j).Left >= squad(b).cXloc And bullet(j).Left + bullet(j).Width <= squad(b).cXloc + 100 Then score = score + 100 bullet(j).Visible = False squad(b).health = squad(b).health - 50 End If End If
and that works great!
also I got rid of timer 1 and put it in the main loop
and thanks PW for fixing the 2 click start button problem! I will check it out right now! Wow you guys are awesome!! |
PW7962 |
Posted - May 19 2006 : 4:51:31 PM Ok so well I have seen a few problems in your code as I have looked at it, and I fixed it up where I thought it would help.
I put in a very simple detection (you send the function the x,y of bullet then it checks it with each squad) and I have found some interesting bugs with it (idk if this happens with walrus's collision detection, use whichever one works for you), but it still works sort of.
I fixed your problem with the 2 clicks on the main menu. What was happening was that VB needs to wait until the form is done loading (where you did the initialization and stuff) until the form acutally shows up. So I just added a DoEvents and Me.Show and thats it.
I added a simple score thingy just to test the collision and stuff (which is where the one bug comes in: you don't even fire and the squad dies )
I noticed that you had the bullet timer set to 1, which I have seen in many places that timers become inaccurate around 100ms, so I put it into a GetTickCount loop, which is much more accurate.
I also cleaned up the main form code by putting the initializations and main loop into seperate subs in your module.
I'm not saying you have to use my code but if you'd like you can get it here -> http://s5.exoload.com/uploads/506/1148075427.zip |
Walrus |
Posted - May 19 2006 : 4:22:02 PM KingKirb's Collided function only works for two Image controls, but in your code, bullets are Picturebox controls and cops are drawn using BitBlt.
Try like this:
Dim rctBullet As tRect rctBullet.Left = bullet(0).Left rctBullet.Right = bullet(0).Left + 9 rctBullet.Top = bullet(0).Top rctBullet.Bottom = bullet(0).Top + 3 Dim rctCop As tRect rctCop.Left = squad(0).cXloc rctCop.Right = squad(0).cXloc + 100 rctCop.Top = squad(0).cYloc rctCop.Bottom = squad(0).cYloc + 51 Dim rctDest As tRect If IntersectRect(rctDest, rctBullet, rctCop) Then DoSomething
This only checks for collision between bullet(0) and squad(0). Modify it to check all bullets and all cop cars.
Or, instead of using IntersectRect, you could add sth like this to Timer_Timer:
bullet(j).Left = bullet(j).Left + 10 for k = 0 to 9 If bullet(j).Visible = True And bullet(j).Left > squad(k).cXloc And _ bullet(j).Left < squad(k).cXloc + 100 And _ bullet(j).Top > squad(k).cYloc And _ bullet(j).Top < squad(k).cYloc + 51 Then DoSomething next k ...
Btw, this version treats bullets as if they were points, eventhough they are actually small rectangles.
Now, let's have a look at the other problem. Have you tried stepping through the code using the "Step Into" button (or the Debug->Step Into menu)? If not, here's what I advise you to do:
1.Set Timer's Enable property to false 2.In Form_Load, comment out the part of the code that moves&draws cops. 3.Step 1 and 2 aren't really necessary and don't really have anything to do with the actual problem, they'll just make things a bit easier. 4.Click the "Step Into" button, then "Start Game", then keep clicking "Step Into" and you'll step through the program line by line.
This way, I hope you'll begin to see what's going on.
|
Dizzee |
Posted - May 19 2006 : 1:30:46 PM thats not working for me, i think its because im BltBlting one thing, but not the other?? |
kingkirb |
Posted - May 19 2006 : 08:22:32 AM heya ok i think i know how to dobasic collisions. cant get ur code goin so im not sure if this is what u are wanting but here i go anyway. ok first make a new module(you might want this in a new project to get used to it) and put in
Type tRect Left As Long Top As Long Right As Long Bottom As Long End Type
Declare Function IntersectRect Lib "user32" (lpDestRect As tRect, lpSrc1Rect As tRect, lpSrc2Rect As tRect) As Long
then in your form put in this code
Private Function Collided(imgA As Image, imgB As Image) As Integer Dim A As tRect Dim B As tRect Dim ResultRect As tRect
A.Left = imgA.Left A.Top = imgA.Top B.Left = imgB.Left B.Top = imgB.Top A.Right = A.Left + imgA.Width - 1 A.Bottom = A.Top + imgA.Height - 1
B.Right = B.Left + imgB.Width - 1 B.Bottom = B.Top + imgB.Height - 1
Collided = IntersectRect(ResultRect, A, B) End Function
then its easy form here. an example
If Collided(Image1, Image2) Then
pretty easy once its in. BTW i have had problems when using this code in conjunction to using key moves(where you makes images move when you push arrow keys) so thats why i suggest a new form save screwin up your game. hope this helps. J(kingkirb) |
Dizzee |
Posted - May 18 2006 : 4:57:43 PM bb code doesnt work here! http://exoload.net/uploads/506/1147989224.rar ^^ direct link! |
|
|
VBGamer |
© |
|
|
|
|