How to get player location

How do I get the player location using the code from the Hello Collision tutorial?

I’m assuming it has something to do with “player.getPhysicsLocation()” but I’m not sure

player.getPhysicsLocation() should work.

I tried this but it didn’t seem to work. Is there something wrong?

 if(player.getPhysicsLocation().equals(new Vector3f(0.0f, 0.0f, 0.0f)){
   // some action
}

Is there something wrong?

That’s a very vague question.

I suggest you explain:

  • what you are trying to do,
  • what you expected to happen, and
  • what actually happened.
1 Like

That if statement feels like it would almost always be false. Youre asking if the player is precisely at the origin. Why not print out the position to see if it seems like what you expect

1 Like

Sorry for being vague.

I’m trying to see if a player has entered a certain area. I expected the boolean to return as true when I entered the area but it did not work.

Someone else said that I used the precise location(retrospectively that was kind of a blunder by me). What would be the best way to implement this?

1 Like

There’s a few ways you could do this.

The simplest way I like to do this is to use a BoundingBox or BoundingSphere to represent the area, and then check that against the player’s location (or the player’s model bounds). Something like :

BoundingVolume area = new BoundingSphere(sphereCenterLocation, sphereRadius);
if(area.contains(player.getPhysicsLocation())){
    // player is in area
}

You could also check if 2 bounding volumes overlap, in the case that you want to represent your player with a BoundingBox instead of a single point

if(area.intersects(palyerModel.getWorldBound())){
 // player is in area
}

And even more simple way would be to just check the distance between the player’s location and the target location, which essentially does the same thing as using a BoundingSphere with a radius

float distance = player.getPhysicsLocation().distance(areaCenterLocation);
if(distance < areaRadius){
    //player in area
}

But these simple ways will only work with a square or circular shape, which seems to be sufficient in most cases from my experience.

For an irregular shaped area you could use a group of boundingVolumes, or an irregular shaped geometry to represent the area, in which case you’d also need to do slightly more advanced collision checking to get precise results, but it is still possible.

2 Likes

Btw, this may sometimes work, but most of time won’t, because your object as a player will never reach (0,0,0), it may reach (0.0000111, 0.00231, 0.00041) for example and other fraction versions of it, although in jme world you will visualize it as reached the target…that’s why it didn’t work, the right way is to either check the distance or build a range difference and check it regularly as Ryan suggests, sometimes comparing things to a constant is a bad practice (honestly i was doing this in the past).

1 Like

The above answers are “strict” to your question.

But here i come with answer that is beyound question here.

Fact is that ECS written to keep track of Entity Location component should be used in this case.
If your game gonna be medium or larger, not just small one, its a way to go.

1 Like