CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: [RESOLVED] how i get point on a 3D line knowing the Z or X or Y?

  1. #1
    Join Date
    Apr 2009
    Posts
    1,300

    [RESOLVED] how i get point on a 3D line knowing the Z or X or Y?

    for i get the point on a 3D line, i use these function:
    Code:
    Position3D GetLinePosition(Position3D Origin, Position3D Destiny, double ActualPosition, Coordenates WhatCoordenate = Z){
    
    
        // Getting the AB vector(B-A or Destiny-Origin):
        Position3D AB;
        AB.X = Destiny.X - Origin.X;
        AB.Y = Destiny.Y - Origin.Y;
        AB.Z = Destiny.Z - Origin.Z;
    
    
        Position3D NewPosition;
        double T;
        // depending on player movement(for get 1 coordenate and the T), we get the point coordenate:
        if ((WhatCoordenate == X))
        {
            // X = Ax + T
            // T = Ax-X
            T = Origin.X - ActualPosition;
            NewPosition.X = ActualPosition;
            NewPosition.Y = Origin.Y + AB.Y * T;
            NewPosition.Z = Origin.Z + AB.Z * T;
        }
        else if ((WhatCoordenate == Y))
        {
            // Y = Ay + T
            // T = Ay-Y
            T = Origin.Y - ActualPosition;
            NewPosition.Y = ActualPosition;
            NewPosition.X = Origin.X + AB.X * T;
            NewPosition.Z = Origin.Z + AB.Z * T;
        }
        else if ((WhatCoordenate == Z))
        {
            // Z = Az + T
            // T = Az-Z
            T = Origin.Z - ActualPosition;
            NewPosition.Z = ActualPosition;
            NewPosition.X = Origin.X + AB.X * T;
            NewPosition.Y = Origin.Y + AB.Y * T;
        }
        GetLinePosition = NewPosition;
    }
    is these function correct for i get a point from 1 3D line using\knowing Z(for example)?

  2. #2
    Join Date
    Feb 2017
    Posts
    564

    Re: how i get point on a 3D line knowing the Z or X or Y?

    Quote Originally Posted by Cambalinho View Post
    is these function correct for i get a point from 1 3D line using\knowing Z(for example)?
    I cannot tell whether your code is correct but if the problem is to find a point on a 3D line given that one coordinate is fixed, then this is how I would go about it.

    Say you have two points in 3D called origin (O) and destination (D) and there's a direction vector AB between O and D calculated as D-O. Now say we have a 3D line going through O and D. It can be described in parametric form as,

    P = O + t*AB

    The parameter t is an ordinary number and by varying it you get all points P on the line.

    Explicitly for 3D it becomes,

    P.x = O.x + t*AB.x
    P.y = O.y + t*AB.y
    P.z = O.z + t*AB.z

    Now say you have P.x and want to find P.y and P.z so P is on the line. Then first solve the P.x equation above for t. Next plug t into the other equations and you get P.y and P.z.
    Last edited by wolle; May 27th, 2020 at 01:34 PM.

  3. #3
    Join Date
    Apr 2009
    Posts
    1,300

    Re: how i get point on a 3D line knowing the Z or X or Y?

    correct me more things:
    - the AB it's the destiny point line minus origin point line, right?
    - knowing the Z, how we calculate the T?
    T = AB.z - Z
    is these correct?
    wow i forget the O point.... maybe you can correct me more
    PS: i know the other thread seems 'RESOLVED', but i need more corrections.. sorry
    Last edited by Cambalinho; May 27th, 2020 at 02:30 PM.

  4. #4
    Join Date
    Feb 2017
    Posts
    564

    Re: how i get point on a 3D line knowing the Z or X or Y?

    Quote Originally Posted by Cambalinho View Post
    correct me more things:
    - the AB it's the destiny point line minus origin point line, right?
    - knowing the Z, how we calculate the T?
    T = AB.z - Z
    is these correct?
    wow i forget the O point.... maybe you can correct me more
    PS: i know the other thread seems 'RESOLVED', but i need more corrections.. sorry
    Yes AB is the vector going from O to D. It's calculated as D - O like,

    AB.x = D.x - O.x
    AB.y = D.y - O.y
    AB.z = D.z - O.z

    If you know the z-coordinate of P that is P.z and want to find out t you start with this,

    P.z = O.z + t*AB.z

    Doing simple algebraic manipulations you can solve it for t like,

    P.z = O.z + t*AB.z
    =>
    P.z - O.z = t*AB.z
    =>
    (P.z - O.z) / AB.z = t

    So the formula for t is,

    t = (P.z - O.z) / AB.z

    When you have t you plug it into,

    P.x = O.x + t*AB.x
    P.y = O.y + t*AB.y

    which will give you P.x and P.y.

    So by starting with a certain P.z you have found also P.x and P.y. The point P = (P.x, P,y, P.z) lies on the 3D line that goes through O and has the direction AB.
    Last edited by wolle; May 27th, 2020 at 03:00 PM.

  5. #5
    Join Date
    Apr 2009
    Posts
    1,300

    Re: how i get point on a 3D line knowing the Z or X or Y?

    thank you so much for all.. thank you.
    i get the same result, because the 1st collision point is wrong
    but now i have the function corrected:
    Code:
    Private Function GetLinePosition(Origin As Position3D, Destiny As Position3D, ActualPosition As Double, Optional WhatCoordenate As Coordenates = Z) As Position3D    
        'Getting the AB vector(B-A or Destiny-Origin):
        Dim AB As Position3D
        AB.X = Destiny.X - Origin.X
        AB.Y = Destiny.Y - Origin.Y
        AB.Z = Destiny.Z - Origin.Z
        
        Dim NewPosition As Position3D
        Dim T As Double
        'depending on player movement(for get 1 coordenate and the T), we get the point coordenate:
        If (WhatCoordenate = X) Then
            'P.x = O.x + t*AB.x
            't = (P.x - O.x) / AB.x
            T = (ActualPosition - Origin.X) / AB.X
            NewPosition.X = ActualPosition
            NewPosition.Y = Origin.Y + AB.Y * T
            NewPosition.Z = Origin.Z + AB.Z * T
        ElseIf (WhatCoordenate = Y) Then
            'P.y = O.y + t*AB.y
            't = (P.y - O.y) / AB.y
            T = (ActualPosition - Origin.Y) / AB.Y
            NewPosition.Y = ActualPosition
            NewPosition.X = Origin.X + AB.X * T
            NewPosition.Z = Origin.Z + AB.Z * T
        ElseIf (WhatCoordenate = Z) Then
            'P.z = O.z + t*AB.z
            't = (P.z - O.z) / AB.z
            T = (ActualPosition - Origin.Z) / AB.Z
            NewPosition.Z = ActualPosition
            NewPosition.X = Origin.X + AB.X * T
            NewPosition.Y = Origin.Y + AB.Y * T
        End If
        GetLinePosition = NewPosition
    End Function
    thank you so much for all... thank you

  6. #6
    Join Date
    Apr 2009
    Posts
    1,300

    Re: how i get point on a 3D line knowing the Z or X or Y?

    i'm sorry, but i can't rate you now: forum rules... sorry

  7. #7
    Join Date
    Apr 2009
    Posts
    1,300

    Re: [RESOLVED] how i get point on a 3D line knowing the Z or X or Y?

    when i calculate the 'T', how i know the Z or X or Y is from the line?
    is only compare, before calculate the 'T', if Z(or X or Y) belongs between the OriginX and DestinyX?

  8. #8
    Join Date
    Feb 2017
    Posts
    564

    Re: [RESOLVED] how i get point on a 3D line knowing the Z or X or Y?

    Quote Originally Posted by Cambalinho View Post
    how i know the Z or X or Y is from the line?
    This is the parametric version of a 3D line we have used,

    P.x = O.x + t*AB.x
    P.y = O.y + t*AB.y
    P.z = O.z + t*AB.z

    Looking at the first equation,

    P.x = O.x + t*AB.x

    we note that if t=0 then

    P.x = O.x

    and if t=1 then

    P.x = O.x + AB.x = O.x + (D.x - O.x) = D.x

    and if t is somewhere in between 0 and 1 then P.x is between O.x and D.x.

    So a P.x between O.x and D.x is equivalent to a t between 0 and 1. P.y and P.z follow analogously. For any specific t between 0 and 1 you have a point (P.x, P.y, P.x) on the line between O and D.
    Last edited by wolle; May 30th, 2020 at 04:12 AM.

  9. #9
    Join Date
    Apr 2009
    Posts
    1,300

    Re: [RESOLVED] how i get point on a 3D line knowing the Z or X or Y?

    let me ask you anotherthing: when i get the point from GetLinePosition(), i notice 1 thing: the floor isn't there for some Z pixels... so the best is change the player.Z to player.Z-100... i'm tell you '100', but i don't have sure what is the position that drawed correctly... so maybe the 100 is the best shot... what you can tell me more about it?
    (i need floor on player bottom )

  10. #10
    Join Date
    Apr 2009
    Posts
    1,300

    Re: [RESOLVED] how i get point on a 3D line knowing the Z or X or Y?

    when:
    Code:
    AB.X = Destiny.X - Origin.X
         AB.Y = Destiny.Y - Origin.Y
        AB.Z = Destiny.Z - Origin.Z
    are zero, how can i calculate the 'T'?
    Code:
    T = (ActualPosition - Origin.X) / AB.X

  11. #11
    Join Date
    Feb 2017
    Posts
    564

    Re: [RESOLVED] how i get point on a 3D line knowing the Z or X or Y?

    Quote Originally Posted by Cambalinho View Post
    zero, how can i calculate the 'T'?
    This is the parametric version of a 3D line we have used,

    P.x = O.x + t*AB.x
    P.y = O.y + t*AB.y
    P.z = O.z + t*AB.z

    If say AB.x is 0 then it becomes,

    P.x = O.x
    P.y = O.y + t*AB.y
    P.z = O.z + t*AB.z

    This means P.x must be O.x for P to be on the line regardless of how P.y and P.z are chosen.

    Mathematically t is no longer part of the first equation which therefore cannot be used to calculate a t. If you still try to do it then you end up with t=infinity, indicating no specific t exists. It's because when AB.x is 0 then there isn't just one t that fixes P.y and P.z to give a point P on the line, there are infinitely many and for all of them P.x=O.x. These t can be calculated from the second or the third equations.

    You will have to treat this situation as a special case in your code. It depends on how the game is supposed to work. Maybe you could just set t to 0 if AB.x is 0 (or very close to 0). This gives (O.x,O.y,O.z) which is a valid point on the line.
    Last edited by wolle; June 1st, 2020 at 01:02 PM.

  12. #12
    Join Date
    Apr 2009
    Posts
    1,300

    Re: [RESOLVED] how i get point on a 3D line knowing the Z or X or Y?

    wolle see these calculation:
    AB.X = Destiny.X - Origin.X
    if Destiny is the same of Origin, the AB.X is zero.
    yes i test point by point for get the right result... maybe i must use the same line origin and destiny and maybe avoid these problem
    it's hard to complete these hidden lines, but i must learn what i can
    Last edited by Cambalinho; June 1st, 2020 at 02:38 PM.

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,783

    Re: [RESOLVED] how i get point on a 3D line knowing the Z or X or Y?

    Quote Originally Posted by Cambalinho View Post
    when:
    Code:
    AB.X = Destiny.X - Origin.X
         AB.Y = Destiny.Y - Origin.Y
        AB.Z = Destiny.Z - Origin.Z
    are zero, how can i calculate the 'T'?
    Code:
    T = (ActualPosition - Origin.X) / AB.X
    In such a case you cannot "calculate the 'T".
    Just because the points Destiny and Origin are the same.
    And it is up to you to prove whether it is possible and if it is then by what conditions.
    Victor Nijegorodov

  14. #14
    Join Date
    Apr 2009
    Posts
    1,300

    Re: [RESOLVED] how i get point on a 3D line knowing the Z or X or Y?

    "Just because the points Destiny and Origin are the same.
    And it is up to you to prove whether it is possible and if it is then by what conditions."
    because i was wrong using the actual line point
    i know that i'm doing several errors without notice for draw where only we see the line:
    Code:
    Private Sub DrawPlane(Position As Position3D, Size As Size3D, Rotation As Angle3D, WorldSize As Size3D)
        Dim Points(4) As POINTAPI
        
        Dim NewPoint As POINTAPI
        
        Dim NewPosition3D As Position3D
        Dim RotatedPosition As Position3D
        Dim DestinyPosition As Position3D
        Dim sizeD As Size3D
        Dim OldPosition As Position3D
        FillSize3D sizeD, 0, 0, 0
        
        'FillPosition3D RotatedPosition, Position.X + Size.Width / 2, Position.Y + Size.Height / 2, Position.Z + Size.ZDepth / 2
        With Player1.Position
           FillPosition3D RotatedPosition, .X, .Y, .Z  'Camera Position
        End With
        
        
        If (IsCollision3D(Camera1.Position, Camera1.Size, Position, Size) = False) Then Exit Sub
        'Floor:
        'Vector1
        FillPosition3D NewPosition3D, Position.X, Position.Y, Position.Z
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        
            
            If (IsCollision3D(NewPosition3D, sizeD, Camera1.Position, Camera1.Size) = False) Then
                FillPosition3D DestinyPosition, Position.X, Position.Y, Position.Z + Size.ZDepth
                If (Player1.MoveCoordenates = X) Then
                    NewPosition3D = GetLinePosition(NewPosition3D, DestinyPosition, Camera1.Position.X, Player1.MoveCoordenates)
                ElseIf (Player1.MoveCoordenates = Y) Then
                    NewPosition3D = GetLinePosition(NewPosition3D, DestinyPosition, Camera1.Position.Y, Player1.MoveCoordenates)
                ElseIf (Player1.MoveCoordenates = Z) Then
                    NewPosition3D = GetLinePosition(NewPosition3D, DestinyPosition, Camera1.Position.Z, Player1.MoveCoordenates)
                End If
            End If
            OldPosition = NewPosition3D
            NewPoint = ConvertPositon3DTo2D(NewPosition3D, Camera1.Size)
            Points(0) = NewPoint
        
        'Vector2
        FillPosition3D NewPosition3D, Position.X, Position.Y, Position.Z + Size.ZDepth
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        If (IsCollision3D(Player1.Position, World3D, NewPosition3D, Size) = False) Then
            FillPosition3D DestinyPosition, Position.X, Position.Y, Position.Z + Size.ZDepth
            If (Player1.MoveCoordenates = X) Then
                NewPosition3D = GetLinePosition(OldPosition, DestinyPosition, Camera1.Position.X, Player1.MoveCoordenates)
            ElseIf (Player1.MoveCoordenates = Y) Then
                NewPosition3D = GetLinePosition(OldPosition, DestinyPosition, Camera1.Position.Y, Player1.MoveCoordenates)
            ElseIf (Player1.MoveCoordenates = Z) Then
                NewPosition3D = GetLinePosition(OldPosition, DestinyPosition, Camera1.Position.Z, Player1.MoveCoordenates)
            End If
        End If
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Points(1) = NewPoint
        
        'Vector3
        FillPosition3D NewPosition3D, Position.X + Size.width, Position.Y, Position.Z + Size.ZDepth
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        If (IsCollision3D(Player1.Position, World3D, NewPosition3D, Size) = False) Then
            FillPosition3D DestinyPosition, Position.X + Size.width, Position.Y + Size.height, Position.Z
            If (Player1.MoveCoordenates = X) Then
                NewPosition3D = GetLinePosition(NewPosition3D, DestinyPosition, Camera1.Position.X, Player1.MoveCoordenates)
            ElseIf (Player1.MoveCoordenates = Y) Then
                NewPosition3D = GetLinePosition(NewPosition3D, DestinyPosition, Camera1.Position.Y, Player1.MoveCoordenates)
            ElseIf (Player1.MoveCoordenates = Z) Then
                NewPosition3D = GetLinePosition(NewPosition3D, DestinyPosition, Camera1.Position.Z, Player1.MoveCoordenates)
            End If
        End If
        OldPosition = NewPosition3D
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Points(2) = NewPoint
        
        'Vector4
        FillPosition3D NewPosition3D, Position.X + Size.width, Position.Y + Size.height, Position.Z
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        If (IsCollision3D(Player1.Position, World3D, NewPosition3D, Size) = False) Then
            FillPosition3D DestinyPosition, Position.X, Position.Y, Position.Z
            If (Player1.MoveCoordenates = X) Then
                NewPosition3D = GetLinePosition(OldPosition, DestinyPosition, Camera1.Position.X, Player1.MoveCoordenates)
            ElseIf (Player1.MoveCoordenates = Y) Then
                NewPosition3D = GetLinePosition(OldPosition, DestinyPosition, Camera1.Position.Y, Player1.MoveCoordenates)
            ElseIf (Player1.MoveCoordenates = Z) Then
                NewPosition3D = GetLinePosition(OldPosition, DestinyPosition, Camera1.Position.Z, Player1.MoveCoordenates)
            End If
        End If
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Points(3) = NewPoint
        
        
        FillStyle = vbFSSolid
        
        FillColor = vbRed
        Polygon Me.hdc, Points(0), 4
    End Sub
    with time, i will fix it or change my idea on how to fix it
    Last edited by Cambalinho; June 1st, 2020 at 03:19 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)