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?
How is it one careless match can start a forest fire, but it takes a whole box to start a campfire?
I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
Posts
1,457
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:
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
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 ) 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.
To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systemshere.
Jotaf's Theories!
"Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."
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.
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.
How is it one careless match can start a forest fire, but it takes a whole box to start a campfire?
I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
Posts
1,457
Oops sorry, I replied in the wrong post hehe
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
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
To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systemshere.
Jotaf's Theories!
"Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."
I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
Posts
1,457
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
To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systemshere.
Jotaf's Theories!
"Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."