C# and SFML game intersection -
i have problem moveentity()
method in 2d platformer game (like pacman). when player on intersection path want make:
e.isatintersection = true;
and call updatedirection()
.
but doesn't stop, instead goes through paths , doesn't matter if there walls or empty paths. program never reaches above line. member stays false
. maybe there possibility write in easier way or there simple mistake made. here complete method:
private void moveentity(entity e) { int angle = 0; if (e.currentdirection == direction.right) angle = 0; else if (e.currentdirection == direction.down) angle = 90; else if (e.currentdirection == direction.left) angle = 180; else if (e.currentdirection == direction.up) angle = 270; var scalex = math.round(math.cos(angle * (math.pi / 180.0))); var scaley = math.round(math.sin(angle * (math.pi / 180.0))); var velocityx = (float)(e.speed * scalex); var velocityy = (float)(e.speed * scaley); sprite sp = sprites[e.name]; vector2f v = new vector2f(sp.position.x + velocityx, sp.position.y + velocityy); sp.position = v; var ecenterx = sp.position.x + sp.texturerect.width / 2; var ecentery = sp.position.y + sp.texturerect.height / 2; var tiley = math.floor((ecentery) / tileheight); var tilex = math.floor((ecenterx) / tilewidth); var tilexpos = tilewidth * math.floor(tilex + 1); var tileypos = tileheight * math.floor(tiley + 1); e.x = (int)tiley; e.y = (int)tilex; if (ecenterx == tilexpos && ecentery == tileypos) e.isatintersection = true; else e.isatintersection = false; }
the == operator, floating points, checks exact equality. floats have gone through similar operations still not equal. when compare equality on floats best practice following:
if(math.abs(floata-floatb) <= somesmallfloat)
somesmallfloat here set float.epsilon in case may want use higher number allow margin error, more fun allow player move if off, punish player small inaccuracies.
Comments
Post a Comment