Raised This Month: $ Target: $400
 0% 

[Help] Trace Ray Functions


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Inhib
Member
Join Date: Oct 2014
Old 12-05-2014 , 04:17   [Help] Trace Ray Functions
Reply With Quote #1

I need help with the following things.

First: How do I trace a ray starting from a clients eye position in the direction he is looking and find the endpoint of the ray? The main problem I had doing this is identifying a ray, I'm not sure how to identify which ray the TR_GetEndPosition function identifies.

Second: How can I trace a ray from a players eye position without it hitting the players own model?

Third: How does the TR_TraceHull function work?

That is all. Thanks!

Last edited by Inhib; 12-05-2014 at 04:55.
Inhib is offline
turtsmcgurts
SourceMod Donor
Join Date: Jul 2011
Old 12-05-2014 , 04:55   Re: [Help] Trace Ray Functions
Reply With Quote #2

pseudocode. probably won't compile.

PHP Code:
FloatGet_Hit_Point_Forward(client) {
    new 
Floatorigin[3]; //where we store the players position
    
new Float:angles[3]; //where we store where the player is looking
    
new Floathit_location[3]; //where the trace hits
    
    
GetClientAbsOrigin(clientorigin);
    
GetClientEyeAngles(clientangles);
    
    
origin[2] += 75.0//we offset the z of the players position because by default it is at his feet. 75 is more or less correct, you'll want to finetune it.
    
    
new Handletrace TR_DoTrace(originangles);
    if(
TR_DidHit(trace))
        
TR_GetEndPosition(hit_locationtrace);
        
    
CloseHandle(trace);
    
    return 
hit_location;

as for making it not hit the player, you want to use a filter. here's one that does exactly what you want.

PHP Code:
public boolTrace_Filter_Not_Self(entitymaskany:data) {
    if(
entity == data)
        return 
false;
    return 
true;

Traceray's create a laser beam from point A to B, think of it as a pixel wide. TraceHull is designed to check an area that is as big as you want it to be. For example, if you wanted to spawn a player in a certain spot, you would do a tracehull the size of a player model, slightly larger even, at that spot to check if it's being blocked. if it's blocked and you spawn a player, he will be stuck.

Last edited by turtsmcgurts; 12-05-2014 at 04:56.
turtsmcgurts is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 12-05-2014 , 05:49   Re: [Help] Trace Ray Functions
Reply With Quote #3

So a tracehull is like a finite sized box and not like a really fat laser beam going off in one direction?
__________________
Chdata is offline
turtsmcgurts
SourceMod Donor
Join Date: Jul 2011
Old 12-05-2014 , 17:13   Re: [Help] Trace Ray Functions
Reply With Quote #4

it's a box going from one position to the other. you can adjust the minimum and maximum sizes so it could be really thin or something though.

you set the starting position and size, then you set the end position and size. as it goes from one position to the other, its size also changes (or doesn't if they're the same).

https://sm.alliedmods.net/api/index....d=show&id=876&

Last edited by turtsmcgurts; 12-05-2014 at 17:13.
turtsmcgurts is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 12-05-2014 , 21:02   Re: [Help] Trace Ray Functions
Reply With Quote #5

Oh so it's like a moving box?

Edit:

PHP Code:
/**
 * Starts up a new trace hull using a global trace result and a customized 
 * trace ray filter.
 *
 * Calling TR_Trace*Filter or TR_Trace*FilterEx from inside a filter 
 * function is currently not allowed and may not work.
 *
 * @param pos            Starting position of the ray.
 * @param vec            Depending on RayType, it will be used as the ending 
 *                        point, or the direction angle.
 * @param mins            Hull minimum size.
 * @param maxs            Hull maximum size.
 * @param flags            Trace flags.
 * @param filter        Function to use as a filter.
 * @param data            Arbitrary data value to pass through to the filter 
 *                        function.
 * @noreturn
 */
native TR_TraceHullFilter(const Float:pos[3],
                          const 
Float:vec[3],
                          const 
Float:mins[3],
                          const 
Float:maxs[3],
                          
flags,
                          
TraceEntityFilter:filter,
                          
any:data=0); 
Depending on RayType

What does it mean by RayType
__________________

Last edited by Chdata; 12-06-2014 at 01:58.
Chdata is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 12-06-2014 , 06:12   Re: [Help] Trace Ray Functions
Reply With Quote #6

Quote:
Originally Posted by Chdata View Post
What does it mean by RayType
It's a copy-paste error from TR_TraceRayFilter, TraceHull is always RayType_EndPoint.
__________________
asherkin is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 12-06-2014 , 06:25   Re: [Help] Trace Ray Functions
Reply With Quote #7

Ray type is what kind of raycast to use, endpoint will stop at the end x y z point you specify, I think its cheaper to use and good for testing if you can see from like point a to be, and test the entity index you hit. Infinite will keep going along the vector you give it for the x y z until it hits something. Hull filter obviously does not use ray type, since it tests bounds instead.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 12-06-2014 , 07:07   Re: [Help] Trace Ray Functions
Reply With Quote #8

The infinite ray type is implemented in SM as a convenience, the engine only supports point-to-point. I have no idea why the type param isn't on the hull functions (but, yeah, it would generally not be useful - hull tests are for checking movability rather than visibility).
__________________

Last edited by asherkin; 12-06-2014 at 07:07.
asherkin is offline
Inhib
Member
Join Date: Oct 2014
Old 12-08-2014 , 06:53   Re: [Help] Trace Ray Functions
Reply With Quote #9

Ok. Here's what I've gathered from all the posts, please correct me if I'm wrong.

To trace a ray
PHP Code:
new Floatorigin[3//Origin of ray (XYZ coords)
new Floatendpoint[3//Endpoint of ray (XYZ coords)
new Floatdirection[3//Vector direction of ray (XYZ coords)

TR_TraceRay(originendpointMASK_SHOTRayType_EndPoint//Trace line from origin to endpoint
TR_TraceRay(origindirectionMASK_SHOTRayType_Infinite//Trace line in direction from origin 
Questions at this point:
What are the CONTENTS_ABC in https://sm.alliedmods.net/api/ under the sdkhooks_trace submenu?
How can I use multiple flags?

To trace a ray and have it not hit the player
PHP Code:
public bool:rayhitplayer(entity,mask,any:data)
{
if(
entity==data)
{
return 
false;
}
else
{
return 
true;
}
}

TR_TraceRayFilter(origindirectionMASK_SHOTRayType_Infiniterayhitplayerclient); 
Questions:
In the above code, entity is undefined, what do I define it as?

I think this might work:
PHP Code:

public initialray()
{
new 
Floatorigin[3//Origin of ray (XYZ coords)
new Floatendpoint[3//Endpoint of ray (XYZ coords)
new Floatdirection[3//Vector direction of ray (XYZ coords)
TR_TraceRay(origindirectionMASK_SHOTRayType_Infinite)
if(
TR_DidHit)
{
new 
hitentity=TR_GetEntityIndex(INVALID_HANDLE)
}
}

public 
boolfinalrayfilter(entitymaskany:data)
{
if(
hitentity==data)
{
return 
false
}
else
{
return 
true
}
}

public 
finalray()
{
TR_TraceRay(origindirectionMASK_SHOTRayType_Infinitefinalrayfilter,client)

I doubt this will work for a few reasons.
I have to define hitentity globally for the filter to access it.
I have no idea what the entity and mask passed to filter does, do they have to be there?

Once again, how do I label a ray, cos I don't want to be searching for a global ray all the time, in case there are more rays.
Inhib is offline
turtsmcgurts
SourceMod Donor
Join Date: Jul 2011
Old 12-08-2014 , 18:18   Re: [Help] Trace Ray Functions
Reply With Quote #10

PHP Code:
public bool:rayhitplayer(entity,mask,any:data)

    if(
entity==data//did the trace hit the player?
    

        return 
false
    } 
    else 
//nope, it did not.
    

        return 
true
    } 


TR_TraceRayFilter(origindirectionMASK_SHOTRayType_Infiniterayhitplayerclient); 
in this example, with just this code, "entity" is whatever "client" is. the last parameter in TR_TraceRayFilter is data that you're passing into "rayhitplayer". this code should work perfectly. (it's exactly what you have, just with proper spacing)

to "label" a traceray, use handles. it's better anyway because you can close the handle when it's finished and not cause a memory leak.

PHP Code:
#define MAX_TRACE_RAYS 12 //how many trace rays we will allow
new Handletraceray_handle[MAX_TRACE_RAYS];
new 
amount_of_existing_handles 0;

test_function (client) {
    if(
amount_of_existing_handles MAX_TRACE_RAYS//if we are belowed our allowed amount
    
{
        
//creates a new handle
        
traceray_handle[amount_of_existing_handles] = TR_TraceRayFilter(origindirectionMASK_SHOTRayType_Infiniterayhitplayerclient);
        if (
TR_DidHit(traceray_handle[amount_of_existing_handles])) //if it hit something that isn't the same client (because we're using rayhitplayer)
            
TR_GetEndPosition(ground_positiontraceray_handle[amount_of_existing_handles]); //get the location of where it hit

        
CloseHandle(traceray_handle[amount_of_existing_handles]); // close the handle because it's unneeded and will eventually cause bad things to happen
    
}
}

public 
bool:rayhitplayer(clientmask,any:data
{  
    if(
client==data//did the trace hit the player? 
    
{  
        return 
false;  
    }  
    else 
//nope, it did not. 
    
{  
        return 
true;  
    }  

I doubt this will compile, i'm in a rush so I did it from memory. gl
turtsmcgurts is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:27.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode