Bonjour a tous,

Je suis actuellement en train de créer un jeu 3D et j'ai besoin d'avoir une fonction qui me retourne l’entité que je regarde, mais je n'est pas réussi a le faire.

Voici mon code actuel:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public Entity rayTrace()
    {
        if(entity instanceof EntityPlayer)
        {
            Vector3f cam = ((EntityPlayer)this.entity).getCameraPos();
            float distanceZ = (float)(Math.cos(Math.toRadians(this.entity.getRotY())) * 0.3f);
            float distanceX = (float)(Math.sin(Math.toRadians(this.entity.getRotY())) * 0.3f);
            Vector3f playerDirection = new Vector3f(cam.x + distanceX, cam.y, cam.z + distanceZ);
            Vector3f playerView = MathHelper.coordinate(cam, playerDirection);
 
            for(Entity entity : Entity.entityList)
            {
 
                for(int k = 0; !entity.hitBox.collideWithVector(playerView); k++)
                {
                    playerView = MathHelper.coordinate(cam.translate(k, 0, k), playerDirection.translate(k, 0, k));
                    if(entity.hitBox.collideWithVector(playerView))
                    {
                        System.out.println(entity);
                        return entity;
                    }
                    if(k>10)
                        break;
                }
            }
        }
        return null;
    }
 
public boolean collideWithVector(Vector3f vec)
    {
        return(vec.x > this.minX && vec.x < this.maxX && vec.y > this.minY && vec.y < this.maxY && vec.z > this.minZ && vec.z < this.maxX);
    }
les methode utiliser dans la classe MathHelper:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
public static Vector3f coordinate(Vector3f vec1, Vector3f vec2)
    {
        return new Vector3f(vec2.x - vec1.x, vec2.y - vec1.y, vec2.z - vec1.z);
    }