c# - 2D Monogame: Rectangle collision detection, side specific -


i have 2 rectangles, 1 player, 1 map. player needs not able walk through map. both player , map have rectangle position , texture width , height, both have vector position. rectangle.intersect() outputs boolean value, cannot figure out how might find out side collided with. found function here outputs vector representing how rectangles overlapped.

public static vector2 getintersectiondepth(this rectangle recta,                 rectangle rectb)     {         // calculate half sizes.         float halfwidtha = recta.width / 2.0f;         float halfheighta = recta.height / 2.0f;         float halfwidthb = rectb.width / 2.0f;         float halfheightb = rectb.height / 2.0f;          // calculate centers.         vector2 centera = new vector2(recta.left + halfwidtha, recta.top + halfheighta);         vector2 centerb = new vector2(rectb.left + halfwidthb, rectb.top + halfheightb);          // calculate current , minimum-non-intersecting distances between centers.         float distancex = centera.x - centerb.x;         float distancey = centera.y - centerb.y;         float mindistancex = halfwidtha + halfwidthb;         float mindistancey = halfheighta + halfheightb;          // if not intersecting @ all, return (0, 0).         if (math.abs(distancex) >= mindistancex || math.abs(distancey) >= mindistancey)             return vector2.zero;          // calculate , return intersection depths.         float depthx = distancex > 0 ? mindistancex - distancex : -mindistancex - distancex;         float depthy = distancey > 0 ? mindistancey - distancey : -mindistancey - distancey;         return new vector2(depthx, depthy);     } 

this function give negative numbers based on side, cannot figure out how use them effectively. tried:

vector2 overlap =   rectangleextensions.getintersectiondepth(map.dungeon[x,y].boundingbox, player.boundingbox); if (overlap.x > 0) //this should collision on left {     //move player } 

however causes strange bugs, when attempting same y player , map values.

the question: how can collision detection done in monogame rectangles let know side collided with, using function or otherwise.

thanks help!

the rectangle class in xna has 4 properties can of use here:

left, top, right , bottom.

you can use these determine side(s) collision took place from.

for example, consider player.rectangle.left > map.rectangle.left. if true, collision have ocurred on right side (given left side of player bigger on x axis map's, means player right of left point of map).

basically can compare x , y coordinates of both rectangles. apart strange cases (like 1 rectangle being submerged entirely in another, or 1 rectangle being super-set of other) these should sufficient tell side collisioned on.

a better way check having middle-point of 2 shapes, might easier way tell relative positioning of 2 objects.

now, though in 3d, may find this post helpful.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -