Near perfect collision in LibGdx Java -


i trying collision in game perfect. testing if hit wall player, come stop. implemented collision code when player hits left side of wall(when wall on right side of player). here code.

    if(entityonright){         if(player.getpositioncorner(squaremaptuples.bottom_right).x -                 ent.getpositioncorner(squaremaptuples.bottom_left).x > -.9f)             player.setmovementbooleans(false, false, false, false);         else             player.setmovementbooleans(true, false, false, false);     } 

note: if go slow, stop player desire stopped, going fast, won't collision way want

essentially, code states if wall on right side, check bottom right corner of rectangle of player, subtract bottom left corner of wall, , check if distance between 2 0.001. 0.001 unnoticeable distance, hence why used value. here code player.setmovementbooleans

public void setmovementbooleans(boolean canmoveright, boolean canmoveleft, boolean canmoveup, boolean canmovedown){      this.canmoveright = canmoveright;     if(canmoveright == false && moveright)         vel.x = 0; } 

the canmoveright boolean in player class (not in parameters) allows able move, moveright when trying move right. here code better explain how these booleans interact:

//if clicked right arrow key , you're not going      //faster max speed      if(moveright && !(vel.x >= 3)){         vel.x += movementspeed;     }else if(vel.x >= 0 && !moveright){         vel.x -= movementspeed * 1.5f;         system.out.println("stopping");         //make sure goes rest         if(vel.x - movementspeed * 1.5f < 0)             vel.x = 0;     } 

and:

if(gdx.input.iskeypressed(keys.d) && canmoveright)         moveright = true;     else         moveright = false; 

so give summary, if click "d" key, allows start moving. if boolean canmoveright false, won't move you. here image showing happens (the player yellow, wall green)

enter image description here

as can see, player goes further want to. should stop @ point:

enter image description here

any figuring out how accomplish extremely appreciated!

maybe way tried bit complicated :-). suggest simpler way scratch: make map , player com.badlogic.gdx.math.rectangle instance. now, in following part of code make check, whether after move player still inside map, if yes allow move, if not, don't allow:

if(gdx.input.iskeypressed(keys.d){     float requestedx, requestedy;     //calculate requested coordinates     rectangle newplayerpositionrectangle = new rectangle(requestedx, requestedy, player.getwidth(), player.getheight());     if (newplayerpositionrectangle.overlaps(map) {         //move player     } else {         //move player edge of map , stop there     } } 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -