1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

# Floating point conversion from world coordinate to local coordinate.

Discussion in 'Programming Questions and Suggestions' started by jerry060599, Mar 22, 2017.

This last post in this thread was made more than 31 days old.
1. ### jerry060599Trainee Engineer

Messages:
37
Hello and thanks for reading. I was doing some testing for a script I am working on and came across an issue. controller.CubeGrid.WorldToGridInteger() only seems to return integer values when I need extact floating point values for the center of mass. So far, searching online and here doesn't seem to yield any results except for one that seems outdated. I have attempted to multiply the inverse CubeGrid.WorldMatrix with the world coordinate but that seems to be entirely dependent on which blocks worldMatrix I use. Here is what I have so far:

Code:
```
Vector3 worldToLocal(Vector3 pos)
{
MatrixD mat = MatrixD.Invert(Me.WorldMatrix);
//Row major multiplication
return new Vector3(pos.X * mat.M11 + pos.Y * mat.M21 + pos.Z * mat.M31 + mat.M41,
pos.X * mat.M12 + pos.Y * mat.M22 + pos.Z * mat.M32 + mat.M42,
pos.X * mat.M13 + pos.Y * mat.M23 + pos.Z * mat.M33 + mat.M43) / BlockSize;
}```
Can anyone see what I'm missing?

2. ### PhoeraSenior Engineer

Messages:
1,713
grid position can be only integer, ofc it will return integer value.
center of mass of grid?
controller.CenterOfMass will give you that.
(controller is IMyShipController, which is cockpits and remote control)

3. ### jerry060599Trainee Engineer

Messages:
37
I understand the reasoning behind making local grid Vector3I only, but I already know that the c.CenterOfMass gets the center of mass... in world coordinates. I need to convert it to grid coordinate without losing any accuracy and that's where the problem is. It is important for the physics calculations i'm doing that I get the most accurate center of mass possible.

4. ### LaidenTrainee Engineer

Messages:
24
If it must be in grid coordinates, then you can try to get blocks with known grid coodinate, that are near the center of mass (in world coodinate), and then interpolate grid position. Every block can give you its world and grid position. Result would be in a fracture block measurement. However, you probably cant do anything with that floating point position - the game just discards it whenever you feed it back to grid positon function. So making your physics calculations in world coordinates may be a more direct way.

5. ### jerry060599Trainee Engineer

Messages:
37
I actually found the solution by copying what keen did in their source code.