[VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects
Go Back  Xtreme Visual Basic Talk > > > [VB6] - calculate the distance between 2 objects


Reply
 
Thread Tools Display Modes
  #1  
Old 07-30-2011, 06:24 PM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default [VB6] - calculate the distance between 2 objects


i have 1 code for calculate the distance between 2 objects:
Code:
If DirDirection = DirectionDown Then
                        lngLeftToCollision = Abs((T1 + H1) - T2)
                    ElseIf DirDirection = DirectionUp Then
                        lngLeftToCollision = Abs(T1 - (T2 + H2))
                    ElseIf DirDirection = DirectionLeft Then
                        lngLeftToCollision = Abs(L1 - (L2 + W2))
                    ElseIf DirDirection = DirectionRight Then
                        lngLeftToCollision = Abs((L1 + W1) - L2)
                    End If
1 is the moved object. the 2 is the second object.
t=top;
w=width;
h=heigth;
l=left.
these results aren't correct
can anyone advice me?

object 1 X object2
|-----||---||-----|

container size
|----------------------|

the x distance is what i need calculate, depending on direction.
Reply With Quote
  #2  
Old 07-31-2011, 06:43 PM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 566
Default

You should take the position of the object 1 to the object 2 into your account otherwise you will got wrong result, here is an example for calculating distance when moving up or down
Code:
Private Sub Command1_Click() Picture1.Top = Picture1.Top + 100 CalcDestance End Sub Private Sub Command2_Click() Picture1.Top = Picture1.Top - 100 CalcDestance End Sub Private Sub CalcDestance() If (Picture1.Top + Picture1.Height) < Picture2.Top Then Me.Caption = Abs((Picture1.Top + Picture1.Height) - Picture2.Top) ElseIf Picture1.Top > (Picture2.Top + Picture2.Height) Then Me.Caption = Abs((Picture2.Top + Picture2.Height) - Picture1.Top) Else Me.Caption = "Bump!" End If End Sub

Do left-right calculating yourself
__________________
Visit my site
Reply With Quote
  #3  
Old 08-01-2011, 04:02 PM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

ok.. now i think is working 90%:
Code:
If DirDirection = DirectionDown Then
                        lngLeftToCollision = T2 - (T1 + H1)
                        If CollisionPrecise(L1, T1 + lngLeftToCollision, W1, H1, L2, T2, W2, H2) = True Then
                            blnIsCollision = True
                        Else
                            blnIsCollision = False
                        End If
                    ElseIf DirDirection = DirectionUp Then
                        lngLeftToCollision = T1 - (T2 + H2)
                        If CollisionPrecise(L1, T1 - lngLeftToCollision, W1, H1, L2, T2, W2, H2) = True Then
                            blnIsCollision = True
                        Else
                            blnIsCollision = False
                        End If
                    ElseIf DirDirection = DirectionLeft Then
                        lngLeftToCollision = L1 - (L2 + W2)
                        If CollisionPrecise(L1 - lngLeftToCollision, T1, W1, H1, L2, T2, W2, H2) = True Then
                            blnIsCollision = True
                        Else
                            blnIsCollision = False
                        End If
                    ElseIf DirDirection = DirectionRight Then
                        lngLeftToCollision = (L1 + W1) - L2
                        If CollisionPrecise(L1 + lngLeftToCollision, T1, W1, H1, L2, T2, W2, H2) = True Then
                            blnIsCollision = True
                        Else
                            blnIsCollision = False
                        End If
                    End If
                    'Sleep (1)
                    DoEvents
                    If blnIsCollision = True And lngLeftToCollision > -1 Then RaiseEvent NotCollision(strName2, CStr(Index2), DirDirection, lngLeftToCollision)
                End If
why 90%?
because the code seems to working, but only sometimes
becaus, if i use the sleep() api function the code seems to work.
is there another function that can avoid the sleep()?(because the sleep "pauses" everything and i don't want it)
Reply With Quote
  #4  
Old 08-01-2011, 08:15 PM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 566
Default

Why not using custom delay procedure like this one
Code:
Private Sub DoDelay(ByVal sngPeriod As Single) Dim sngEnd As Single sngEnd = Timer + (sngPeriod / 1000) Do DoEvents Loop Until Timer > sngEnd End Sub
__________________
Visit my site
Reply With Quote
  #5  
Old 08-01-2011, 08:55 PM
passel's Avatar
passel[VB6] - calculate the distance between 2 objects passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

Cambalinho_83,
Your code is presented out of context.
How did you get to this point in the code?
Why do a DoEvents at that particular spot of the code?
Could doing a DoEvents at this point cause your code to re-enter itself, so that you process this code numerous times before raising the first event?
And if the code is re-entering itself, are there global values being changed and referenced that will cause problems when accessed out of sequence as the stack unwinds?
If you're going to use DoEvents, you need to make sure you are not inside an event handler that might be triggered again when you allow events to be processed.
The DoEvents call should be done at the end or beginning of your game loop, outside any event processing.
The Delay routing 4x2y provided as an example would be done at the bottom or top of your game loop, not in the middle of the processing as your DoEvents seems to be.

In this thread you can look at an example of the development of a game (not finished) and see where a delay like the one 4x2y provided is used to maintain a 30 hz update rate for the game.

Be sure to work your way through the thread to find the last update to the code for the best example of the progress made.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 08-01-2011 at 09:05 PM.
Reply With Quote
  #6  
Old 08-02-2011, 04:25 PM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

ok i will explian:
i did a timer for catch the move event(if don't moves, the notmove event is trigged) then by a loop(for see all controls) test the collision. if the collision isn't detetcted, the notcollision event is trigged for tell me the, depending on direction, how many pixels is the distance between the player and wall(for example). your question is: why to catch the distance? because my games have 1 back effect in walls. why these effect? think that the speed is 5, but to wall collision you only need 3. knowing these i can avoid the back effect.
these code seems to work, but not always
that's why i need some information to try fix it
Reply With Quote
  #7  
Old 08-05-2011, 04:58 PM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

i had 1 problem in my timers but now works fine.
but i found 1 bug:
Code:
................
If DirDirection = DirectionDown Then
                        lngLeftToCollision = T2 - (T1 + H1)
                        If CollisionPrecise(L1, T1 + lngLeftToCollision, W1, H1, L2, T2, W2, H2) = True Then
                            blnIsCollision = True
                        Else
                            blnIsCollision = False
                        End If
...................
why if the T1+H1<=0(up outside form) is give me a value(3 or 4)?
i have tested and i have seen these bug.. in these case the tested control is in form bottom.
Reply With Quote
  #8  
Old 08-05-2011, 05:24 PM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 566
Default

Quote:
why if the T1+H1<=0(up outside form) is give me a value(3 or 4)?
It is simple math rule, if you subtract negative value from positive value, the result is the addition of the two values e.g. 200 - (-90) = 290 not 110 as you could expected

BTW: refine your code, no need for using if...then
Code:
blnIsCollision = CollisionPrecise(L1 + lngLeftToCollision, T1, W1, H1, L2, T2, W2, H2)
__________________
Visit my site
Reply With Quote
  #9  
Old 08-05-2011, 05:39 PM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

Quote:
Originally Posted by 4x2y View Post
It is simple math rule, if you subtract negative value from positive value, the result is the addition of the two values e.g. 200 - (-90) = 290 not 110 as you could expected

BTW: refine your code, no need for using if...then
Code:
blnIsCollision = CollisionPrecise(L1 + lngLeftToCollision, T1, W1, H1, L2, T2, W2, H2)
lngLeftToCollision... sorry but these variable isn't about direction, but what left(is the distance value).
enter in msn, if you can mate.
Reply With Quote
  #10  
Old 08-05-2011, 05:54 PM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 566
Default

Quote:
lngLeftToCollision... sorry but these variable isn't about direction, but what left(is the distance value).

I don't understand, what exactly you mean by "why if the T1+H1<=0(up outside form) is give me a value(3 or 4)" is not value (3 or 4) is the result of lngLeftToCollision = T2 - (T1 + H1)
__________________
Visit my site
Reply With Quote
  #11  
Old 08-05-2011, 06:09 PM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

Quote:
Originally Posted by 4x2y View Post
I don't understand, what exactly you mean by "why if the T1+H1<=0(up outside form) is give me a value(3 or 4)" is not value (3 or 4) is the result of lngLeftToCollision = T2 - (T1 + H1)
in these case froget the "DoEvents() i don't need it.
the 1 is the moved object and the 2 is the stoped object(in these case).
and if the collision isn't detected i need to know what is the distance between these 2 objects(lngLeftToCollision).
t=top
l=left
w=wight
h=heigth
when the ti+h1 is <=0 then i recive the 4 or 3 values. but in these test isn't right. that's why i need ask these: my calculation is right?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
[VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects [VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects
 
[VB6] - calculate the distance between 2 objects
[VB6] - calculate the distance between 2 objects
 
-->