Getting object's EntityId from raycast result (Zay-ES)

Hello,

I’m trying to use Zay-ES entity system for a project and i’ve worked with entity component system in other game engines before.i just have a question, is it possible to get An Object’s EntityId from a raycast result ?

How should i know that the result collision or geometry from raycast is for which Entity ?
I Remember there was a function called “GetEntityFromPhysics” in CryEngine for this case is it possible with “Zay-ES” or i have to implement it myself ?

Or maybe i should create a ColliderComponent And loop through all the entities that has that Component and see which one has that “Geometry” or “Collision” ? i’m not sure what’s the right way for doing so in Jmonkeyengine.

Thanks,

In your spatial system, store each entity id in the userdata of their associated spatials. When you make a raycast that collides with a geometry, iterate up the spatial hierarchy until you encounter a spatial that contains an entity id.

public EntityId getId(Spatial spatial) {
    while (spatial != null) {
        Long id = spatial.getUserData("EntityId");
        if (id != null) {
            return new EntityId(id);
        }
        spatial = spatial.getParent();
    }
    return null;
}
4 Likes

Thank you.

One more question if you don’t mind. :ninja:

I have two components like “UnitControllerComponent” which adds a BetterCharacterController to entity and does navigation and other stuffs and Another component called “UnitAninmationComponent” which adds the character mesh to entity and does animation stuffs.

But if i move CharacterController , character’s mesh/geometry doesn’t move anymore.because “UnitAninmationComponent” have no idea about other components.should i create a geometry cache or something inside entity class and add control to all of them or there is a better way for dealing with this problem too ? (when you have multiple mesh / geometry on an entity)

Components are just data. No logic.

So when I see things like ‘“UnitControllerComponent” which adds a BetterCharacterController’… I wonder what that means exactly.

Some system should be doing the adding. The component is just data.

2 Likes

I’m using “UnitControllerComponent” for entities that have ability to find path around the world or navmesh or whatever it is.but the component itself will use a system called NavigationSystem for pathfinding inside the component i will probably just follow the path.if that makes sense.

There was something like this in CryEngine called “EntityNavigationComponent” or something like that it had a function called “NavigateTo” which finds the path and returns a velocity for following the path which you could use on the CharacterController.i was trying to do something like that.(same with animationComponent which is in there too i’m not that much of the game dev myself but i’ve been working with CryEngine for a few years and my mind kinda works the way that engine used to work i think).

And i think i kind solved the problem above, i just created a node for each entity and if i add control to this node all the meshes will move too

Your life with “entity systems” will be much easier in the long run if you follow some simple rules with components:

  1. data only
  2. immutable whenever possible

Game logic should only be in system level code.

3 Likes