android - LibGDX - Basic physics error? Cannot detect collision between player and ONE side of a platform (tile) -
couldn't find useful tutorials online tried freestyle algorithm collision.
basically, i've modelled player (main character) , platform (a tile player can collide with) rectangles sake of collision detection. in code, playerrect rectangle representing player , getrect() rectangle representing platform.
as stands player moves left right, can collide 3 of platform's sides (left, , bottom). have managed detect collision on top , bottom part of platform, reason can't detect collision on left side.
would highly appreciated if can have @ code , possibly find i'm going wrong.
public class platform extends gameobject { ... public void update() { // rectangle of player playerrect = player.getrect(); //a boolean checks whether or not player's rectangle //and platform's rectangle overlap isoverlapping = playerrect.overlaps(getrect()); if (isoverlapping) { // if player hits bottom part of platform, // have gravity pull him down if ((playerrect.getx() > getrect().getx() && playerrect.getx() < getrect().getx() + getrect().getwidth()) && playerrect.gety() > getrect().gety()); { player.setyspeed(player.getgravity()); // if player on platform or on ground, // allow him jump if (playeronplatform() || player.onground()) { player.setallow("true", "jump"); } else { player.setallow("false", "jump"); } } // if player on top part of platform, // set yspeed 0 remains on platform rather // fall due gravity if ((playerrect.getx() > getrect().getx() && playerrect.getx() < getrect().getx() + getrect().getwidth()) && playerrect.gety() < getrect().gety()) { player.setyspeed(0); // if player on platform or on ground, allow him jump if (playeronplatform() || player.onground()) { player.setallow("true", "jump"); } else { player.setallow("false", "jump"); } // if player hits side of platform, // set xspeed 0 not go through platform if(playerrect.getx() > getrect().getx() && (playerrect.gety() > getrect().gety() && playerrect.gety() < getrect().gety() + getrect().getheight())) { player.setyspeed(20); } } ... }
thanks in advance input
not if code has bug when assessing if, desired results or 0 , write set 20 player.setyspeed(20);
.perhaps not understood @ time :) should not setx ... (0), no sety
// if player hits side of platform, // set xspeed 0 not go through platform if(playerrect.getx() > getrect().getx() && (playerrect.gety() > getrect().gety() && playerrect.gety() < getrect().gety() + getrect().getheight())) { player.setyspeed(20); }
. on other hand if correct, inside, not outside have not test it, above
// if player on top part of platform, // set yspeed 0 remains on platform rather // fall due gravity if ((playerrect.getx() > getrect().getx() && playerrect.getx() < getrect().getx() + getrect().getwidth()) && playerrect.gety() < getrect().gety()) { player.setyspeed(0); // if player on platform or on ground, allow him jump if (playeronplatform() || player.onground()) { player.setallow("true", "jump"); } else { player.setallow("false", "jump"); } // if player hits side of platform, // set xspeed 0 not go through platform if(playerrect.getx() > getrect().getx() && (playerrect.gety() > getrect().gety() && playerrect.gety() < getrect().gety() + getrect().getheight())) { player.setyspeed(20); } }
you can use log, have if condition met sometime
// if player hits side of platform, // set xspeed 0 not go through platform if(playerrect.getx() > getrect().getx() && (playerrect.gety() > getrect().gety() && playerrect.gety() < getrect().gety() + getrect().getheight())) { player.setyspeed(20); gdx.app.log("inside", "test"); }
Comments
Post a Comment