Comparing floats
In general I suggest to avoid equal compare (==
) on float values. The likelihood to get the exact match is pretty low.
Even when it is very close and visually there is no difference the values are not the same.
It is better to do a range check with <
and >
.
Components
You might argue that you compare a vector with a list. This kind of compare will perform a compare of the components (content of the container). As the components are float consider the first section (Comparing floats).
Unfortunately you need more range checks right now. Luckily you can reduce the effort by reviewing your requirements.
Comparing position vectors
Position vectors describe a location referred by a shared origin.
To compare two position vectors I recommend to check if the distance between them is below a certain range.
You can do that by calculating the vector between this two positional vectors and check it's length:
import mathutils
import bge
owner = bge.logic.getCurrentController().owner
positionToCheck = owner.worldPosition
positionToCompareTo = mathutils.Vector([1,2,3])
distance = (positionToCheck - positionToCompareTo).length
nearDistance = 0.000001
if distance < nearDistance:
print("the positions are nearly equal")
else:
print("the positions are too different")
Comparing single components
Rather than comparing the whole container (with all it's content) you get the component from the container and perform the compare on that component only. As the component is still float consider the first section (Comparing floats).
Example:
if abs(owner.worldPosition.x - 1.0) < 0.000001:
print("the positions are nearly equal")
else:
print("the positions are too different")
Be aware this ignores the y and z coordinates completely. Therefore this is a check if the position is near the YZ-Plane at X=1.0.
LocalPosition vs. WorldPosition
I hope you noticed that I defined position vectors as being referred from a shared origin.
A common vector has no position. It can be anywhere. Therefore a vector alone can't describe a position. You always need the origin where the vector points from.
Typically this origin (or reference point) is implicitly provided via context.
When you use KX_GameObject.worldPosition
you get a vector that describes the position from the scene origin (0,0,0)
.
When you use KX_GameObject.localPosition
you get a vector that describes the position from the object's parent's position.
This means the vector of worldPosition
might be equal (or nearly equal) to the vector of localPosition
, but as the origins differ, they do not describe the same point in space.
This also belongs to objects with different parents.
Remark
The origin is not the only context. You need to consider orientation and scale too. This is typically described by the coordinate system or space (scene space, parent space).
Imagine you turned the parent. localPosition
did not change, but worldPosition
did due to the rotation of the referred coordinate system. The parent space turned with the parent.