skadistats / clarity-examples

Example code for clarity
BSD 3-Clause "New" or "Revised" License
113 stars 37 forks source link

Coordinates for a given in-game entity #22

Closed TodorovicF closed 8 years ago

TodorovicF commented 8 years ago

So I've found the property CBodyComponent.m_vec[X,Y,Z] but I'm struggling to see how these numbers relate to the actual game field. When I look at the CWorld entity's m_WorldMins/m_WorldMaxs properties, I get [-8391.375, -8320.0, -1072.4688] for mins and [9216.0, 8323.8125, 3584.0] for maxs, while m_vec data for any given entity never rises above around 250, and is never negative. Is there another property to tell an entity's position on the game field or a way to convert this value? Thanks!

spheenik commented 8 years ago

I forgot about this, I wanted to share this:

(it's from source 1, and code for clarity 1, but it should be portable to clarity 2)

    private static final int MAX_COORD_INTEGER = 16384;

    public static float getVecOrigin(Entity e, int idx) {
        Object v = e.getProperty("m_vecOrigin");
        if (v instanceof Vector2f) {
            float[] v2 = new float[2];
            ((Vector2f) v).get(v2);
            return v2[idx];
        } else if (v instanceof Vector3f) {
            float[] v3 = new float[3];
            ((Vector3f) v).get(v3);
            return v3[idx];
        } else {
            throw new RuntimeException("unsupported vector found");
        }
    }

    public static float getX(Entity e) {
        int cellBits = e.getProperty("m_cellbits");
        int cellX = e.getProperty("m_cellX");
        return cellX * (1 << cellBits) - MAX_COORD_INTEGER + getVecOrigin(e, 0) / 128.0f;
    }

    public static float getY(Entity e) {
        int cellBits = e.getProperty("m_cellbits");
        int cellY = e.getProperty("m_cellY");
        return cellY * (1 << cellBits) - MAX_COORD_INTEGER + getVecOrigin(e, 1) / 128.0f;
    }