 |

07-30-2011, 05:24 PM
|
 |
Senior Contributor
|
|
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,177
|
|
[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.
|
|

07-31-2011, 05:43 PM
|
|
Contributor
|
|
Join Date: Sep 2005
Posts: 565
|
|
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 
|
|

08-01-2011, 03:02 PM
|
 |
Senior Contributor
|
|
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,177
|
|
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)
|
|

08-01-2011, 07:15 PM
|
|
Contributor
|
|
Join Date: Sep 2005
Posts: 565
|
|
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
|
|

08-01-2011, 07:55 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
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 08:05 PM.
|

08-02-2011, 03:25 PM
|
 |
Senior Contributor
|
|
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,177
|
|
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
|
|

08-05-2011, 03:58 PM
|
 |
Senior Contributor
|
|
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,177
|
|
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.
|
|

08-05-2011, 04:24 PM
|
|
Contributor
|
|
Join Date: Sep 2005
Posts: 565
|
|
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)
|
|

08-05-2011, 04:39 PM
|
 |
Senior Contributor
|
|
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,177
|
|
Quote:
Originally Posted by 4x2y
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.
|
|

08-05-2011, 04:54 PM
|
|
Contributor
|
|
Join Date: Sep 2005
Posts: 565
|
|
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)
|
|

08-05-2011, 05:09 PM
|
 |
Senior Contributor
|
|
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,177
|
|
Quote:
Originally Posted by 4x2y
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?
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|