Jump to content

[1.7.10] Change player position


LegendLength

Recommended Posts

I am trying to move the player by a small amount in x,y,z during each game tick.  I have done various searches on this forum and stepped through the code but still struggling to find the correct way to do it.

 

Currently I'm able to set the player's motion during the player tick end event and it works well.  But if I try to set position instead then the player will bounce a bit in the direction and then reset back.  I assume it's not getting sent to the server.

 

I also tried player.setPositionAndUpdate() but I believe that is for when you are teleporting a player.  The aim of this is for normal movement (trying to implement physics on the player using a certain type of numerical integration , hence the need to set position rather than motion).

 

Feel free to offer a solution for a higher minecraft version instead.

Link to comment
Share on other sites

Player position is determined by the server.

Player motion is determined by the client (and sent to the server which the server then uses to update the player's position).

 

So if you want to use .setPositionAndUpdate you have to do it on the server.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Thanks.  I did test test player.setPostionAndUpdate() but wonder if it's the 'correct' way.  I noticed there was no interpolation when doing it this way.

 

From my research it seems that method is used mainly for setting the player's position during a teleport event.  Do you think it will still handle interpolation, collision detection etc. properly?  I'm also not sure if it is the right way because it seems to send a network update straight away, whereas using MotionX etc. may do network updates a little differently.

Link to comment
Share on other sites

Thanks.  I did test test player.setPostionAndUpdate() but wonder if it's the 'correct' way.  I noticed there was no interpolation when doing it this way.

 

No there's not because interpolation is handled by the client's

motion

values.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I think I found a solution so i'll give some details in case anyone needs it in future:

 

The goal is to move the player by position rather than using motion.

 

The problem is if you directly call something like setPositionAndUpdate() you don't get the benefit of minecraft's position interpolation, among other things.

 

After looking through the movement code I saw that the Entity.motionX fields are simply added to the Entity.posX fields every tick.  There are no special physics derivations etc. going on.

 

This means you can simply set motionX to be your position delta and the game will move the player by that amount.  It allows you to use any kind of physics system you want because most of the advanced ones (like Runge Kutta) require you to adjust position, rather than velocity.

 

So it would look something like this:

 

@SideOnly(Side.CLIENT)
@SubscribeEvent
public void onPlayerTick(TickEvent.PlayerTickEvent event) {

   if (event.player.world.isRemote) {
      if (event.phase == TickEvent.Phase.END) {
         EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer;

         // calculate all the physics you need
         Vector3F deltaPosition = myCalculateDelta();

         // you can store current velocity in your own variable somewhere

         // move player
         player.motionX = deltaPosition.x;
         player.motionY = deltaPosition.y;
         player.motionZ = deltaPosition.z;
      }
   }
}

Link to comment
Share on other sites

Just don't use this: EntityClientPlayerMP, generally speaking - you should aim at lowest abstraction layer possible - EntityPlayer (EntityPlayerMP for server or AbstractClientPlayer for client if you need).

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Also, PlayerTickEvent is most definitely NOT client-side only. Please read this post on @SideOnly.

 

As for movement, there is no interpolation at all (built in, at least) if you use any kind of #setPosition-type functionality, but there is if you set player motion. Adding / setting player motion is probably the best way.

Link to comment
Share on other sites

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.

×
×
  • Create New...

Important Information

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