Posted June 1, 201510 yr 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?
June 1, 201510 yr 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/
June 1, 201510 yr Author 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);
June 2, 201510 yr Author found it for those that are reading you can get the look vector on server by doing Vec3 vec3 = player.getLookVec();
June 2, 201510 yr Author 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
June 2, 201510 yr 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 http://i.imgur.com/NdrFdld.png[/img]
June 2, 201510 yr 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.