Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 09-14-2004, 09:19 PM   #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!
Xeris is offline   Reply With Quote
Old 09-14-2004, 10:03 PM   #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?
Xeris is offline   Reply With Quote
Old 09-16-2004, 11:20 AM   #3
aym
Registered User
 
aym's Avatar
 
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
(top1 > bottom2) checks wether first box is below the second box, if true, there is no collision.

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.
aym is offline   Reply With Quote
Old 09-16-2004, 06:05 PM   #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!")
Thanks so much man!
Xeris is offline   Reply With Quote
Old 09-16-2004, 06:43 PM   #5
aym
Registered User
 
aym's Avatar
 
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)
Value of epsilon depends on your game, experiment with it until you get it right.

Good luck
aym is offline   Reply With Quote
Old 09-16-2004, 08:50 PM   #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?
Xeris is offline   Reply With Quote
Old 09-17-2004, 08:01 AM   #7
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
"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.
doctorgonzo is offline   Reply With Quote
Old 09-17-2004, 09:59 AM   #8
aym
Registered User
 
aym's Avatar
 
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)
aym is offline   Reply With Quote
Old 09-18-2004, 09:43 PM   #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
Do you have to make variables for them or somthing? Because when I entered the code there was an error saying somthing like "expected Label or segment... or somthing like that" I am still new to Visual basic so if theres somthing that you thought I would know... I prolly wont
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!
Xeris is offline   Reply With Quote
Old 09-19-2004, 07:42 AM   #10
aym
Registered User
 
aym's Avatar
 
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.
aym is offline   Reply With Quote
Old 09-19-2004, 03:44 PM   #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
I didnt try it yet, Ill try it tomorrow in class... Is this what you ment tho? Or do I have to make Variuables? I was looking through the book and they had stuff like "X = Today is" and stuff like that... Do I have to do somthing like that?
Xeris is offline   Reply With Quote
Old 09-20-2004, 09:40 AM   #12
aym
Registered User
 
aym's Avatar
 
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
Same for Shape2.

However, if you haven't read about variables yet, I suggest you wait till you do so
aym is offline   Reply With Quote
Old 09-22-2004, 09:11 AM   #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...
Xeris is offline   Reply With Quote
Old 09-22-2004, 09:38 AM   #14
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
It looks like you are comparing Shape1's horizontal position to Shape2's vertical position, which won't work.
doctorgonzo is offline   Reply With Quote
Old 09-22-2004, 09:58 AM   #15
aym
Registered User
 
aym's Avatar
 
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
This should work for moving Shape1 to the left.

Last edited by aym; 09-22-2004 at 10:00 AM.
aym is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:53 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2