Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Where is my math wrong?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
LostInTransliteration
Newbie cheater
Reputation: 0

Joined: 25 Aug 2021
Posts: 10

PostPosted: Sat Jun 22, 2024 1:50 am    Post subject: Where is my math wrong? Reply with quote

I'm trying to change the values of a rotation matrix. I can change yaw and roll just fine but if I change the pitch things glitch out, so there must be some mistake in how I'm calculating or editing pitch specifically.

I believe that the matrix functions exactly like this one (sorry, can't post URLs yet): eecs.qmul.ac.uk/~gslabaugh/publications/euler.pdf . I extract the yaw, pitch and roll in radians, convert them to degrees, change the degrees by key input, convert them back into radians and feed those values back into the matrix.
My function to do this looks like this:

Code:

local M11 = "[pCamCoords]+24"
local M21 = "[pCamCoords]+28"
local M31 = "[pCamCoords]+2C"
local M12 = "[pCamCoords]+30"
local M22 = "[pCamCoords]+34"
local M32 = "[pCamCoords]+38"
local M13 = "[pCamCoords]+3C"
local M23 = "[pCamCoords]+40"
local M33 = "[pCamCoords]+44"

tmatrixMath = createTimer(nil, false)
tmatrixMath.Interval = 100
tmatrixMath.Enabled = true
tmatrixMath.OnTimer = function(matrixMathfunct)
-------------------------------------------------

--ψ is roll
rollRad = math.atan2(readFloat(M32), readFloat(M33))
rollDeg = rollRad  * (180 / 3.14159265359)
rollDegNew = rollDeg
rollRadNew = rollRad

--  θ is pitch (up/down)
pitchRad = math.asin(readFloat(M31))
pitchDeg = pitchRad * (180 / 3.14159265359)
pitchDegNew = pitchDeg
pitchRadNew = pitchRad

--φ is yaw (left/right) (x axis)
yawRad = math.atan2( (readFloat(M21)/math.cos(pitchRad)) , (readFloat(M11)/math.cos(pitchRad)) )
yawDeg = yawRad * (180 / 3.14159265359)
yawDegNew = yawDeg
yawRadNew = yawRad

--------------------------------------------------------------------------------

if (isKeyPressed(VK_NUMPAD2)) then
  rollDegNew = rollDeg - 1
  rollRadNew = rollDegNew * (3.14159265359 / 180)
  changed = "True"
  end

if (isKeyPressed(VK_NUMPAD3)) then
  rollDegNew = rollDeg + 1
  rollRadNew = rollDegNew * (3.14159265359 / 180)
  changed = "True"
  end

if (isKeyPressed(VK_NUMPAD5)) then
  pitchDegNew = pitchDeg - 1
  pitchRadNew = pitchDegNew * (3.14159265359 / 180)
  changed = "True"
  end

if (isKeyPressed(VK_NUMPAD6)) then
  pitchDegNew = pitchDeg + 1
  pitchRadNew = pitchDegNew * (3.14159265359 / 180)
  changed = "True"
  end

if (isKeyPressed(VK_NUMPAD8)) then
  yawDegNew = yawDeg - 1
  yawRadNew = yawDegNew * (3.14159265359 / 180)
  changed = "True"
  end

if (isKeyPressed(VK_NUMPAD9)) then
  yawDegNew = yawDeg + 1
  yawRadNew = yawDegNew * (3.14159265359 / 180)
  changed = "True"
  end

-- ψ is roll
-- θ is pitch (up/down)
-- φ is yaw (left/right) (x axis)

if (changed == "True") then
  newMatrixA = math.cos(pitchRadNew) * math.cos(yawRadNew)
  newMatrixB = math.cos(pitchRadNew) * math.sin(yawRadNew)
  newMatrixC = math.sin(pitchRadNew)

  newMatrixD = math.sin(rollRadNew) * math.sin(pitchRadNew) * math.cos(yawRadNew) - math.cos(rollRadNew) * math.sin(yawRadNew)
  newMatrixE = math.sin(rollRadNew) * math.sin(pitchRadNew) * math.sin(yawRadNew) + math.cos(rollRadNew) * math.cos(yawRadNew)
  newMatrixF = math.sin(rollRadNew) * math.cos(pitchRadNew)

  newMatrixG = math.cos(rollRadNew) * math.sin(pitchRadNew) * math.cos(yawRadNew) + math.sin(rollRadNew) * math.sin(yawRadNew)
  newMatrixH = math.cos(rollRadNew) * math.sin(pitchRadNew) * math.sin(yawRadNew) - math.sin(rollRadNew) * math.cos(yawRadNew)
  newMatrixI = math.cos(rollRadNew) * math.cos(pitchRadNew)
  writeFloat(M11,newMatrixA)
  writeFloat(M21,newMatrixB)
  writeFloat(M31,newMatrixC)
  writeFloat(M12,newMatrixD)
  writeFloat(M22,newMatrixE)
  writeFloat(M32,newMatrixF)
  writeFloat(M13,newMatrixG)
  writeFloat(M23,newMatrixH)
  writeFloat(M33,newMatrixI)
  changed = "False"
  end

-----------------------------------------
end


If I change pitchDeg/RadNew, yawDeg/RadNew and pitchDeg/RadNew also get changed even though they shouldn't. Changing yawDeg/RadNew and rollDeg/RadNew does not change either of the other values and simply changes yaw/pitch as it should.

Where is my math wrong?

Thanks so much for any help! ^-^
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4706

PostPosted: Sat Jun 22, 2024 10:18 am    Post subject: Reply with quote

In the document, M31 is `-sin(theta)`; in your code, it's just `sin(theta)`
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites