Jump to content

Recommended Posts

Posted

Well, like the title says, I'm trying to initiate a short range teleport (limited 3-5 blocks) in the direction that the player is facing. It would ideally be usable in any direction, including upwards, but I'm having some difficulty getting the block to teleport them to. I tried using the rayTrace method contained within EntityLiving but that only works if there is a block targeted and also doesn't work server-side so isn't really applicable.

Have a modding question? PM me and hopefully I'll be able to help. Good at 2d Pixel Art? We need your help!  http://www.minecraftforum.net/topic/1806355-looking-for-2d-pixel-artist/

Posted

Well, like the title says, I'm trying to initiate a short range teleport (limited 3-5 blocks) in the direction that the player is facing. It would ideally be usable in any direction, including upwards, but I'm having some difficulty getting the block to teleport them to. I tried using the rayTrace method contained within EntityLiving but that only works if there is a block targeted and also doesn't work server-side so isn't really applicable.

Use instead player.moveEntity(distance*sin(player.pitch)*sin(player.yaw),distance*cos(player.pitch),distance*sin(player.pitch)*sin(player.yaw));

If it's moving at odd angles try switching x and z.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Posted

You got me on the right track with that moveEntity method, so thanks!

In the end, I wound up having to do:

 

 
            int distance = 5;
            float f1 = MathHelper.cos(-this.rotationYaw * 0.017453292F - (float)Math.PI);
            float f2 = MathHelper.sin(-this.rotationYaw * 0.017453292F - (float)Math.PI);
            float f3 = -MathHelper.cos(-this.rotationPitch * 0.017453292F);
            float f4 = MathHelper.sin(-this.rotationPitch * 0.017453292F);
            double i = this.posX;
            double j = this.posY;
            double k = this.posZ;
            this.moveEntity(distance*f2*f3, 
				distance*f4,
				distance*f1*f3);

 

So for anyone interested, this is how I had to do it. It works great! :D

Have a modding question? PM me and hopefully I'll be able to help. Good at 2d Pixel Art? We need your help!  http://www.minecraftforum.net/topic/1806355-looking-for-2d-pixel-artist/

Posted

Use instead player.moveEntity(distance*sin(player.pitch)*sin(player.yaw),distance*cos(player.pitch),distance*sin(player.pitch)*sin(player.yaw));

If it's moving at odd angles try switching x and z.

 

Is the function generally player.moveEntity(x,y,z)?

Which pivot (for yaw and pitch) is which axes? I have done bits of code for mods of different games and the pivots are not usually with the same axes.

 

I know that I could of made a new post but you seemed to have answered the his/her question clearly.

 

Many thanks, Melonize

Posted

Use instead player.moveEntity(distance*sin(player.pitch)*sin(player.yaw),distance*cos(player.pitch),distance*sin(player.pitch)*sin(player.yaw));

If it's moving at odd angles try switching x and z.

 

Is the function generally player.moveEntity(x,y,z)?

Which pivot (for yaw and pitch) is which axes? I have done bits of code for mods of different games and the pivots are not usually with the same axes.

 

I know that I could of made a new post but you seemed to have answered the his/her question clearly.

 

Many thanks, Melonize

Yes. Y is up. I couldn't remember whether yaw starts from x or z, which is why I mentioned that he might have to switch the two. And it looks like I made a typo anyway. In Minecraft:

X = cos(yaw)sin(pitch)

Y = cos(pitch)

Z = sin(yaw)sin(pitch)

I think. Unfortunately, I can't check with Mojang code because they overcomplicate things ( -cos(x) = -cos(-x) = cos(-x+π), even though none of those should be necessary for such formulas), but the only error should be a switch in X and Z.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Posted

Use instead player.moveEntity(distance*sin(player.pitch)*sin(player.yaw),distance*cos(player.pitch),distance*sin(player.pitch)*sin(player.yaw));

If it's moving at odd angles try switching x and z.

 

I find that moving the player at a slope into the ground causes the player to slide further as your y travel is abruptly blocked by the ground but your horizontal is still full length of what it needs to go for the sloped travel if not blocked e.g. falling.

So here is the fix for it:

try{
// Trace for blocks
MovingObjectPosition eyeTrace = player.rayTrace(distance, 1.0F);
// If no blocks are traced, then eyeTrace is null.
if(eyeTrace.hitVec != null){
	//If a block is found, call the moveEntity below
	player.moveEntity(eyeTrace.hitVec.xCoord-player.posX, eyeTrace.hitVec.yCoord-player.posY + 1.1, eyeTrace.hitVec.zCoord-player.posZ ); 
	}
}
catch(NullPointerException npe){
	// Lolz at your NPE, going to throw you whatever set distance forward in the hope to ignore your NPE
	player.moveEntity(-distance*Math.sin(Math.toRadians(player.rotationYawHead))*Math.cos(Math.toRadians(player.rotationPitch)),-distance*Math.sin(Math.toRadians(player.rotationPitch)), distance*Math.cos(Math.toRadians(player.rotationYawHead))*Math.cos(Math.toRadians(player.rotationPitch)));
}

Posted

Use instead player.moveEntity(distance*sin(player.pitch)*sin(player.yaw),distance*cos(player.pitch),distance*sin(player.pitch)*sin(player.yaw));

If it's moving at odd angles try switching x and z.

 

I find that moving the player at a slope into the ground causes the player to slide further as your y travel is abruptly blocked by the ground but your horizontal is still full length of what it needs to go for the sloped travel if not blocked e.g. falling.

So here is the fix for it:

try{
// Trace for blocks
MovingObjectPosition eyeTrace = player.rayTrace(distance, 1.0F);
// If no blocks are traced, then eyeTrace is null.
if(eyeTrace.hitVec != null){
	//If a block is found, call the moveEntity below
	player.moveEntity(eyeTrace.hitVec.xCoord-player.posX, eyeTrace.hitVec.yCoord-player.posY + 1.1, eyeTrace.hitVec.zCoord-player.posZ ); 
	}
}
catch(NullPointerException npe){
	// Lolz at your NPE, going to throw you whatever set distance forward in the hope to ignore your NPE
	player.moveEntity(-distance*Math.sin(Math.toRadians(player.rotationYawHead))*Math.cos(Math.toRadians(player.rotationPitch)),-distance*Math.sin(Math.toRadians(player.rotationPitch)), distance*Math.cos(Math.toRadians(player.rotationYawHead))*Math.cos(Math.toRadians(player.rotationPitch)));
}

I had a feeling you would run into that; I wasn't quite sure what to do about it. But thanks for posting your solution!

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

  • 3 years later...
Posted

I know I'm late to the party, but this was the most elaborate work done on the subject I could find, so if there is anyone left listening, can someone help me make an item that does this? The code is not working and I barely understand what it's doing.

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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