Jump to content

3D Vector projection not working on Minecraft Forge...


Player131

Recommended Posts

Hello,

 

I'm trying to make a system like radar i made on GMod using 3D Vector projection. However it seams that Minecraft Forge refuses my formula.

 

I'm trying to transpose every entities 3D World coords into a 2D screen position, to be able to render red or green points in a box of 64x64 in the bottom right corner of the screen, here is my current code.

 

List<Entity> l = EntityFinder.findEntitiesInRayon(EntityType.LIVING, mc.thePlayer, 30, false); // This is a fuction i made using old classes of Minecraft 1.6, i reimplemented.
            drawString(mc.fontRenderer, "Entities arround you : " + l.size(), 5, 30, 0xE6FF00);

            ScaledResolution resolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);
            int j = resolution.getScaledWidth();
            int k = resolution.getScaledHeight();

            drawScreenAlphaColoredRect(j - 64, k - 64, 64, 64, new int[]{133, 133, 133, 16});//This function draws any rectangle you want at coords xy, with width and height. It supports alpha values too.
            for (Entity e : l){
                int[] xy = get2DFrom3D((float)e.posX, (float)e.posY, (float)e.posZ, resolution);
                int x = xy[0];
                int y = xy[1];
                drawScreenAlphaColoredRect(x, y, 2, 2, new int[]{255, 0, 0, 255});
            }

 

The get2DFrom3D void (it would more interesting if mc.entityRenderer.cameraZoom would be public) :

   public int[] get2DFrom3D(float x, float y, float z, ScaledResolution resolution) {
        int centerX = resolution.getScaledWidth() / 2;
        int centerY = resolution.getScaledHeight() / 2;
        Double d = ObfuscationReflectionHelper.getPrivateValue(EntityRenderer.class, FMLClientHandler.instance().getClient().entityRenderer, "cameraZoom");
        double zoom = d;
        int u = (int) (x / z * zoom + centerX);
        int v = (int) (y / z * zoom + centerY);

        return new int[]{u, v};
    }

 

Link to comment
Share on other sites

I updated the code...

 

Now it's a little bit more good... Indead, i just used the code i was using on Garry's Mod Lua to make minimaps, but it seams that all coords systems are screwed up on Minecraft Forge...

 

The new function get2DFrom3D

    public int[] get2DFrom3D(float x, float y, float z, ScaledResolution resolution, EntityPlayer player) {
        int centerX = resolution.getScaledWidth() - 30;
        int centerY = resolution.getScaledHeight() - 30;

        Vec3 pos = Vec3.createVectorHelper(player.posX - x, player.posY - y, player.posZ - z);
        // / (mc.gameSettings.mouseSensitivity * 100)
        pos.rotateAroundY(-1 * (player.rotationYaw));
        int x1 = (int) (centerX + pos.yCoord / 5);
        int y1 = (int) (centerY + 0.6 * pos.xCoord);
        return new int[]{x1, y1};
    }

Link to comment
Share on other sites

Hi

 

The coordinate system that minecraft uses is shown on this page http://greyminecraftcoder.blogspot.co.at/2014/12/blocks-18.html

 

By 3D to 2D, do you mean you want an overhead view?  i.e. someone looking straight down from up high?

 

If so, your code looks about right except that your 64x64 rectangle should actually be a circle.  I would suggest you make sure to clip the result to a valid range (i.e. cull if it's outside the valid range) before drawing. 

 

Does it work how you expect or is there a problem?

 

-TGG

 

Link to comment
Share on other sites

So i made two videos to show what the problem is. Note that the code i'm using in GMod Lua is based of the CAP one, so i give you the CAP code.

 

Here is what it should look like (don't care of textures or positions, they aren't corresponding to my mod) :

 

Here is what it renders on my Minecraft Mod. You can see that on my mod, sometimes you have position moving from up to down relative to yellow point (you), and sometimes it makes the contrary (witch is completely unexpected)

 

And at least the updated code on my mod :

    public float[] get2DFrom3D(float x, float y, float z, ScaledResolution resolution, EntityPlayer player) {
        int centerX = resolution.getScaledWidth() - 30;
        int centerY = resolution.getScaledHeight() - 30;

        Vec3 supperPos = Vec3.createVectorHelper(x, y, z);
        Vec3 playerPos = Vec3.createVectorHelper(player.posX, player.posY, player.posZ);
        Vec3 pos = playerPos.subtract(supperPos);
        // / (mc.gameSettings.mouseSensitivity * 100)
        float rotation = (float) ((double) ((player.rotationYaw * 4F) / 360F) + 0.5D);
        pos.rotateAroundY(-1 * (rotation));
        float x1 = (float) (centerX + pos.xCoord / 5);
        float y1 = (float) (centerY + 0.6 * pos.zCoord);
        return new float[]{x1, y1};
    }

 

And finaly the code that CAP uses (Pasted from their Github, and comented by me)

         for k, v in pairs(ents.GetAll()) do //Don't care it's a loop over every world entities
		if v:IsNPC() or v:IsPlayer() then //If this is a form of EntityLiving, don't care
			local ang = ply:GetAngles(); //There you go, interesting code starts here,
			local pos = ply:GetPos() - v:GetPos();
			pos:Rotate(Angle(0, -1*ang.Yaw, 0));
			local x1 = 256 + pos.y/5;
			local y1 = 512 + 0.3*pos.x; //And ends here
			if (math.abs(pos.z)<200) then
				surface.DrawTexturedRect(x1-16, y1-24, 32, 48);
			end
		end
	end

Link to comment
Share on other sites

I was having trouble from your video trying to figure out what was happening; maybe it would be better to test your code using a few "fake" fixed entity positions.

 

Are you sure your calculation of rotation is correct?  It looks like you are calculating a quadrant number, but rotateAroundY uses radians.

 

-TGG

Link to comment
Share on other sites

Ok, i found the original cause of the left/right bug, it's because of the rotation everytime i rotate, x and z position aren't updated correctly with the player yaw rotation...

 

Here is my new code without rotation (working, but difficult to see where entities are coming from) :

    public float[] get2DFrom3D(float x, float y, float z, ScaledResolution resolution, EntityPlayer player) {
        int centerX = resolution.getScaledWidth() - 30;
        int centerY = resolution.getScaledHeight() - 30;

        Vec3 supperPos = Vec3.createVectorHelper(x, y, z);
        Vec3 playerPos = Vec3.createVectorHelper(player.posX, player.posY, player.posZ);
        Vec3 pos = playerPos.subtract(supperPos);
        //float rotation = (float) ((double) ((player.rotationYaw * 4F) / 360F) + 0.5D);
        //pos.rotateAroundY(-1 * (rotation));
        float x1 = (float) (centerX + pos.zCoord / 1);
        float y1 = (float) (centerY + 0.7 * pos.xCoord);
        return new float[]{x1, y1};
    }

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.