|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (6 bit)
Join Date: Aug 2004
Posts: 48
|
Visual Basic, NEW
Hello. I am Very new to Microsoft Visual basic. I just started to take a class on it and know very little to nothing about the program. I am not really in it to get a career in programing... I just want to make little games and cool stuff with it. The coolest thing I did myself was to make Shapes Move...
I am in the middle of making a small game, but I cant figure out how to make one shape not be able to move through another... This is like my problem. [] <--- Shape1 l-----------l l-----------l <---- Shape 2 Right now when I make shape1 move by using a command button... it goes right through Shape 2. What is the code to make it where it cant go through? And is the code in shape 2 itself, or the whole file Program? Thanks a bunch! |
|
|
|
|
|
#2 |
|
Member (6 bit)
Join Date: Aug 2004
Posts: 48
|
This might be a better example...
l [] l l - l l - l The [] is the shape... and the -'s are its path that l - l You can move it. l - l_______ l [] - - - -------------- Right now the box can do the + path + + how can you make it where it will not let you through the [] wall (I have shapes as walls), but where you have to Turn it right? |
|
|
|
|
|
#3 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
What you are trying to do is called collision detection, in this case, it's between 2 boxes in 2D space.
Let's say that you represent a box in your program as 4 values, top, left, bottom, and right. So first box is top1, left1, bottom1, right1. And the second box is top2, left2, bottom2, right2. Now to check whether the 2 boxes collide: Code:
If (top1 > bottom2) or (bottom1 < top2) or (right1 < left2) or (left1 > right2) then
(no collision, move boxes)
else
(boxes collide, don't move them)
endif
Same for the rest: (bottom1 < top2) second box is below the first box. (right1 < left2) first is to the right of the second. (left1 > right2) second is to the left of the first. I haven't programmed anything in Basic for ages, hope my syntax is correct. |
|
|
|
|
|
#4 |
|
Member (6 bit)
Join Date: Aug 2004
Posts: 48
|
You are awsome man!!
Ive been trying to find that out for days! Ive posted it everywhere... I have a class on it every day in highschool... but its stupid... I think I know more then my teacher does... thanks so much... Im going to try it as soon as I get in class tomorrow. I also have another question that you might be able to answer... it sounds like you know a lot. im making a lil rpg like game.... Thats why I needed to know that code, so people cant wakl right through the dungeon walls. I know from looking in a book that you can make messages and stuff like that pop up. I sucessfully made message boxes pop up when you clicked a mouse, but is there a way to have a message box pop up when you walk twords a shape? thats how I was thinking of making my fights... ex- [] <-- Guy ----- <-- hallway {} <--Invisible Shape (Monster) ------------------- [] {} ------------------- I was thinking of when [] steps on the invisible Shape, a message bow will appear saying, "Fight!" Will this work with the same type code as the walls? Like Code:
If (Guy1.Top > Monster) then
(No msgBox)
else
(msgBox ("Fight!")
|
|
|
|
|
|
#5 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
One possible way to see whether the player is close to a monster or a wall, is the following formula:
Code:
(player.top - monster.top < epsilon) and (player.left - monster.left < epsilon) Good luck
|
|
|
|
|
|
#6 |
|
Member (6 bit)
Join Date: Aug 2004
Posts: 48
|
Hey... thanks for everything! Im going to try it first thing tomorrow. I have one more question though. Im trying to learn a lot about Visual basic... there was some programs that I made from just copying codes and stuff... but I didnt learn anything. I am trying to understand what everything means... Um... what is epslion?
|
|
|
|
|
|
#7 |
|
Professional gadfly
|
"Epsilon" is used in math meaning "an arbitrarily small number." In your code, the smaller epsilon is, the closer you will have to be to the monster for the code to trigger. As aym said, use different values for epsilon until you find something that works well.
|
|
|
|
|
|
#8 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
After reading my post here, I forgot to add calls to the function Abs, in case monster.top is larger than player.top, and the same for left:
Code:
(abs(player.top - monster.top) < epsilon) and (abs(player.left - monster.left) < epsilon) |
|
|
|
|
|
#9 |
|
Member (6 bit)
Join Date: Aug 2004
Posts: 48
|
Hey, when you said that code,
Code:
If (top1 > bottom2) or (bottom1 < top2) or (right1 < left2) or (left1 > right2) then
(no collision, move boxes)
else
(boxes collide, don't move them)
endif
My teacher doesnt know much about it but she said that I prolly have to make variables or somthing, that the computer prolly doesnt reconize the line (no collision, move boxes) because thats where the error was. Is there anything els I have to type before this code? thanks a lot for everything!
|
|
|
|
|
|
#10 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
You need to replace top, bottom, left and right with your variable names, how do you represent the 2 shapes in your program?
And replace "(no collision, move boxes)" with the code that moves the shapes, and ignore the else. |
|
|
|
|
|
#11 |
|
Member (6 bit)
Join Date: Aug 2004
Posts: 48
|
2 shapes would just be Shape1 and Shape2 right? I didnt make any variuable yet... I dont really understand them...
I Just started to learn the program. Would this code be right?Code:
If Shape1.left < Shape2.Left then
Shape1.Left = Shape1.Left + 100
else
Shape1.Left = Shape1.Left + 0
end if
|
|
|
|
|
|
#12 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
I believe objects in VB have top, width and height properties too, but I'm not sure.
As for "right", it's (left + width), and "bottom" is (top + height) Code:
*------------------------->left | | |--width----| | top | - left O-----------* | | | | | | | | | height | | | | | | | | | | | - *-----------O | bottom | right | /top Code:
left1 = Shape1.left right1 = Shape1.left + Shape1.width top1 = Shape1.top bottom1 = Shape1.top + Shape1.height However, if you haven't read about variables yet, I suggest you wait till you do so
|
|
|
|
|
|
#13 |
|
Member (6 bit)
Join Date: Aug 2004
Posts: 48
|
Ok... Im getten mad at this stupid program...
What is wrong with this code? I still am just trying to stop the shape from going through the other one...If Shape1.Left + Shape1.Width > Shape2.Top + Shape2.Height Then Shape1.Left = Shape1.Left + 100 Else Shape1.Left = Shape1.Left + 0 End If (Sorry not in code form... the school comps wont let it for some reason...) There is no error... But when I click the shape right... it still moves right through the wall... I have tried putting this code in the command button that makes this shape move right, and also in the form codeing window... It still doesnt work... |
|
|
|
|
|
#14 |
|
Professional gadfly
|
It looks like you are comparing Shape1's horizontal position to Shape2's vertical position, which won't work.
|
|
|
|
|
|
#15 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
And you should check for the new position, (Shape1.left + 100) for example, if there is no collision, you can move the shape:
Code:
if Shap1.left + 100 + Shape1.width < Shape2.left then
Shape1.left = Shape1.left + 100
endif
Last edited by aym; 09-22-2004 at 10:00 AM. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|