Skip to content

Instantly share code, notes, and snippets.

@ikt32
Created June 10, 2017 15:49
Show Gist options
  • Save ikt32/dfc29a32ede5f317ffca4059380a8214 to your computer and use it in GitHub Desktop.
Save ikt32/dfc29a32ede5f317ffca4059380a8214 to your computer and use it in GitHub Desktop.
Get absolute wheel coordinates (without GetOffsetInWorldCoords native)
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Channels;
using GTA.Math;
using GTA.Native;
namespace GTA.examples
{
public class WheelPositions : Script
{
public WheelPositions()
{
Tick += OnTick;
}
void OnTick(object sender, EventArgs e)
{
Ped player = Game.Player.Character;
if (!player.IsInVehicle())
{
return;
}
Vehicle vehicle = player.CurrentVehicle;
var coords = GetWheelCoords(vehicle, vehicle.Position, vehicle.Rotation, vehicle.ForwardVector);
int i = 0;
foreach (var coord in coords)
{
Function.Call(Hash.DRAW_LINE,
coord.X, coord.Y, coord.Z,
coord.X, coord.Y, coord.Z + 2.5f,
0, 0, 255, 127);
ShowText(0.01f + i * 0.1f, 0.26f + 0 * 0.025f, "X: " + coord.X, 0.5f);
ShowText(0.01f + i * 0.1f, 0.26f + 1 * 0.025f, "Y: " + coord.Y, 0.5f);
ShowText(0.01f + i * 0.1f, 0.26f + 2 * 0.025f, "Z: " + coord.Z, 0.5f);
i++;
}
ShowText(0.01f + i * 0.1f, 0.26f + 0 * 0.025f, "DX: " + vehicle.ForwardVector.X, 0.5f);
ShowText(0.01f + i * 0.1f, 0.26f + 1 * 0.025f, "DY: " + vehicle.ForwardVector.Y, 0.5f);
ShowText(0.01f + i * 0.1f, 0.26f + 2 * 0.025f, "DZ: " + vehicle.ForwardVector.Z, 0.5f);
i++;
ShowText(0.01f + i * 0.1f, 0.26f + 0 * 0.025f, "RX: " + vehicle.Rotation.X, 0.5f);
ShowText(0.01f + i * 0.1f, 0.26f + 1 * 0.025f, "RY: " + vehicle.Rotation.Y, 0.5f);
ShowText(0.01f + i * 0.1f, 0.26f + 2 * 0.025f, "RZ: " + vehicle.Rotation.Z, 0.5f);
}
void ShowText(float x, float y, string text, float size = 0.2f)
{
Function.Call(Hash.SET_TEXT_FONT, 0);
Function.Call(Hash.SET_TEXT_SCALE, size, size);
Function.Call(Hash.SET_TEXT_COLOUR, 255, 0, 0, 255);
Function.Call(Hash.SET_TEXT_WRAP, 0.0, 1.0);
Function.Call(Hash.SET_TEXT_CENTRE, 0);
Function.Call(Hash.SET_TEXT_OUTLINE, true);
Function.Call(Hash._SET_TEXT_ENTRY, "STRING");
Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
Function.Call(Hash._DRAW_TEXT, x, y);
}
unsafe ulong GetWheelsPtr(Vehicle handle)
{
var address = (ulong)handle.MemoryAddress;
ulong offset = 0xB10;
return *((ulong*)(address + offset));
}
unsafe int GetNumWheels(Vehicle handle)
{
var address = (ulong)handle.MemoryAddress;
ulong offset = 0xB10;
offset += 8;
return *((int*)(address + offset));
}
unsafe List<ulong> GetWheelPtrs(Vehicle handle)
{
var wheelPtr = GetWheelsPtr(handle); // pointer to wheel pointers
var numWheels = GetNumWheels(handle);
List<ulong> wheelPtrs = new List<ulong>();
for (int i = 0; i < numWheels; i++)
{
var wheelAddr = *((ulong*)(wheelPtr + 0x008 * (ulong)i));
wheelPtrs.Add(wheelAddr);
}
return wheelPtrs;
}
unsafe List<Vector3> GetWheelOffsets(Vehicle handle)
{
var wheels = GetWheelPtrs(handle);
List<Vector3> positions = new List<Vector3>();
ulong offPosX = 0x20;
ulong offPosY = 0x24;
ulong offPosZ = 0x28;
foreach (var wheelAddr in wheels)
{
Vector3 wheelPos;
wheelPos.X = *((float*)(wheelAddr + offPosX));
wheelPos.Y = *((float*)(wheelAddr + offPosY));
wheelPos.Z = *((float*)(wheelAddr + offPosZ));
positions.Add(wheelPos);
}
return positions;
}
List<Vector3> GetWheelCoords(Vehicle handle, Vector3 position, Vector3 rotation, Vector3 direction)
{
List<Vector3> worldCoords = new List<Vector3>();
List<Vector3> positions = GetWheelOffsets(handle);
foreach (var wheelPos in positions)
{
Vector3 absPos = GetOffsetInWorldCoords(position, rotation, direction, wheelPos);
//handle.GetOffsetInWorldCoords(wheelPos);
worldCoords.Add(absPos);
}
return worldCoords;
}
Vector3 GetOffsetInWorldCoords(Vector3 position, Vector3 rotation, Vector3 forward, Vector3 offset)
{
const float deg2Rad = (float)0.01745329251994329576923690768489;
float num1 = (float)System.Math.Cos(rotation.Y * deg2Rad);
float x = num1 * (float)System.Math.Cos(-rotation.Z * deg2Rad);
float y = num1 * (float)System.Math.Sin(rotation.Z * deg2Rad);
float z = (float)System.Math.Sin(-rotation.Y * deg2Rad);
Vector3 right = new Vector3((float)x, (float)y, (float)z);
Vector3 up = Vector3.Cross(right, forward);
return position + (right * offset.X) + (forward * offset.Y) + (up * offset.Z);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment