-
Game Help Please
Hi,
I've written a game, which is playable but I need to fix a few minor problems. One of them is in the collision detection.
I have an image(a fly) popping up at random around the screen and when the Frog collides with it, points are supposed to be added to the score and a sound plays. Problem is, the points and sound are playing when the frog is nowhere near the fly, then when the frog gets the fly it doesn't always work.
Here's the code I've used:
Code:
Dim GameTop As Single
Dim GameLeft As Single
'Code for Bonus Fly
Randomize
With imgBonusFly
GameTop = Rnd * (Height - .Height)
GameLeft = Rnd * (Width - .Width)
.Move GameLeft, GameTop
End With
'Froggy gets Fly
If imgBonusFly.Left < imgFrog(FrogIndex).Left + imgFrog(FrogIndex).Width _
And imgBonusFly.Left + imgBonusFly.Width > imgFrog(FrogIndex).Left Then Bonus
Could someone show me where I've gone wrong?
-
Hmm weird, I was gonna tell you to normalize the vector and then do a whole bunch of complicated stuff, but hey, I think that this should work too:
MouseCos = (MouseX - PlayerX) / Sqr((MouseX - PlayerX) ^ 2 + (MouseY - PlayerY) ^ 2))
MouseSen = (MouseY - PlayerY) / Sqr((MouseX - PlayerX) ^ 2 + (MouseY - PlayerY) ^ 2))
Don't ask. I inverted the formulae I use to find a point in a circle based on the position and radius of the circle, and an angle, to find the Cosine and Sine ;)
Hey wait! I just found a formulae to normalize a vector!!! YES!!! Thank you thank you thank you thank you :D :D :D
I was looking for this for quite some time. Oh well. All I need now is to find out what the hell is the "dot product" of a vector (any idea?). Damn C++ articles for D3D, never try to use them in 2D you never find the functions you need...
Oops sorry. Ok back to your question... now you normalized the vector of the mouse (duh I ended up doing the same thing :rolleyes: ) all you have to do is convert the Cos and Sen you have into a usable angle (in degrees). Maybe someone can help me in this one, I don't think I know how to do it... I'll do a little bit of research and post here again in a few hours.
-
Are you trying to do a box collision??
Do something like this:
function isColliding(SrcRect, DestRect) As Boolean
Dim Colliding as boolean
Colliding = True
if SrcRect.Left > DestRect.Right And
SrcRect.Top > DestRect.Bottom And
SrcRect.Right < DestRect.Left And
SrcRect.Bottom < DestRect.Top Then
Colliding = False
End If
isColliding = Colliding
End Function
Or use the API Call "IntersectRect" to intersect the rectangles to see if they collide and if they do, it will return the parts of the first rect that collides with the second rect.
If you are looking for a circle collision or some odd shape collision detection, then you will have to do loads of work (kind of like what Jotaf is talking about). Cosines and Sines!! Just Math.
-
Erm, Jotaf98, you lost me there...
I've tried using a couple of different collision methods, but they don't seem to work. When the games first starts, if the fly pops up on the starting position of the frog then the collision is detected, no problem, but once the frog starts to move around the game area the collision detection stops.
-
Oops sorry, I replied in the wrong post hehe :D
Actually I replied to the post, forgot, then replied again but in this one. It was late and I was sleepy this kind of things happen a lot under these conditions :rolleyes:
Anyways... there's a problem with your code but it's pretty obvious, I'm sure you already noticed it... you're only testing in the X axis (Left and Width), you should also do the same in the Y axis (Top and Height). You should use MoMad's code if you're using RECTs. If not, use this one:
'Froggy gets Fly
If imgBonusFly.Left < imgFrog(FrogIndex).Left + imgFrog(FrogIndex).Width _
And imgBonusFly.Left + imgBonusFly.Width > imgFrog(FrogIndex).Left _
And imgBonusFly.Top < imgFrog(FrogIndex).Top+ imgFrog(FrogIndex).Height _
And imgBonusFly.Top + imgBonusFly.Height > imgFrog(FrogIndex).Top Then Bonus
-
1 Attachment(s)
Hi,
Still not working. It should, I can't see anything wrong.
I've attached the form, maybe you can see something.
-
Hmm sorry, but the images are missing... put the images, the form and the vbp file in a single zip file and upload it here :)
-
It's OK, I've got it fixed now. I had the collision detection in the wrong place.