[Info & release] Functional trace helper (locational & physics)

InfinityScript plugins for dedicated servers.
cubrr
TeknoMan
TeknoMan
Posts: 138
Joined: Sun Oct 05, 2014 12:54 am

[Info & release] Functional trace helper (locational & physics)

Postby cubrr » Thu Dec 31, 2015 2:05 pm

Hey,

As some might know, bullettrace doesn't work in IS.
However, physicstrace works. Or to be precise, physicstracenormal.

Physicstracenormal works just like physicstrace as described in ZeRoY's documentation.
With this, it's possible to, for example, get the location of whatever a player is pointing his crosshair at, like in this video (props to Vox for putting together that sweet weapon name string). Or an aimbot, I guess.

Here's a class containing some extension methods that I created for your ease.

EDIT: Class also now includes G_LocationalTrace which directly calls the engine method. This will only work in MW3 1.4, which is what all clients are patched to.

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using InfinityScript;

namespace Cbr.Extensions
{
    public static class Tracer
    {
        private static readonly IntPtr LocationalTracePointer = new IntPtr(0x48A2C0);
        private static G_LocationalTrace _locationalTraceFunction = null;

        [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        private delegate void G_LocationalTrace(ref Trace trace, ref Vector3 start, ref Vector3 end, int ignoreEntNum, int clipMask, int unk);

        public static Trace LocationalTrace(Vector3 start, Vector3 end, int ignoreEntNum = 2047, int clipMask = 0x806831)
        {
            Trace trace = new Trace();
            GetLocationalTraceFunction()(
                ref trace,
                ref start,
                ref end,
                ignoreEntNum,
                clipMask,
                0);
            return trace;
        }

        public static Vector3 LocationalTraceFromEye(Entity e, float maxDistance)
        {
            var headPosition = e.Call<Vector3>("gettagorigin", "tag_eye");
            var forwardAngles = Call<Vector3>("anglestoforward", e.Call<Vector3>("getplayerangles"));
            var traceEndPosition = headPosition + forwardAngles * maxDistance; // far to the forward towards crosshair
            Trace trace = LocationalTrace(headPosition, traceEndPosition);
            return trace.GetHitPosition(headPosition, traceEndPosition);
        }

        /// <summary>
        /// Physics trace, ignoring characters. Returns the endpos vector.
        /// </summary>
        /// <param name="start">The start point.</param>
        /// <param name="end">The end point.</param>
        /// <returns></returns>
        public static Vector3 PhysicsTrace(Vector3 start, Vector3 end)
        {
            return Call<Vector3>("physicstracenormal", start, end);
        }

        /// <summary>
        /// Physics trace from the vision of the player.
        /// </summary>
        /// <param name="e">The entity.</param>
        /// <param name="maxDistance">Specifies the maximum distance to the forwards to test for.</param>
        /// <returns></returns>
        public static Vector3 PhysicsTraceFromEye(Entity e, float maxDistance = 10000)
        {
            var headPosition = e.Call<Vector3>("gettagorigin", "tag_eye"); // props to kenny
            var forwardAngles = Call<Vector3>("anglestoforward", e.Call<Vector3>("getplayerangles"));
            var traceEndPosition = headPosition + forwardAngles * maxDistance; // far to the forward towards crosshair
            return PhysicsTrace(headPosition, traceEndPosition);
        }

        /// <summary>
        /// Physics trace using the player capsule size, ignoring characters. Returns the endpos vector.
        /// The player capsule's diameter on the xy-axis is approximately 15 units.
        /// </summary>
        /// <param name="start">The start point.</param>
        /// <param name="end">The end point.</param>
        /// <returns></returns>
        public static Vector3 PlayerPhysicsTrace(Vector3 start, Vector3 end)
        {
            return Call<Vector3>("playerphysicstrace", start, end);
        }

        private static G_LocationalTrace GetLocationalTraceFunction()
        {
            if (_locationalTraceFunction == null)
            {
                _locationalTraceFunction = (G_LocationalTrace)Marshal.GetDelegateForFunctionPointer(
                    LocationalTracePointer,
                    typeof(G_LocationalTrace));
            }
            return _locationalTraceFunction;
        }

        private static T Call<T>(string name, params Parameter[] args)
        {
            Function.SetEntRef(-1);
            return Function.Call<T>(name, args);
        }
    }

    [StructLayout(LayoutKind.Sequential, Size = 44)]
    public struct Trace
    {
        public float Fraction;
        public Vector3 Normal;
        public int SurfaceFlags;
        public int Contents;
        [MarshalAs(UnmanagedType.LPStr)]
        public string Material;
        public TraceHitType HitType;
        public short HitId;
        public short ModelIndex;
        public short PartName;
        public short PartGroup;
        public bool AllSolid;
        public bool StartSolid;
        public bool Walkable;
        public bool HitPartition;

        public Vector3 GetHitPosition(Vector3 start, Vector3 end)
        {
            return start + (end - start) * Fraction;
        }
    }

    public enum TraceHitType : int
    {
        None,
        Entity,
        DynamicEntityModel,
        DynamicEntityBrush,
        DynamicEntityUnknown
    }
}


Documentation comments from ZeRoY's documentation.

FAQ

Q: I get this exception: "System.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly ..."

A: Change your project to target .NET 4

Changes
26.3.2016 - Added LocationalTrace (used in BulletTrace)
28.2.2016 - Use Function.Call directly, thanks Lambder for reminding about that.
User avatar
Boboo
TeknoDev
Posts: 145
Joined: Thu Jul 23, 2015 3:41 pm

Re: Functional physicstrace helper

Postby Boboo » Thu Dec 31, 2015 3:13 pm

Thanks alot :good: :bravo: :bravo:
Image
cubrr
TeknoMan
TeknoMan
Posts: 138
Joined: Sun Oct 05, 2014 12:54 am

Re: Functional physicstrace helper

Postby cubrr » Thu Dec 31, 2015 3:14 pm

Boboo wrote:Thanks alot :good: :bravo: :bravo:

You're welcome!
User avatar
~Lambder
TeknoSheep
Posts: 448
Joined: Thu Apr 11, 2013 5:40 pm
Location: Meadow.
Contact:

Re: Functional physicstrace helper

Postby ~Lambder » Thu Dec 31, 2015 5:57 pm

Bookmarked.
User avatar
~Seraphim
TeknoHorse
TeknoHorse
Posts: 1186
Joined: Sun Jan 12, 2014 10:36 pm

Re: [Release] Functional physicstrace helper

Postby ~Seraphim » Sun Jan 03, 2016 11:06 am

Just a quick note, tag_eye actually contains viewOrigin. j_head contains the base head.
cubrr
TeknoMan
TeknoMan
Posts: 138
Joined: Sun Oct 05, 2014 12:54 am

Re: [Release] Functional physicstrace helper

Postby cubrr » Sun Jan 03, 2016 4:31 pm

c4271605 wrote:Just a quick note, tag_eye actually contains viewOrigin. j_head contains the base head.

Oh damn, I figured that there would be some tag like that but didn't know what it was named.

Thank you! Updated.
User avatar
~Lambder
TeknoSheep
Posts: 448
Joined: Thu Apr 11, 2013 5:40 pm
Location: Meadow.
Contact:

Re: [Release] Functional physicstrace helper

Postby ~Lambder » Sat Feb 27, 2016 10:02 am

Also, I don't see a reason why Function.Call wouldn't work?

I should be able to just make them static and use Function.Call instead of BaseScript.Call.



teknosamet01
TeknoDog
TeknoDog
Posts: 59
Joined: Sat May 17, 2014 7:11 am

Re: [Release] Functional physicstrace helper

Postby teknosamet01 » Sat Feb 27, 2016 11:09 am

Didint work for me :(
cubrr
TeknoMan
TeknoMan
Posts: 138
Joined: Sun Oct 05, 2014 12:54 am

Re: [Release] Functional physicstrace helper

Postby cubrr » Sat Feb 27, 2016 12:51 pm

Lambder wrote:Also, I don't see a reason why Function.Call wouldn't work?

I should be able to just make them static and use Function.Call instead of BaseScript.Call.

Yes, you should be able to just do

Code: Select all

Function.SetEntRef(-1);
Function.Call("name", params);

I didn't think of this when I made the helper methods.
teknosamet01 wrote:Didint work for me :(

What didn't?
teknosamet01
TeknoDog
TeknoDog
Posts: 59
Joined: Sat May 17, 2014 7:11 am

Re: [Release] Functional physicstrace helper

Postby teknosamet01 » Sat Feb 27, 2016 1:01 pm

I tried add fx on the target with your methods but it didnt work , need help :(

Return to “InfinityScript”

Who is online

Users browsing this forum: No registered users and 11 guests