Jump to content

Get extact MovingObjectPoition from the Player


Kloonder

Recommended Posts

So I used this code, but its too inaccurate, and because I have only a very small idea what these cos and sin (I know that these return numbers wich are somehow working with the rotation to 360) methods are doing, I can't really change them, sorry for bad Math :/

 

public static MovingObjectPosition getMovingObjectPositionFromPlayer(World world, EntityPlayer entityplayer, boolean flag, double reach)
{
	float f = 1.0F;
	float playerPitch = entityplayer.prevRotationPitch + (entityplayer.rotationPitch - entityplayer.prevRotationPitch) * f;
	float playerYaw = entityplayer.prevRotationYaw + (entityplayer.rotationYaw - entityplayer.prevRotationYaw) * f;
	double playerPosX = entityplayer.prevPosX + (entityplayer.posX - entityplayer.prevPosX) * f;
	double playerPosY = (entityplayer.prevPosY + (entityplayer.posY - entityplayer.prevPosY) * f + 1.6200000000000001D) - entityplayer.getYOffset();
	double playerPosZ = entityplayer.prevPosZ + (entityplayer.posZ - entityplayer.prevPosZ) * f;
	Vec3 vecPlayer = new Vec3(playerPosX, playerPosY, playerPosZ);
	float cosYaw = MathHelper.cos(-playerYaw * 0.01745329F - 3.141593F);
	float sinYaw = MathHelper.sin(-playerYaw * 0.01745329F - 3.141593F);
	float cosPitch = -MathHelper.cos(-playerPitch * 0.01745329F);
	float sinPitch = MathHelper.sin(-playerPitch * 0.01745329F);
	float pointX = sinYaw * cosPitch;
	float pointY = sinPitch;
	float pointZ = cosYaw * cosPitch;
	Vec3 vecPoint = vecPlayer.addVector(pointX * reach, pointY * reach, pointZ * reach);
	MovingObjectPosition movingobjectposition = world.rayTraceBlocks(vecPlayer, vecPoint, false, false, true);
	return movingobjectposition;
}

 

So I have no idea, I know there is a method in entity, but it is only Client side

Creator of Extra Shoes

 

Watch out, I'm total jerk, and I'll troll anybody if it feels like its necessary. Pls report me then

Link to comment
Share on other sites

public static MovingObjectPosition getMovingObjectPositionFromPlayer(World world, EntityPlayer entityplayer, boolean flag, double reach)
{
	Vec3 vecPlayer = entityplsuer.getLookVec();
	MovingObjectPosition movingobjectposition = world.rayTraceBlocks(vecPlayer, vecPoint, false, false, true);
	return movingobjectposition;
}

 

?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Is not magic, but I did miss a line.

 

Vec3 vecPlayer = new Vec3 (entityplayer.posX,entityplayer.posY+entityplayer.eyeHeight,entityplayer.posZ);

 

Edit:

Also misnamed one var.  I am on my tablet atm.

So it should look like this

 

public static MovingObjectPosition getMovingObjectPositionFromPlayer(World world, EntityPlayer entityplayer, boolean flag, double reach)
{
	Vec3 vecPoint = entityplsuer.getLookVec();
	Vec3 vecPlayer = new Vec3 (entityplayer.posX,entityplayer.posY+entityplayer.eyeHeight,entityplayer.posZ);
	MovingObjectPosition movingobjectposition = world.rayTraceBlocks(vecPlayer, vecPoint, false, false, true);
	return movingobjectposition;
}

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Its Minecraft.getMinecraft().objectMouseOver

And that will crash your game when used on the SERVER, which is where the OP is trying to get a MovingObjectPosition. Not only that, but the objectMouseOver is limited in range and thus limited in use.

 

@OP What about the code is not working for you? Works fine for me:

Vec3 vec31 = new Vec3(player.posX, player.posY + player.getEyeHeight(), player.posZ);
Vec3 vec32 = new Vec3(i, j, k); // where i, j, k are coordinates of some place in the world, such as the normalized coordinates from the player's look vector multiplied by some amount indicating maximum range, e.g. 100
MovingObjectPosition mop = world.rayTraceBlocks(vec31, vec32);

However, that will only result in a hit on BLOCKS; if you want to check for an entity within the player's line of sight, this is not the solution.

Link to comment
Share on other sites

I had to do something similar with my PowersAPI mod.

 

This website has a good method for getting the MovingObjectPosition client side. (At any distance as well!)

http://jabelarminecraft.blogspot.com/p/minecraft-modding-extending-reach-of.html

 

Of course, this is only client side, so you'll have to send the position over in a message to the server.

With all due respect, sir: I do, what I do, the way I do it. ~ MacGyver

Link to comment
Share on other sites

Its Minecraft.getMinecraft().objectMouseOver

And that will crash your game when used on the SERVER, which is where the OP is trying to get a MovingObjectPosition. Not only that, but the objectMouseOver is limited in range and thus limited in use.

 

@OP What about the code is not working for you? Works fine for me:

Vec3 vec31 = new Vec3(player.posX, player.posY + player.getEyeHeight(), player.posZ);
Vec3 vec32 = new Vec3(i, j, k); // where i, j, k are coordinates of some place in the world, such as the normalized coordinates from the player's look vector multiplied by some amount indicating maximum range, e.g. 100
MovingObjectPosition mop = world.rayTraceBlocks(vec31, vec32);

However, that will only result in a hit on BLOCKS; if you want to check for an entity within the player's line of sight, this is not the solution.

 

Well, that doen't work, because, you know, look:

 

I'm not quite sure what you mean with those i, j, k, so I assumed I take the players look coords and multiplie them by 100:

 

Vec3 vec31 = new Vec3(player.posX, player.posY + player.getEyeHeight(), player.posZ);
	Vec3 vec32 = new Vec3(player.getLookVec().xCoord*100, player.getLookVec().yCoord*100, player.getLookVec().zCoord*100);
	MovingObjectPosition mop = world.rayTraceBlocks(vec31, vec32);
	System.out.println(mop);

 

That gives back coords, wich are totally wrong.

 

Also:

 

Vec3 vec31 = new Vec3(player.posX, player.posY + player.getEyeHeight(), player.posZ);
	Vec3 vec32 = player.getLookVec(); 
	MovingObjectPosition mop = world.rayTraceBlocks(vec31, vec32);
	System.out.println(mop);

 

This gives back nothing

 

So I have really no idea what to do

Creator of Extra Shoes

 

Watch out, I'm total jerk, and I'll troll anybody if it feels like its necessary. Pls report me then

Link to comment
Share on other sites

This gives back nothing

 

So I have really no idea what to do

 

Try this. I pulled it from one of Jabelar's turorials, who pulled it from Minecraft's mouse over code:

 

 

public static MovingObjectPosition getMouseOverExtended(float dist)
{
    Minecraft mc = FMLClientHandler.instance().getClient();
    Entity theRenderViewEntity = mc.getRenderViewEntity();
    AxisAlignedBB theViewBoundingBox = new AxisAlignedBB(
            theRenderViewEntity.posX-0.5D,
            theRenderViewEntity.posY-0.0D,
            theRenderViewEntity.posZ-0.5D,
            theRenderViewEntity.posX+0.5D,
            theRenderViewEntity.posY+1.5D,
            theRenderViewEntity.posZ+0.5D
            );
    MovingObjectPosition returnMOP = null;
    if (mc.theWorld != null)
    {
        double var2 = dist;
        returnMOP = theRenderViewEntity.rayTrace(var2, 0);
        double calcdist = var2;
        Vec3 pos = theRenderViewEntity.getPositionEyes(0);
        var2 = calcdist;
        if (returnMOP != null)
        {
            calcdist = returnMOP.hitVec.distanceTo(pos);
        }
         
        Vec3 lookvec = theRenderViewEntity.getLook(0);
        Vec3 var8 = pos.addVector(lookvec.xCoord * var2, 

              lookvec.yCoord * var2, 

              lookvec.zCoord * var2);
        Entity pointedEntity = null;
        float var9 = 1.0F;
        @SuppressWarnings("unchecked")
        List<Entity> list = mc.theWorld.getEntitiesWithinAABBExcludingEntity(

              theRenderViewEntity, 

              theViewBoundingBox.addCoord(

                    lookvec.xCoord * var2, 

                    lookvec.yCoord * var2, 

                    lookvec.zCoord * var2).expand(var9, var9, var9));
        double d = calcdist;
            
        for (Entity entity : list)
        {
            if (entity.canBeCollidedWith())
            {
                float bordersize = entity.getCollisionBorderSize();
                AxisAlignedBB aabb = new AxisAlignedBB(

                      entity.posX-entity.width/2, 

                      entity.posY, 

                      entity.posZ-entity.width/2, 

                      entity.posX+entity.width/2, 

                      entity.posY+entity.height, 

                      entity.posZ+entity.width/2);
                aabb.expand(bordersize, bordersize, bordersize);
                MovingObjectPosition mop0 = aabb.calculateIntercept(pos, var8);
                    
                if (aabb.isVecInside(pos))
                {
                    if (0.0D < d || d == 0.0D)
                    {
                        pointedEntity = entity;
                        d = 0.0D;
                    }
                } else if (mop0 != null)
                {
                    double d1 = pos.distanceTo(mop0.hitVec);
                        
                    if (d1 < d || d == 0.0D)
                    {
                        pointedEntity = entity;
                        d = d1;
                    }
                }
            }
        }
           
        if (pointedEntity != null && (d < calcdist || returnMOP == null))
        {
             returnMOP = new MovingObjectPosition(pointedEntity);
        }

    }
    return returnMOP;
}

 

With all due respect, sir: I do, what I do, the way I do it. ~ MacGyver

Link to comment
Share on other sites

Well, I now use this code

 

if(world.isRemote){
					if(player.rayTrace(7, 1) != null){
						if(player.rayTrace(7, 1).hitVec != null){
							MovingObjectPosition mop = player.rayTrace(7, 1);
							Vec3 hit = mop.hitVec;
							int side = mop.sideHit.getIndex();
							MoreMinecraft.network.sendToServer(new MessageToServer(2, hit.xCoord, hit.yCoord, hit.zCoord, side, halfStep ? 1 : 0, rootUp ? 1 : 0));

 

But I somehow have the problem, that it is sometimes wrong about one block in x or z position, any ideas

Creator of Extra Shoes

 

Watch out, I'm total jerk, and I'll troll anybody if it feels like its necessary. Pls report me then

Link to comment
Share on other sites

Well, I now use this code

 

if(world.isRemote){
					if(player.rayTrace(7, 1) != null){
						if(player.rayTrace(7, 1).hitVec != null){
							MovingObjectPosition mop = player.rayTrace(7, 1);
							Vec3 hit = mop.hitVec;
							int side = mop.sideHit.getIndex();
							MoreMinecraft.network.sendToServer(new MessageToServer(2, hit.xCoord, hit.yCoord, hit.zCoord, side, halfStep ? 1 : 0, rootUp ? 1 : 0));

 

But I somehow have the problem, that it is sometimes wrong about one block in x or z position, any ideas

 

Try what I mentioned above. It works, trust me it does.

With all due respect, sir: I do, what I do, the way I do it. ~ MacGyver

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.