[NMS] Minecart Physics

Discussion in 'Plugin Development' started by MoeMix, Mar 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    MoeMix

    Hi bukkit devs. I'm in a bit of a dilemma right now regarding Minecart physics. I have created a custom minecart and it works fine however, it doesn't obey gravity so it just floats in the air even when there is ground right under it.

    [​IMG]

    Here is the code for the custom minecart...
    Code:java
    1.  
    2. import java.math.RoundingMode;
    3. import java.text.DecimalFormat;
    4.  
    5. import org.bukkit.util.Vector;
    6.  
    7. import net.minecraft.server.v1_7_R1.*;
    8.  
    9. public class CustomKart extends EntityMinecartRideable implements WASDEntity {
    10. public double forward = 0;
    11. public double sideways = 0; //Left is positive
    12. public boolean jump = false;
    13. public boolean shift = false;
    14. public long lastTimeJumped = 0L;
    15. public float speed = 0.4f;
    16.  
    17. public float accellerationRate = 0.0045f;
    18. public float currentAccelleration = 0.0f;
    19. public float maxAccelleration = 0.23f;
    20. public int ticksWithoutDriver = 0;
    21. public CustomKart(World world) {
    22. super(world);
    23. }
    24. @Override
    25. public void h() {
    26. if(this.passenger == null) {
    27. ticksWithoutDriver++;
    28. if (ticksWithoutDriver >= 60){
    29. }
    30. } else {
    31. if (!(passenger instanceof EntityPlayer)) {
    32. }
    33. EntityPlayer driver = (EntityPlayer)passenger;
    34. float percent = (this.currentAccelleration < 0.0f ? (this.currentAccelleration/-0.3f) : (this.currentAccelleration/this.maxAccelleration));
    35. driver.getBukkitEntity().setLevel((int) Math.ceil((double)(percent*100)));
    36. driver.getBukkitEntity().setExp(percent);
    37.  
    38. this.yaw = driver.yaw;
    39. Vector v = driver.getBukkitEntity().getLocation().getDirection().setY(0).normalize();
    40. Vector vSideways = driver.getBukkitEntity().getLocation().getDirection().setY(0).normalize().crossProduct(new Vector(0,1,0));
    41. if(forward > 0) {
    42. //forward
    43. this.currentAccelleration = accelerate(currentAccelleration, maxAccelleration, accellerationRate);
    44. } else if(forward < 0) {
    45. //back
    46. this.currentAccelleration = decelerate(currentAccelleration, -(maxAccelleration/2), accellerationRate*5);
    47. } else{
    48. if (this.currentAccelleration < 0.0f){
    49. this.currentAccelleration = accelerate(currentAccelleration, 0.0f, accellerationRate*2.0f);
    50. } else if (this.currentAccelleration > 0.0f){
    51. this.currentAccelleration = decelerate(currentAccelleration, 0.0f, accellerationRate*1.5f);
    52. }
    53. }
    54.  
    55. if(sideways > 0) {
    56. v.add(vSideways.multiply((this.currentAccelleration < 0.0f ? 1.0f : -1.0f)));
    57. }else if(sideways < 0) {
    58. v.add(vSideways.multiply((this.currentAccelleration > 0.0f ? 1.0f : -1.0f)));
    59. }
    60. v.multiply(currentAccelleration);
    61. this.move(v.getX(),0,v.getZ());
    62. this.motX=v.getX();
    63. this.motZ=v.getZ();
    64. if (positionChanged){
    65. if (ticksLived - lastTimeJumped > 10){
    66. this.move(v.getX(), 1.2, v.getZ());
    67. this.lastTimeJumped=ticksLived;
    68. }
    69. } else {
    70. }
    71. if(!this.onGround){
    72. }
    73. this.e((float)sideways,(float)forward, forward);
    74. }
    75. }
    76.  
    77. public boolean R(){
    78. return false;
    79. }
    80.  
    81. public boolean S(){
    82. return false;
    83. }
    84.  
    85. public static float roundDown(float a){
    86. DecimalFormat df = new DecimalFormat("#,###.###");
    87. df.setRoundingMode(RoundingMode.HALF_DOWN);
    88. return Float.parseFloat(df.format(a));
    89. }
    90.  
    91. public static float roundUp(float a){
    92. DecimalFormat df = new DecimalFormat("#,###.###");
    93. df.setRoundingMode(RoundingMode.HALF_UP);
    94. return Float.parseFloat(df.format(a));
    95. }
    96.  
    97. public static float accelerate(float a, float b, float c){
    98. if (a + c < b) return roundDown(a+c);
    99. else return roundDown(a + (b-a));
    100. }
    101.  
    102. public static float decelerate(float a, float b, float c){
    103. if (a - c > b) return roundUp(a - c);
    104. else return roundUp(a - (a-b));
    105. }
    106.  
    107.  
    108. @Override
    109. public void forward() {
    110. forward = 1;
    111. }
    112.  
    113. @Override
    114. public void backward() {
    115. forward = -1;
    116. }
    117.  
    118. @Override
    119. public void left() {
    120. sideways = 1;
    121. }
    122.  
    123. @Override
    124. public void right() {
    125. sideways = -1;
    126. }
    127.  
    128. @Override
    129. public void jump() {
    130. jump = true;
    131. }
    132.  
    133. @Override
    134. public void shift() {
    135. shift = true;
    136. }
    137.  
    138. @Override
    139. public void reset() {
    140. forward = 0;
    141. sideways = 0;
    142. jump = false;
    143. shift = false;
    144. }
    145.  
    146. @Override
    147. public int m() {
    148. // TODO Auto-generated method stub
    149. return 0;
    150. }


    And I use an interface with this as well...
    Code:java
    1.  
    2. public interface WASDEntity {
    3. //All methods should be self explanitory.
    4. public void forward();
    5. public void backward();
    6. public void left();
    7. public void right();
    8. public void jump();
    9. public void shift();
    10. public void reset(); // Resets all values
    11. }
    12.  


    So I guess my question is, what method in EntityMinecartAbstract, handles gravity? Then I could just copy and paste that code into my CustomKart class and override it. Thanks!
    [​IMG]
     
  2. Offline

    Garris0n

    Please fix the indentation or post as gists.
     
  3. Offline

    MoeMix

  4. Offline

    Garris0n

    What sort of gravity were you expecting if you never coded any?

    Edit: Where is that e() method coming from? I can't find it anywhere.
     
  5. Offline

    MoeMix

    I'm using protocollib's PacketPlayInSteerVehicle. That's where the e() method comes from. And I know gravity doesn't work since I haven't coded it. But where is it located in the EntityMinecartAbstract class? So I can just copy and paste it over? Or is there a simpler way to do it?
     
  6. Offline

    Garris0n

    I just realized you didn't paste in the full class, maybe that's why I'm not seeing that e(float, float, float) method anywhere. Anyway, the gravity is simply modifying the motY value. It's fairly obvious where it is in EntityMinecartAbstract.
     
  7. Offline

    MoeMix

    Would it be this? Found it in line 213
    Code:java
    1. this.motY -= 0.03999999910593033D;
     
  8. Offline

    Garris0n

    MoeMix That's my guess, try it and see what happens.
     
  9. Offline

    TeeePeee

    MoeMix
    Didn't I say that on skype 2 nights ago?
     
    Garris0n likes this.
Thread Status:
Not open for further replies.

Share This Page