Finding a point in front of the vehicle

Discussion in 'Programming' started by ryakra, Mar 15, 2018.

  1. ryakra

    ryakra
    Expand Collapse

    Joined:
    Aug 5, 2013
    Messages:
    213
    I feel like this should be very simple, but I can't seem to figure it out. I want to get a point in front of the player vehicle for an eventual castRay(). I have the current user position gotten via getPosition(), and I attempted to get the rotation for use, but the vehicle rotation never changed.

    How would I go about getting a point that is always a set distance in front of the PlayerVehicle's front bumper?
     
  2. thomatoes50

    thomatoes50
    Expand Collapse
    BeamNG Team

    Joined:
    Jan 31, 2013
    Messages:
    722
    This code will work only on GE side and obj is the vehicle object. It does a raycast from 5 to 20 m in front of the vehicle
    Code:
    local pos= vec3( obj:getPosition() )
    local rot = quat( obj:getRotation() )
    local zVec,yVec,xVec = rot *vec3(0,0,1), rot *vec3(0,1,0), rot *vec3(1,0,0)
    
    local p1 = yvec * 5 + pos
    local p2 = yvec * 20 + pos
    local distance  = be:castRay(p1:toPoint3F(), p2:toPoint3F() )
    Note :
    • you may need to invert Y and X
    • with 0.12 and after use the method below
     
    #2 thomatoes50, Mar 15, 2018
    Last edited: Mar 15, 2018
    • Informative Informative x 1
  3. thomatoes50

    thomatoes50
    Expand Collapse
    BeamNG Team

    Joined:
    Jan 31, 2013
    Messages:
    722
    Here is an updated version that is better to use in 0.12

    Code:
    local pos= vec3( obj:getPosition() )
    local zVec,yVec= vec3(obj:getDirectionVectorUp()),vec3(obj:getDirectionVector())
    
    local p1 = yvec * 5 + pos
    local p2 = yvec * 20 + pos
    local distance  = be:castRay(p1:toPoint3F(), p2:toPoint3F() )
     
  4. ryakra

    ryakra
    Expand Collapse

    Joined:
    Aug 5, 2013
    Messages:
    213
    That doesnt seem to work, the rot is always 0,0,0,1. So no matter where im facing it gets distance in only one direction. im getting the vehicle via be:getPlayerVehicle(0), am I supposed to use another method?
     
  5. thomatoes50

    thomatoes50
    Expand Collapse
    BeamNG Team

    Joined:
    Jan 31, 2013
    Messages:
    722
    I guess you will have to wait for 0.12, the direction vector is always refreshed when you get it.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice