hi, i did this projection a while ago. because i'm not playing wow anymore i'm too lazy to check all the things for you =P but i can post you my version and you can compare (the offsets are out of date of cause)
the coding style is probably bad =P but it should work with the right offsets (at least is did when i tried it last)
Code:
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using Bot.Misc;
namespace Bot.KeyMouse
{
class KeyMouse
{
public struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
private static extern bool GetClientRect(IntPtr hWnd, ref Rect rect);
public const float Deg2Rad = 0.01745329251f;
private struct CameraInfo
{
public Vector Pos;
public Matrix ViewMat;
public float Foc;
};
public static bool MoveMouseToWoWCoords(float x, float y, float z)
{
var pseudoVec = new Vector(x, y, z); //not really a vector. its the location we want to click
IntPtr hwnd = Mem.Wow.WindowHandle;
var camera = new CameraInfo();
//Read information
uint pAddr2 = Mem.Wow.ReadUInt((Mem.Wow.ReadUInt((uint)Offsets.Static.cameraPointer)) + (uint)Offsets.Static.cameraOffset);
byte[] bCamera = Mem.Wow.ReadBytes(pAddr2, 68);
//Convert bytes to usable data
camera.Pos = new Vector(BitConverter.ToSingle(bCamera, 8), BitConverter.ToSingle(bCamera, 12), BitConverter.ToSingle(bCamera, 16));
camera.ViewMat = new Matrix(BitConverter.ToSingle(bCamera, 20), BitConverter.ToSingle(bCamera, 24), BitConverter.ToSingle(bCamera, 28),
BitConverter.ToSingle(bCamera, 32), BitConverter.ToSingle(bCamera, 36), BitConverter.ToSingle(bCamera, 40),
BitConverter.ToSingle(bCamera, 44), BitConverter.ToSingle(bCamera, 48), BitConverter.ToSingle(bCamera, 52));
camera.Foc = BitConverter.ToSingle(bCamera, 64);
//Get windoesize
var rc = new Rect();
GetClientRect(hwnd, ref rc);
//Vector camera -> object
var diff = pseudoVec - camera.Pos;
if ((diff * camera.ViewMat.GetFirstColumn) < 0)
return false;
var view = diff * camera.ViewMat.Inverse();
var cam = new Vector( -view.Y, -view.Z, view.X);
float fScreenX = (rc.right - rc.left) / 2.0f;
float fScreenY = (rc.bottom - rc.top) / 2.0f;
//Aspect ratio
float fTmpX = fScreenX / (float)Math.Tan(((camera.Foc * 44.0f) / 2.0f) * Deg2Rad);
float fTmpY = fScreenY / (float)Math.Tan(((camera.Foc * 35.0f) / 2.0f) * Deg2Rad);
var pctMouse = new Point
{
X = (int) (fScreenX + cam.X*fTmpX/cam.Z),
Y = (int) (fScreenY + cam.Y*fTmpY/cam.Z)
};
if (pctMouse.X < 0 || pctMouse.Y < 0 || pctMouse.X > rc.right || pctMouse.Y > rc.bottom)
return false;
InputControl.MoveMouse(pctMouse.X, pctMouse.Y);
return true;
}
}
}
vector-library:
Code:
namespace Bot.Misc
{
public class Vector
{
private float _x, _y, _z;
public Vector()
{
_x = 0;
_y = 0;
_z = 0;
}
public Vector(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
}
public Vector(Vector v)
{
_x = v._x;
_y = v._y;
_z = v._z;
}
public void SetVec(Vector v)
{
_x = v._x;
_y = v._y;
_z = v._z;
}
public void SetVec(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
}
public float X
{
get { return _x; }
set { _x = value; }
}
public float Y
{
get { return _y; }
set { _y = value; }
}
public float Z
{
get { return _z; }
set { _z = value; }
}
public static Vector operator +(Vector v1, Vector v2)
{
var v3 = new Vector((v1._x + v2._x), (v1._y + v2._y), (v1._z + v2._z));
return v3;
}
public static Vector operator -(Vector v1, Vector v2)
{
var v3 = new Vector((v1._x - v2._x), (v1._y - v2._y), (v1._z - v2._z));
return v3;
}
public static float operator *(Vector v1, Vector v2)
{
var f = v1._x * v2._x + v1._y * v2._y + v1._z * v2._z;
return f;
}
}
}
matrix - library
Code:
namespace Bot.Misc
{
class Matrix
{
private float _x1, _x2, _x3, _y1, _y2, _y3, _z1, _z2, _z3;
public Matrix(float x1, float x2, float x3, float y1, float y2, float y3, float z1, float z2, float z3)
{
_x1 = x1;
_x2 = x2;
_x3 = x3;
_y1 = y1;
_y2 = y2;
_y3 = y3;
_z1 = z1;
_z2 = z2;
_z3 = z3;
}
public Matrix()
{ }
public Vector GetFirstColumn
{
get { return new Vector(_x1, _y1, _z1); }
}
public Matrix Inverse()
{
var d = 1 / Det();
var inv = new Matrix(d * (_y2 * _z3 - _y3 * _z2), d * (_x3 * _z2 - _x2 * _z3), d * (_x2 * _y3 - _x3 * _y2),
d * (_y3 * _z1 - _y1 * _z3), d * (_x1 * _z3 - _x3 * _z1), d * (_x3 * _y1 - _x1 * _y3),
d * (_y1 * _z2 - _y2 * _z1), d * (_x2 * _z1 - _x1 * _z2), d * (_x1 * _y2 - _x2 * _y1));
return inv;
}
public float Det()
{
float det = (_x1 * _y2 * _z3) + (_x2 * _y3 * _z1) + (_x3 * _y1 * _z2)
- (_x3 * _y2 * _z1) - (_x2 * _y1 * _z3) - (_x1 * _y3 * _z2);
return det;
}
public static Vector operator *(Vector v, Matrix m)
{
var res = new Vector(m._x1 * v.X + m._y1 * v.Y + m._z1 * v.Z,
m._x2 * v.X + m._y2 * v.Y + m._z2 * v.Z,
m._x3 * v.X + m._y3 * v.Y + m._z3 * v.Z);
return res;
}
}
}
if you have problems understanding the code feel free to ask. ^^