Difference between revisions of "Talk:GetPositionX - ObjectReference"
imported>Rasikko m (casting better than floor.) |
imported>Rasikko m (global and local positions) |
||
Line 18: | Line 18: | ||
Use this to get the exact X coordinate for the cell. To be used only for exterior cells. Takes negative values into account. [[User:Lisselli|Lisselli]] ([[User talk:Lisselli|talk]]) 2017-10-27T12:22:34 (EDT) | Use this to get the exact X coordinate for the cell. To be used only for exterior cells. Takes negative values into account. [[User:Lisselli|Lisselli]] ([[User talk:Lisselli|talk]]) 2017-10-27T12:22:34 (EDT) | ||
: Math.Floor would give an inaccurate coordinate. If Y is -14.2, Floor will put it to -15. To properly handle positive and negatives in the case of coordinates, simply casting as int will do and no need to check if the position is negative or positive. --[[User:Rasikko|Rasikko]] ([[User talk:Rasikko|talk]]) 2018-01-10T12:39:49 (EST) | : Math.Floor would give an inaccurate coordinate. If Y is -14.2, Floor will put it to -15. To properly handle positive and negatives in the case of coordinates, simply casting as int will do and no need to check if the position is negative or positive. --[[User:Rasikko|Rasikko]] ([[User talk:Rasikko|talk]]) 2018-01-10T12:39:49 (EST) | ||
== Global and Local positions == | |||
When in the exterior worldspace, [[GetPositionX - ObjectReference]] and [[GetPositionY - ObjectReference]] return what I will call the '''global positions'''. These are accurate of course and do in fact help you determine where you are Skyrim. But what if you don't care about knowing the global positions? What if you can find out the '''local position'''. That is, the current position inside of a cell, ignoring any other cells around you. I shall explain..<br> | |||
Every exterior cell is 4096x4096 units, and your global positions is based on that number and grows(or shrinks in the case of negative numbers) from there. The origin cell is 0 to 4096 on the X, and 4096 to 0 on the Y. Using a random spot in that cell, say your X is 215 and your Y is 943. That would be your global position in the world ''but'' it would also be your local position in that cell. The origin cell is the only cell where the global and local are the same. For all other cells, the local positions needs to be calculated from the global positions.<br><br> So how do you get the local position? First you need to know what is the '''starting X and Y values for the cell'''. For example, cell 0, 1 has a starting X value of 4096, and and starting Y value of 4096. For it is these numbers you must subtract your global positions from to get the local positions. | |||
To get the X local position: | |||
<source lang="papyrus"> | |||
Float Function GetLocalPositionX(ObjectReference akObj) Global | |||
; Returns the local X position in a cell. | |||
Float posX = akObj.GetPositionX() | |||
; calculation is done by getting the starting x position for a cell. | |||
return posX - (posX - (posX - ((posX / 4096) as int) * 4096)) as Float | |||
EndFunction | |||
</source> | |||
<br> | |||
To get the Y location position: | |||
<source lang="papyrus"> | |||
Float Function GetLocalPositionY(ObjectReference akObj) Global | |||
; Returns the local Y position in a cell. | |||
Float posY = akObj.GetPositionY() | |||
return posY - (posY - (posY - ((posY / 4096) as int) * 4096)) as Float | |||
EndFunction | |||
</source> | |||
Proof: | |||
<source lang="papyrus"> | |||
[01/10/2018 - 06:17:26PM] The player's X: 23444.015625 The player's Y: -43183.757813 | |||
[01/10/2018 - 06:17:26PM] The starting positions for this cell: X: 20480.000000 Y: -40960.000000 | |||
[01/10/2018 - 06:17:26PM] The player's local X: 2964.015625 The player's local Y: -2223.757813 | |||
</source> | |||
It is possible to get the ending X/Y values for a cell, but they aren't necessary and are really just the starting values on a given side of a cell. --[[User:Rasikko|Rasikko]] ([[User talk:Rasikko|talk]]) 2018-01-10T13:28:47 (EST) |
Revision as of 13:28, 10 January 2018
Getting Cell Coordinates
Ever wondered how you can use the Cell View window's coordinates in game? Simple. You take the two values and you multiply them by 4096. If you want to convert an object's in game coordinates to use in the Cell View window, you divide the X and Y values by 4096 and cast as int to remove the decimals. Lisselli (talk)Lisselli (talk) 2017-10-06T11:57:52 (EDT)
GetCoordinateX
Int Function GetCoordinateX(ObjectReference akRef)
; Converts the objects' position to the X coordinate of the cell.
Float GetPosX = akRef.GetPositionX()
If GetPosX >= 0.0
Return GetPosX as int
Else
Return Math.Floor(GetPosX)
EndIf
EndFunction
Use this to get the exact X coordinate for the cell. To be used only for exterior cells. Takes negative values into account. Lisselli (talk) 2017-10-27T12:22:34 (EDT)
- Math.Floor would give an inaccurate coordinate. If Y is -14.2, Floor will put it to -15. To properly handle positive and negatives in the case of coordinates, simply casting as int will do and no need to check if the position is negative or positive. --Rasikko (talk) 2018-01-10T12:39:49 (EST)
Global and Local positions
When in the exterior worldspace, GetPositionX - ObjectReference and GetPositionY - ObjectReference return what I will call the global positions. These are accurate of course and do in fact help you determine where you are Skyrim. But what if you don't care about knowing the global positions? What if you can find out the local position. That is, the current position inside of a cell, ignoring any other cells around you. I shall explain..
Every exterior cell is 4096x4096 units, and your global positions is based on that number and grows(or shrinks in the case of negative numbers) from there. The origin cell is 0 to 4096 on the X, and 4096 to 0 on the Y. Using a random spot in that cell, say your X is 215 and your Y is 943. That would be your global position in the world but it would also be your local position in that cell. The origin cell is the only cell where the global and local are the same. For all other cells, the local positions needs to be calculated from the global positions.
So how do you get the local position? First you need to know what is the starting X and Y values for the cell. For example, cell 0, 1 has a starting X value of 4096, and and starting Y value of 4096. For it is these numbers you must subtract your global positions from to get the local positions.
To get the X local position:
Float Function GetLocalPositionX(ObjectReference akObj) Global
; Returns the local X position in a cell.
Float posX = akObj.GetPositionX()
; calculation is done by getting the starting x position for a cell.
return posX - (posX - (posX - ((posX / 4096) as int) * 4096)) as Float
EndFunction
To get the Y location position:
Float Function GetLocalPositionY(ObjectReference akObj) Global
; Returns the local Y position in a cell.
Float posY = akObj.GetPositionY()
return posY - (posY - (posY - ((posY / 4096) as int) * 4096)) as Float
EndFunction
Proof:
[01/10/2018 - 06:17:26PM] The player's X: 23444.015625 The player's Y: -43183.757813
[01/10/2018 - 06:17:26PM] The starting positions for this cell: X: 20480.000000 Y: -40960.000000
[01/10/2018 - 06:17:26PM] The player's local X: 2964.015625 The player's local Y: -2223.757813
It is possible to get the ending X/Y values for a cell, but they aren't necessary and are really just the starting values on a given side of a cell. --Rasikko (talk) 2018-01-10T13:28:47 (EST)