Post subject: LUA assistance needed, calculating angles.
Warepire
He/Him
Editor
Joined: 3/2/2010
Posts: 2174
Location: A little to the left of nowhere (Sweden)
I am kinda new at LUA scripting, and I cannot figure out why my angle math isn't working. I would appreciate some help. The script is for Bugs Bunny Lost In Time on PSX, gameID SLUS-00838. The game maps the word so that 0,0,0 is in a corner, so I cannot rely on the position to get a proper angle, I figured using the speed values would also work, but it seems that it doesn't, because according to that, I never steer away more than 3 degrees from "straight forward". Can someone please explain what I am doing wrong? Script:
Language: lua

--position, speed and direction local Xpos local XposOld = 0 local Ypos local YposOld = 0 local Zpos local ZposOld = 0 local Xspeed = 0 local Yspeed = 0 local Zspeed = 0 --not really a need to look at Y-speed as it seems to be independent of XZ speed, might still be useful for jumping. local XZspeed local XZdirection = 0 --stats local health local inlvlCarrots local inlvlGoldenCarrots local inlvlClocks local inlvlACME local totalCarrots local totalGoldenCarrots local totalClocks local abilities local jump = false local music = false local sesame = false local fan = false local function hud() --Actual speed vector addresses, not very good as they show speed even when you're blocked by a wall etc. --addr 00069D30 X Speed [signed word] --addr 00069D34 Y Speed(?) [signed word] --addr 00069D38 Z Speed [signed word] Xpos = memory.readdwordsigned(0x00069DC0) Ypos = memory.readdwordsigned(0x00069DC4) Zpos = memory.readdwordsigned(0x00069DC8) --will not show the proper speed on the first frame of a level --the game only runs at 30 fps. local tempXspeed = Xpos - XposOld local tempYspeed = Ypos - YposOld local tempZspeed = Zpos - ZposOld local tempXZdirection = math.atan2(tempZspeed, tempXspeed) if tempXspeed ~= 0 or tempYspeed ~= 0 or tempZspeed ~= 0 then Xspeed = math.abs(tempXspeed) Yspeed = math.abs(tempYspeed) Zspeed = math.abs(tempZspeed) XZdirection = tempXZdirection end XZspeed = math.sqrt(Xspeed*Xspeed + Zspeed*Zspeed) health = memory.readbyte(0x00010041) inlvlCarrots = memory.readbyte(0x00069525) inlvlGoldenCarrots = memory.readbyte(0x00069574) inlvlClocks = memory.readbyte(0x00069514) inlvlACME = memory.readbyte(0x00010044) totalCarrots = memory.readbyte(0x00010043) totalGoldenCarrots = (memory.readbyte(0x0001013C)*256) + memory.readbyte(0x00010047) --addr 0x0001013C is a roll-over counter for addr 0x00010047 totalClocks = memory.readbyte(0x00010045) abilities = memory.readbyte(0x00010048) --0x08 == SuperJump if abilities >= 0x08 then jump = true abilities = abilities - 0x08 end -- TODO: Get the flags for the rest of the abilities gui.opacity(1.0) gui.text(0,0,string.format("Pos (%6d,%6d,%6d)\n",Xpos,Ypos,Zpos) ..string.format("Speed XZ: %3d Angle: %3d\n",XZspeed,XZdirection) ..string.format("Speed Y: %3d\n\n",Yspeed) ..string.format("Health: %d\n",health) ..string.format("Carrots: %d\n",totalCarrots) ..string.format("LVL Golden: %2d Total Golden: %3d\n",inlvlGoldenCarrots,totalGoldenCarrots) ..string.format("LVL Clocks: %2d Total Clocks: %3d\n",inlvlClocks,totalClocks) ..string.format("ACME boxes: %d\n\n",inlvlACME) ..string.format("Jump: %s\n",tostring(jump)) ..string.format("Music: %s\n",tostring(music)) ..string.format("Sesame: %s\n",tostring(sesame)) ..string.format("Fan: %s\n",tostring(fan))) XposOld = Xpos YposOld = Ypos ZposOld = Zpos end gui.register(hud)
Editor, Skilled player (1172)
Joined: 9/27/2008
Posts: 1085
The function math.atan2 does not return the value in degrees. It returns a value in radians. 360 degrees per full revolution. 2 pi radians per full revolution (about 6.28 radians). This probably explains why you're not seeing the angle increase beyond the number 3. This is what I think, at a glance. Divide by math.pi and multiply by 180 if you want degrees.
Warepire
He/Him
Editor
Joined: 3/2/2010
Posts: 2174
Location: A little to the left of nowhere (Sweden)
All the guides told me I was getting degrees from that function, but adding "* math.pi) / 180) * 1000" fixed it. Thanks for the help, I can now see the direction I'm going in!
Editor, Skilled player (1172)
Joined: 9/27/2008
Posts: 1085
The reference manual I'm using says radians. Apparently the guides you were using didn't lead you correctly. I think you got the calculations backwards. It should be ( (that atan2 value) * 180 / math.pi), but... Well, if the numbers make enough sense as it stands, then you really shouldn't have trouble either way.
Warepire
He/Him
Editor
Joined: 3/2/2010
Posts: 2174
Location: A little to the left of nowhere (Sweden)
I cannot seem to stop failing today. Even if the old values worked, these new ones work a lot better. Thanks!