-
In my game you shoot bad guys with lasers. I want to see if there is a easy way of testing if the laser hit the target or not. Is there some kind of:
If line1 is touching shape1 then
sort of thing???
If you can tell me if there is please tell me. If there isent one can you please tell me the best way to do this? This game will be mutiplayer it will be the next number one game! :D
-
Woah! :eek: The story in yet game is incredible!!! Better than any DeusEx or whatever!
Erm - enough of laughing :D
Try this:
Code:
Dim Laser As RECT
Dim Shape As RECT
If Laser.x + Laser.w > Shape.x Then
If Laser.y + Laser.h > Shape.y Then
If Laser.x < Shape.x + Shape.w Then
If Laser.y < Shape.y + Shape.h Then
'Code for hit here!
EndIf
EndIf
EndIf
EndIf
-
-
Kedaman, if someone asks such a question I think rectangular collision is good enough :rolleyes:
Or prolly a circle.. ask if you want to collide circles :)
-
yeah maybe, but i guess it depends, if the laser line is long enough i would treat it as a line
-
Hey Fox,...
what does this do :
Dim Laser As RECT <--------here :confused:
Dim Shape As RECT <--------here :confused:
If Laser.x + Laser.w > Shape.x Then
If Laser.y + Laser.h > Shape.y Then
If Laser.x < Shape.x + Shape.w Then
If Laser.y < Shape.y + Shape.h Then
'Code for hit here!
EndIf
EndIf
EndIf
EndIf
My lasar is line1 and my shape is shape1,.. explian the vareibles to me please!
-
A RECT type has the x & y coordinates of the top-left corner of a rectangle, and either the coords of the bottom-left corner or the height and width of the rectangle. It's just a structure to store a rectangle in.
Anyway, like Kedaman said, what kind of shape is it? A circle?
-
its a square!
Its a sqare... But my laser is line1 and my shape is shape1 ,.... what does darclaring those vars do??:confused:
-
More trouble
When i try to run that code the VB program highlights " Laser As RECT" from Dim Laser As RECT and says "Compile error: User-defined type not defined"
whats does that mean?
-
It means you must declare the Type, as it is an extended member of the normal VB types:
Code:
Type RECT
Left as Integer
Top as Integer
Right as Integer
Bottom as Integer
End Type
Then make sure the scalemode is 3 - Pixels (vbPixels)
Then you would put .X1 in left, .Y1 in top, .X2 in right, .Y2 in bottom (for the line).
For the shape you would put .Top in Top, .Left in Left, .Left + .Scalewidth in Right, and .Top + .Scaleheight in Bottom. Then you can check the collision.
-
Since I didn't really use the original RECT type you should either include this instead:
Code:
Type RECT
x As Integer 'x-pos
y As Integer 'y-pos
w As Integer 'width
h As Integer 'height
End Type
...or replace x by left, y by top and so on in my code.
It does not depend wheter your laser is just a line or a rectangle, use this code will work in both cases (better said: if you decide to switch to a rectangular (bitmap) laser, the code will still work perfectly) :)