Jump to content

Recommended Posts

Posted

OK,

So I'm trying to set the properties of a tile entity by the speed of the fastest entity near to it. I have the following method setup, (code is in Scala but I can translate if required)


  override def update(): Unit = {
    if (!this.getWorld.isRemote) {

      val ents = this.getWorld.getEntitiesWithinAABB(classOf[EntityLivingBase], new AxisAlignedBB(getPos.getX-3, getPos.getY-3, getPos.getZ-3, getPos.getX+3, getPos.getY+3, getPos.getZ+3))

      val speeds: List[Double] = ents.map(e => {
        val vx = e.motionX
        val vy = e.motionY
        val vz = e.motionZ
        println(List(vx, vy, vz))

        vx**2 + vy**2 + vz**2
      }).sortWith((d0, d1) => d0 > d1).toList

      val speed: Double = speeds.headOption.getOrElse[Double](0) ** .5
      powerLevel = Math.min(speed * 15, 15).toInt
      println(powerLevel)

      var state = BlockMotionSensor.getDefaultState
      state = state.withProperty[integer, Integer](ModBlocks.meta, powerLevel)

      this.getWorld.setBlockState(this.getPos, state)
    }
  }

 

for my entity player (only tested so far on single player) velocities are printed as

List(0.0, -0.07544406518948656, 0.0)

, with y increasing if I jump, but x and z never change. Testing on pigs (we do not fear animal testing here) showed that pigs do have velocity in all three directions using the above code. Any suggestions on what I'm missing? or is this a bug in the latest forge (1.9-12.16.0.1816)

Posted

OK, so there is a motionX/Z when the player jumps while sprinting... I'm guessing that the payer motion when walking on the ground or jumping normally is just dictated by a setPos from the key bindings (maybe?) any ideas for a work around?

Posted

Hi

 

I think the most robust solution might be to have your TileEntity keep track of each nearby entity's location from tick to tick, and calculate the speed from that.

 

eg - in your TileEntity tick method

(1) look for nearby entities

(2) store all nearby entities in a map along with their current [x,y,z].

(3) compare the entity positions this tick with the positions from last tick  -> calculate the speed.

 

motionX, motionY, motionZ don't always give reliable answers because some entities have custom movement (like the player for example).

 

-TGG

 

 

Posted

Awesome. tried this last night and it didn't as the block was getting replaced, and therefore so was the tile entity. changed the code so that rather than doing it from the state it reads the value from the tile entity and updates the neighbors. I have to keep an array of the previous values though to try and smooth it out a bit, otherwise it flickers on and off quite a bit

 

  var powerLevel: Int = 0
  var prevEnts: Map[EntityLivingBase, Vec3d] = Map()
  var postMax: List[Double] = new Array(10).toList

  override def update(): Unit = {
    if (!this.getWorld.isRemote) {
      val entList: List[EntityLivingBase] = getWorld.getEntitiesWithinAABB(classOf[EntityLivingBase],
        new AxisAlignedBB(this.getPos.getX-4, this.getPos.getY-4, this.getPos.getZ-4,this.getPos.getX+4, this.getPos.getY+4, this.getPos.getZ+4)).toList

      val posList: Map[EntityLivingBase, Vec3d] = entList.map(e => (e, new Vec3d(e.posX, e.posY, e.posZ))).toMap
      val speedList: List[Double] = posList.map(kvp => (kvp._2-prevEnts.getOrElse(kvp._1, kvp._2)).lengthVector()).toList.sortWith((d0, d1) => d0 > d1)

      val max: Double = speedList.headOption.getOrElse(0)
      postMax = postMax.tail :+ max

      var mean: Double = 0
      postMax foreach(d => mean += d/postMax.length)
      println(mean)
      powerLevel = (mean * 30).toInt
      println(powerLevel)

      prevEnts = posList

      this.getWorld.notifyNeighborsOfStateChange(this.getPos, this.getBlockType)
    }
  }

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.