Results 1 to 12 of 12
  1. #1
    MSgt. Shooter Person
    Join Date
    Apr 2012
    Posts
    139

    Question Beyond straight trace(); - Can we improved the accuracy?

    Hi,

    I have created the simple first person shooting game with unreal scripts. It's working quite good although it has several drawbacks.
    One limitation I have noticed is that selection functionality of friendly soldiers (NPC). I use trace(); for the pawn selection as bellow.

    PHP Code:
    function RayCast()
    {
        
    local vector locnormend;
        
    local TraceHitInfo hitInfo;
        
    local Actor traceHit;

        
    Local VHUserPawn TempVHUser;

        foreach 
    DynamicActors(class'VHUserPawn',TempVH)
        {
            if(
    TempVH.IsA('VHUserPawn'))
            {    
                
    User TempVH;
                break;
            }
        }
             
        
    end User.Location normal(vector(User.Rotation))*32768// trace to "infinity"
        
    traceHit trace(locnormendLocationtrue,, hitInfo);
         
        ...
        ...

    But I have to make several attempts to select as it is always not guarantee to hit on the pawn's collision box as expected. (especially while I am walking)
    So I think to find out the closest pawn (NPC) to the ray. I have stated what I have in mind as bellow.


    In this case pawn A should be selected. I need to know how to do it from unreal scripts. It will be much helpful your comments...
    Thankx
    Last edited by scriptowl; 07-14-2012 at 11:14 PM.

  2. #2
    Palace Guard

    Join Date
    Jun 2007
    Location
    Christchurch
    Posts
    3,514

    Default

    Use some vector maths to do this. Do a dot product of the players view rotation to the actual direction needed to view the Actor. The closer to one this is, the closer the two vectors are.

  3. #3
    Redeemer
    Join Date
    Dec 2008
    Location
    Germany
    Posts
    1,235

    Default

    Quote Originally Posted by Object.uc
    Code:
    /**
     * Calculates the distance of a given Point in world space to a given line,
     * defined by the vector couple (Origin, Direction).
     *
     * @param	Point				point to check distance to Axis
     * @param	Line				unit vector indicating the direction to check against
     * @param	Origin				point of reference used to calculate distance
     * @param	OutClosestPoint		optional point that represents the closest point projected onto Axis
     *
     * @return	distance of Point from line defined by (Origin, Direction)
     */
    native final function float PointDistToLine(vector Point, vector Line, vector Origin, optional out vector OutClosestPoint);
    Just check the distance for every NPC and store the closest.
    2B || !(2B)

  4. #4
    Iron Guard
    Join Date
    Jan 2012
    Posts
    630

    Default

    I'm trying to use it to check if the shot of a projectile will hit a teammate (the trace work really bad even with an extent) but i can't understand how to interpret the float value that the function gives to me.
    What i tryed is:

    Code:
    foreach MySquad.Members(teammate)
    	{
    		if(teammate != Pawn)
    		{
    			if(PointDistToLine(teammate.Location, Location + normal(vector(Rotation))*2000, Location) < 30)
    				return FALSE;
    		}
    	}
    
    	return TRUE;
    I did some logs and it seems to give higher values if you are more distant from the origin and lower value if you are near it.
    So it seems to project the point on the line checking how distant the actor is on that line, not how far it is from it on the side.
    Am i interpreting something wrong?

  5. #5
    Boomshot
    Join Date
    Aug 2011
    Posts
    2,281

    Default

    Quote Originally Posted by Ivan84 View Post
    I did some logs and it seems to give higher values if you are more distant from the origin and lower value if you are near it.
    So it seems to project the point on the line checking how distant the actor is on that line, not how far it is from it on the side.
    Am i interpreting something wrong?
    You're probably calling this from the player controller, so your second parameter should be Pawn.Location.

  6. #6
    Iron Guard
    Join Date
    Jan 2012
    Posts
    630

    Default

    Yes i've seen that error and i corrected it, but the float the function gives me regard the projection at 90 degrees of the pawn on the line and so it is the distance of the pawn from the origin if i'm not wrong, so it doesn't seem to solve the thread question.
    I will do more tests.

  7. #7
    Redeemer
    Join Date
    Dec 2008
    Location
    Germany
    Posts
    1,235

    Default

    Couple of things: the comment seems slightly messed up an doesn't stick to the same terms (no parameter named Direction).
    I also didn't test this function, but assume it works correctly.

    I'd say that "Point" is the point you want to check the distance to the line. Your teammate/NPC pawns location.
    "Line", a unit vector, the direction of the line. In your case it would be normal(vector(Rotation)).
    Origin is the Origin of the line so it should be Pawn.Location as Spoof said.
    Check your parameters again, they are still incorrect.

    This would (should) return the distance of Point to the line defined by the direction "Line" and the Origin "Origin".
    You might want to check if the OutClosestPoint is in front or behind the player!

    If it doesn't work you can still try to implement the same function yourself. It doesn't require too complicated vector math.
    2B || !(2B)

  8. #8
    Boomshot
    Join Date
    Aug 2011
    Posts
    2,281

    Default

    The parameters should be:
    Code:
    PointDistToLine(teammate.Location, Vector(Pawn.Rotation), Pawn.Location);
    First parameter is the enemy position
    Second parameter is a unit vector for the line's direction (pawn's rotation)
    Third parameter is the origin of the line (your pawn)

  9. #9
    Iron Guard
    Join Date
    Jan 2012
    Posts
    630

    Default

    Thank you both, now i understood how it works.
    The problem is that i have to check if the pawn is in front or behind as UnrealEverything said, but also if the teammate is at an higher distance than the enemy because if a teammate is exactly on the line of fire but the shot would hit the enemy because it is closer than the teammate, now the pawn doesn't fire.

    With all this checks the function would be more precise that the trace but i don't know if it will be also less expensive.

  10. #10
    MSgt. Shooter Person
    Join Date
    Apr 2012
    Posts
    139

    Default

    Thank you guys, PointDistToLine is the perfect solution for my problem. Now player needs to put less effort while playing the game. Still that is on evaluating stage. Hope to discuss the results ASAP.

    BTW what is the advantage of having, OutClosestPoint? What is the value assigning to it?

  11. #11
    Redeemer
    Join Date
    Dec 2008
    Location
    Germany
    Posts
    1,235

    Default

    If you need to know the exact location of the closest point on the line to a given point, then OutClosestPoint is what you want to use. Otherwise you would have to calculate it yourself (if you needed it). It also lets you determine the direction of the given point to the closest point on that line.

    You can pretty much do anything with it you what...
    2B || !(2B)

  12. #12
    MSgt. Shooter Person
    Join Date
    Apr 2012
    Posts
    139

    Default

    Greatt.... that is really friendly for programmers.


 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright ©2009-2011 Epic Games, Inc. All Rights Reserved.
Digital Point modules: Sphinx-based search vBulletin skin by CompletevB.com.