Tuesday, 13 January 2015

Minecraft API - Player's Direction

One of the questions I get asked a lot about the Minecraft: Pi edition APi is "how can I get the direction the player is facing?" and I have always had to say "sorry you can't do that".

While I can't change the API for Minecraft: Pi edition I can change RaspberryJuice - so I decided to add functions to allow you to find out where the player is looking.  You can download the RaspberryJuice plugin Canarymod and Bukkit.

I have also created a new Adventures in Minecraft starterkit which includes the new version of RapsberryJuice and everything you need to use the new api functions.



The 3 new functions in the api are:
  • player.getRotation() - return the angle of rotation between 0 and 360
  • player.getPitch() - returns the angle of pitch between -90 and 90
  • player.getDirection() - returns a unit-vector of x,y,z pointing in the direction the player is facing
The functions also work with the entities as well so you can use entity.getRotation(), entity.getPitch() and entity.getDirection()

Here are the couple of code examples I demo in the video.

Get the players rotation and pitch angles:
#import the minecraft module
import mcpi.minecraft as minecraft

#create a connection to minecraft
mc = minecraft.Minecraft.create()

while True:
    #get the players rotational angle
    angle = mc.player.getRotation()
    #get the player up and down angle
    pitch = mc.player.getPitch()
    mc.postToChat(str(angle) + " : " + str(pitch))

Create a block in front of the player using getDirection():
import mcpi.minecraft as minecraft
import mcpi.block as block
import time

#how far in front of the player the block will be
BLOCKDISTANCE = 5

mc = minecraft.Minecraft.create()

while True:
    #get the position
    pos = mc.player.getPos()
    #get the direction
    direction = mc.player.getDirection()
    #calc the position of the block in front of the player
    x = round(pos.x + (direction.x * BLOCKDISTANCE))
    y = round(pos.y + (direction.y * BLOCKDISTANCE) + 1)
    z = round(pos.z + (direction.z * BLOCKDISTANCE))
    mc.setBlock(x,y,z,block.DIAMOND_BLOCK)
    time.sleep(0.1)
    mc.setBlock(x,y,z,block.AIR)

I hope you find the new api functions useful.

7 comments:

  1. I've posted some code on the Adventures in Minecraft forum, showing how you can approximate direction in Minecraft Pi Edition. It's very rough, its only an approximation based on monitoring changes in the player position, but it's good enough for some purposes.

    David Whale
    @whaleygeek

    ReplyDelete
    Replies
    1. And here is a link to David's post - http://www.stuffaboutcode.com/p/adventures-in-minecraft-forum.html?place=msg%2Fadventures-in-minecraft-forum%2Fxj6_42BrBAY%2FrEuwnB5gv98J

      Delete
  2. I have installed the latest version of RasperryJuice on my server but where can I download the updated Python module with the new functions from?

    I got mine from https://github.com/brooksc/mcpipy

    Thanks.

    ReplyDelete
    Replies
    1. You can get it from the raspberry juice git hub repo - https://github.com/zhuowei/RaspberryJuice/tree/master/src/main/resources/mcpi/api/python/mcpi

      Delete
  3. Is there a way to do this on the original minecraft pi, that comes with the Raspberry pi

    ReplyDelete
  4. oh well... Going to look into david's toutorial. See if it works.

    ReplyDelete

Note: only a member of this blog may post a comment.