Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Detect obstacle in players movement direction

Discussion in 'Modding API' started by Shabazza, Mar 9, 2019.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Shabazza Junior Engineer

    Messages:
    689
    Hi.

    I'll just ask directly:
    How can I detect the closest obstacle a player (in suit) is moving towards?
    What i eventually need, is the location of that obstacle, so I can calculate the distance and the speed at which the player
    is approaching it.
    And it should be able to detect any "hard" obstacle, if possible, not just voxels.

    "Usually" I would just do a ray cast of a certain length originating from the player position towards the players movement direction.
    And upon a "hit" would retrieve the object it hit, it's location and be happy with it.

    But it seems like the SE modding API does not have a ray cast method that will take a max distance.
    You always need to specify a target location. (how weird is that??)
    So it's only good to refine your info about a known location and the stuff between you and it.

    And I also could not figure out, how to get the velocity of a player in suit.
    I was just able to get the players position and the "speed", which is not related to a direction.
    It's just the absolute average speed in the world, or something.
    If there is no RayCast like mentioned, I guess I do need to manually calculate the velocity in that direction in some convoluted way.
    But even then, I'm still missing a way to detect that obstacle in the first place.
    So I would be happy to hear, how to approach this problem with no "classic" RayCast available.
     
  2. plaYer2k Master Engineer

    Messages:
    3,160
    I dont know the ModAPI methods and cba to look them up right now but i can give some help based on what you wrote down.

    If the raycast method you got only just takes a start and an end position but you rather want to use your start position, define a max distance and got a normalized forward vector, then you can calculate your target position out of those informations very easily.
    Code:
    // You got these
    Vector3D playerPosition;
    double raycastDistance;
    Vector3D playerForwardVectorNormalized
    
    // You calculate the target position based on the current player position changed by desired raycast distance in forward direction 
    Vector3D raycastTargetPosition = playerPosition + raycastDistance * playerForwardVectorNormalized;
    
    // Now you raycast from the player position towards the target.
    raycast(playerPosition, raycastTargetPosition);
    
     
    • Agree Agree x 1
  3. Shabazza Junior Engineer

    Messages:
    689
    Oh....kay.
    Yeah. Why not. I can just determine my own ray cast target position by extending the "from" vector.
    Wow. I should have had another coffee...
    Thanks a bunch.
    I was quite frustrated yesterday with that CastRay method not looking like I expected it
    and was thinking too complicated.
    I was expecting something like the Unity one, which has a simple maxRange param. But vectors are way cooler, right? :woot:
     
  4. plaYer2k Master Engineer

    Messages:
    3,160
    Well that sounds like Unity actually contains the informations for position and direction already. Thus multiplying the direction with the maxRange while adding it to the position will yield the resulting target position. Potentially the two vectors position and direction are even given as matrix or object containing a matrix.
    So it really is just another way of getting to the same result, walking clockwise around the circle instead of counterclockwise.

    Good it solved your issue tho. Keep engineering and make us proud *craft craft* :D
     
  5. Shabazza Junior Engineer

    Messages:
    689
    Uh. I was too quick with closng it.
    There is still the second problem:

    The playerForwardVectorNormalized - No idea how I can construct that.
    Did as lot of Google-foo without result.

    This here seems to work for blocks on grids:

    Code:
    var forwardPos = reference.Position + Base6Directions.GetIntVector(reference.Orientation.TransformDirection(Base6Directions.Direction.Forward)); 
    	var forward = reference.CubeGrid.GridIntegerToWorld(forwardPos); 
    	var forwardVector = Vector3D.Normalize(forward - reference.GetPosition());
    
    But Players don't have an "Orientation" property like cube blocks.

    Might head over to DIscord, if it's not that easy.
     
  6. plaYer2k Master Engineer

    Messages:
    3,160
    Players, like all IMyEntity, got a WorldMatrix with a Forward vector.

    Code:
    Vector3D forwardVectorNormalized = player.WorldMatrix.Forward;
    
     
  7. Shabazza Junior Engineer

    Messages:
    689
    Thank you.
    But That seems to always point into the direction the players body is facing. Not the direction he's moving towards in the world.
    I need the latter, because in free fall on a planet, the body will always face "upright" while falling perpendicular-ish to the surface,
    and in space, it will also have some default orientation with inertia dampeners disabled.
    So the direction he's moving towards is usually not the direction his main body is facing at.

    How can I get the "velocity vector" ?
     
  8. plaYer2k Master Engineer

    Messages:
    3,160
    In that case, i can not help directly. I didnt code for SE in 2 years and never worked with the ModAPI.

    The only solution i can think of atm is to manually get your own velocity through keeping the past position and comparing it to the current for your very own velocity calculations. That however conflicts slightly at the end where you have to measure the time accurately. Due to high loads, a single update can be less than 1/60th of a second and i am not sure if that is exposed in the ModAPI or even if a more elegant and direct approach is exposed.

    Thoe ModAPIs IMyEntity exposes
    Code:
    MyPhysicsComponentBase Physics
    
    which offers
    Code:
    public virtual Vector3 LinearVelocity
    public virtual Vector3 LinearVelocityLocal
    
    so that may help. However, i am not sure if they are accessible, but they should *crosses fingers*.
     
  9. Shabazza Junior Engineer

    Messages:
    689
    I guess I have to construct my vector like that then.

    The rate at which I update my calculations is only at every 6th frame anyway = every 10ms (to save some CPU time, as raycasting can get expensive).
    What I do is, to propagate my speed towards the next frame where my calculation will run. (means: including the current acceleration + taking into account the simulation rate)
    So the braking will start soon enough.
    Works pretty well in the only scenario I can handle right now: Free fall in planet atmosphere.
    Admittedly, this scenario is the one where I derp the most and kill myself at times.
    So the current iteration is already protecting me from myself...
    Next step is to handle any direction - where my movement vector comes into play.

    Funny. I also have paused modding in SE for almost 2 years now. ^^
    It's not easy to get warm again with that really quirky SE ModAPI.
    But version 1.0 made me come back and I immediately remembered, that my SuitAI mod still needs a lot of love.
    And my best buddy hates me for it, because I don't play as much Anthem with him as he would like me to because of that. :woot:

    Thanks for your ideas anyway.
    Live long and prosper! .... Oh wait, wrong franchise.
     
    Last edited: Mar 16, 2019
Thread Status:
This last post in this thread was made more than 31 days old.