Solved How to use an object's coordinate system as a reference, rather than global coordinates?

Discussion in 'Mod Support' started by Derpitron, Dec 27, 2022.

  1. Derpitron

    Derpitron
    Expand Collapse

    Joined:
    Jun 25, 2017
    Messages:
    252
    Is it possible to set/calculate coordinates using an object as a reference, rather than the world coordinate grid? I.e using the local coordinate system of the object as a reference of coordinates, rather than the global coordinate system.

    If not, then is it somehow possible to calculate a resultant global coordinate, using an object's local coordinate system as the initial reference?

    My goal is to set a camera offset 1 metre behind a vehicle. But, if I set the desired coordinates using core_camera.setOffset(), then it applies to the camera's Global/world position, which has adverse effects when the car is pointed in directions other than the positive X axis.

    I see that the axisSystemApply() function might be helpful here, but I'm not sure how to use it. Help would be appreciated on how to understand this.
     
  2. Gamergull

    Gamergull
    Expand Collapse
    BeamNG Team

    Joined:
    Jun 3, 2018
    Messages:
    460
    Hi. I'm away from my workstation, but I'll see if I can make a post that makes sense.

    As you may know, vectors can be used for both global and local coordinates, and a globalVec + localVec gives you a resultant global coordinate.

    For object global coordinates:
    You can use obj:getPosition(), but if you want to get the actual center or rear position of a vehicle:
    Code:
    pos = obj:getSpawnWorldOOBB():getCenter()
    posRear = obj:getSpawnWorldOOBBRearPoint()
    For object local coordinates:
    To get the vector of the vehicle's direction, and the vehicle's upwards direction; these are also known as unit vectors:
    Code:
    dirVec = obj:getDirectionVector()
    dirVecUp = obj:getDirectionVectorUp()
    Now, to get the new global coordinates, take the vehicle coordinates and add or subtract local coordinates (in this case, I will start with the vehicle rear coordinates):
    Code:
    result = posRear - dirVec * 5 + dirVecUp
    In this case, the result is always 5 m behind and 1 m above the rear point of the vehicle, no matter which way it is facing.

    I'm not sure how core_camera.setOffset() works, but you may be able to get the result you want with this:
    Code:
    core_camera.setOffset(-dirVec)


    I can talk about more details and additional functions later.
     
    • Like Like x 2
  3. Derpitron

    Derpitron
    Expand Collapse

    Joined:
    Jun 25, 2017
    Messages:
    252
    Your solution worked for Position!

    Is it possible to do something like this but for rotation?

    E.g the Orbit camera rotates forward when sudden braking or crash occurs ( G forces). But if the camera is to the right side of the car because the user rotated it using the joystick, then the camera should rotate left-wards upon the g force being applied.
     
  4. Gamergull

    Gamergull
    Expand Collapse
    BeamNG Team

    Joined:
    Jun 3, 2018
    Messages:
    460
    Hmm, alright. There is a method that you can use:
    Code:
    core_camera.setRotation(vehId, vec3(45, -45, 0))
    However, surprisingly, the input it uses needs to be a vector of angles (x = horizontal, y = vertical). The angle seems to be in global space, so some trigonometry may be needed to get a local vector. I'm a bit too tired to do the math right now, but you may be able to use this as a starting point.

    By the way, small correction in my previous comment, the correct code is:
    Code:
    core_camera.setOffset(vehId, -dirVec)
     
  5. Derpitron

    Derpitron
    Expand Collapse

    Joined:
    Jun 25, 2017
    Messages:
    252
    Thank you so much!

    I release you from my service. Go now and sleep in what way seems best to you.
     
  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