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.

Little help with game's 3D math

Discussion in 'Programming Questions and Suggestions' started by posthy, Apr 16, 2017.

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

    Messages:
    118
    I have an old script which worked fine for a while. but recently it broke and I hava no idea what's wrong. Here's the faulty code:
    Code:
    Base6Directions.Direction GetDirection(Vector3D TargetPos, IMyRemoteControl RemoteControl) 
    { 
    	Vector3D OwnPosition = RemoteControl.GetPosition(); 
    	Vector3D Head = RemoteControl.WorldMatrix.Forward; 
    	Vector3D Port = RemoteControl.WorldMatrix.Left; 
    	Vector3D Up = RemoteControl.WorldMatrix.Up; 
    	PlaneD XYShip = new PlaneD(Up,0); 
    	PlaneD XZShip = new PlaneD(Head,0); 
    	PlaneD YZShip = new PlaneD(Port,0); 
    	Vector3D VtoT = Vector3D.Normalize(Vector3D.Subtract(TargetPos, OwnPosition)); 
    	Double TEle = 90-MathHelper.ToDegrees(Math.Acos(XYShip.DotNormal(VtoT))); 
    	Double TBX = 90-MathHelper.ToDegrees(Math.Acos(XZShip.DotNormal(VtoT))); 
    	Double TBY = 90-MathHelper.ToDegrees(Math.Acos(YZShip.DotNormal(VtoT)));
    
           Debug("TEle:"+TEle); 
           Debug("TBX:"+TBX); 
           Debug("TBY:"+TBY); 
    
           if (TEle > 70) return Base6Directions.Direction.Up; 
           if (TEle < -70) return Base6Directions.Direction.Down; 
           if (TBX > 70) return Base6Directions.Direction.Forward; 
           if (TBX < -70) return Base6Directions.Direction.Backward; 
           if (TBY > 70) return Base6Directions.Direction.Left; 
           if (TBY < -70) return Base6Directions.Direction.Right; 
    
           throw new Exception("Couldn't determine direction (are you sure your waypoint is along the track?)"); 
    }
    TBX gets a NaN value, causing the exception thrown.
    The world didn't change, the coordinates are in place, really don't understand why this happens. Any help is really appreciated!
     
  2. plaYer2k Master Engineer

    Messages:
    3,160
    Seeing all the math there, it seems that you actually just want to know the direction some vector lies relative to a blocks orientation.
    What i would suggest here is transforming the vector into the blocks local space and then compare the X, Y and Z components against each other.

    Code:
    Base6Directions.Direction GetDirection(Vector3D target, IMyTerminalBlock block)
    {
    	// Turn the target vector into the vector from the blocks position to the target.
    	target -= block.WorldMatrix.Position;
    	// Transform the target vector into the blocks local space.
    	// Note: Transposing the matrix is fine here as we TransformNormal, which doesnt account for the position.
    	target = Vector3D.TransformNormal(target, MatrixD.Transpose(block.WorldMatrix));
    
    	// Get the closest direction value for the transformed vector.
    	return Base6Directions.Direction.GetClosestDirection(ref target);
    }
    
    I didnt code ingame for a while so take this with a grain of salt but it 'should' be correct.
     
  3. posthy Apprentice Engineer

    Messages:
    118
    Thank you for your answer, but unfortunately this doesn't compile.
    [​IMG]

    Probably a couple things changed since you didn't code ingame :)
    (sorry for the big image, but I couldn't find a way to copy that error message...)
     
  4. plaYer2k Master Engineer

    Messages:
    3,160
    Ah right it was MatrixD.Translation and not MatrixD.Position, so it is just a case of flawed memories :-)
     
  5. posthy Apprentice Engineer

    Messages:
    118
    Ok, the other error I could figure out myself :) Here's the final version if anyone wants to use it:
    Code:
    Base6Directions.Direction GetDirection(Vector3D target, IMyTerminalBlock block) 
    { 
    	// Turn the target vector into the vector from the blocks position to the target. 
    	target -= block.WorldMatrix.Translation; 
    	// Transform the target vector into the blocks local space. 
    	// Note: Transposing the matrix is fine here as we TransformNormal, which doesnt account for the position. 
    	target = Vector3D.TransformNormal(target, MatrixD.Transpose(block.WorldMatrix)); 
    
    	// Get the closest direction value for the transformed vector. 
    	return Base6Directions.GetClosestDirection(target); 
    }
    Thank you for your help!
     
  6. plaYer2k Master Engineer

    Messages:
    3,160
    Does it work with the desired effect tho?

    Also why do you use Base6Directions.Direction at all? I personally find that rather useless as almost anything revolves around straight vectors (V3, V3D, V3I).
     
  7. posthy Apprentice Engineer

    Messages:
    118
Thread Status:
This last post in this thread was made more than 31 days old.