OpenMods / OpenPeripheral-Addons

An addon for OpenPeripheral, adding physical blocks and items to the game
MIT License
11 stars 10 forks source link

yawHead returning values bigger than 180 and smaller than -180 #31

Closed TorakWolf closed 10 years ago

TorakWolf commented 10 years ago

I have noticed that sometimes the yawHead ( and maybe the normal yaw, IDK ) returns me weird values like aprox. -280 or 310. It should only return values from -180 to 180, so there's a problem here...

boq commented 10 years ago

We are returning raw values directly from Minecraft, no point in fixing that.

Also, no need to clamp it to range, trigonometric functions work fine with that. If needed, it should be easy to do this operation directly in Lua.

TorakWolf commented 9 years ago

They are not returning minecraft values, as mc can only return values from -180 to 180 ( I check the debug screen if needed ).

The problem with values out of that range is that I can't figure out how to use them to make my trigonometric program. If you just explain me how they are generated by the mod, I can just transform them back to normal values.

I think they vary from 0 to 360 rather than -180 to 180. But here lies the problem... How can I know if it's, for example, 56 of 360 or 56 of 180?

TorakWolf commented 9 years ago

The problem isn't the value range, but the fact that we have 2 possible ranges and there are no methods to know wich one is being used. At least my LUA-amateur brain can't figure out how. Can you explain it to me if possible? And what do those out of range values mean in the conventional mc debug screen values ( -180 to 180 )?

boq commented 9 years ago

Those are angles (not radians) returned directly from game. That's all you need to know. As I said, there are NO conversions between OpenPeripherals and values reported by Minecraft. It doesn't matter. You know why?

sin(x + 360°) = sin(x)
sin(x - 360°) = sin(x)
cos(x + 360°) = cos(x)
cos(x - 360°) = cos(x)
tan(x + 360°) = tan(x)
tan(x - 360°) = tan(x)
...

Starting to see pattern? It's nifty thing called periodicity of trigonometric functions. -180° = +180°. Doesn't matter if you turn 180° left or right, you are still facing backwards. 0° = 360°. If you turn around you are facing same point.

Why there are values in -180°..180° range on debug screen? Minecraft takes extra mile and wraps those before printing. Such code it trivial, but here's particular implementation used by game:

value = value % 360;
if value >= 180 then
    value =  value - 360;
end

if value <= -180 then
    value =  value + 360;
end

There you go. Was it that hard?

TorakWolf commented 9 years ago

Hum... gonna test some code based on what you said once I got my hands on one desktop computer. About the degree thing, it's pretty normal for me, as my country uses degrees rather than radians.

I was using something like:

math.tan(math.rad(math.abs(angle)))

To get the tangent of the absolute angle... Ok, since my programs compare positions using both the tangent ( 0 to 90 degrees ) times z to see if the result is the x and also the facing to see if you are really loking on that direction rather than the oposite and identical angle, I have to change something.

TorakWolf commented 9 years ago

About your question on: "Was it hard?"

Not really, except for the part of minecraft code, wich, of course, I never imagined. Seems like I've to make the code simplier in order to avoid problems.