Jump to content

[1.7.10] Getting item to drop directly in front of player no matter what


Recommended Posts

Posted

Hi everyone,

 

I guess this is more of a math question really, I want to drop an item directly in front of my player no matter what his position is or rotation. I have the players position x1,y1,z1 and I want to drop the item at x2,y2,z2 so that these coordinates (where i drop the item) are always located directly in front of my player. The player has a rotation yaw and rotation pitch I was wondering if there is a way to use these values to determine the "forward coordinates" x2,y2,z2 where my item should drop so that they always exist in front of the player. Any ideas?

Posted

I believe the easiest way to do it is to use vectors. Minecraft has a 3D vector class called Vec3 which basically just contains an x, y, z but also includes methods for adding, normailizing, etc.

 

Anyway, there is a thing called the "look vector" which Minecraft maintains that indicates a normailzed (length of 1) vector in the direction the entity is facing. So if you want to find a position in front of an entity, the math is pretty simple:

 

1) Make a new Vec3 using the player position.

2) Get the player look vector multiplied by the distance you want it to be in front of the player.

3) Add the two vectors from step #1 and #2 together -- the x, y, z will be a position in front of the player.

 

Note that in 1.7.10 to create a new Vec3 you have to use the vector helper method, but in 1.8 you can just construct them directly.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

also where would I access the look vector is that obtained by using

 MovingObjectPosition mop= player.rayTrace(5, 1.0F);

(cause this is client side only and I need this vector on server side) is there a way to get the look vector server side to avoid a packet.

 

and ya to make a vector in 1.7.10 you need to do

Vec3 vec = Vec3.createVectorHelper(player.posX, player.posY, player.posZ);

Posted

In not sure about th emath but this is definately wrong...

                                
                                Vec3 lookVector = player.getLookVec();
			lookVector.xCoord = lookVector.xCoord * 1.5;
			lookVector.yCoord = lookVector.yCoord * 1.5;
			lookVector.zCoord = lookVector.zCoord * 1.5;
			lookVector.addVector(player.posX, player.posY, player.posZ);

 

I want the distance to be 1.5 away from the player but it isnt

Posted

You don't want a vector, though, you want world coordinates: add the vector coordinates to the player's position, not the player's position to the vector:

Vec3 look = player.getLookVec();
float distance = 1.5F;
double dx = player.posX + (look.xCoord * distance);
double dy = player.posY + player.getEyeHeight(); // notice I'm not using the vector here: if the player is looking straight down, the item would end up in the ground
double dz = player.posZ + (look.zCoord * distance);
// now spawn your entity with (dx, dy, dz) as its position

Posted

You don't want a vector, though, you want world coordinates: add the vector coordinates to the player's position, not the player's position to the vector.

 

Actually, adding the vectors works too. A Vec3 is really just an x, y, z coordinate. It is a "vector" if you consider it's origin to be relative, but it is an absolute position relative to the world origin. So the way he added the vectors would work as well. The final position would just be the xCoord, yCoord, zCoord of the resulting vector.

 

But I did forget to discard the Y part of the vector. Good catch.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.