objective c - Cocoa iOS Make a Rectangle into a Circle with Collisions -


so have uiview called fallingball collides nicely uiview called theblockview. using cgrectintersectsrect(theblockview.frame, fallingball.frame) detect collision.

that's well, fallingball round, , top corners of theblockview rounded. this, used following code:

//round top right-hand corner of theblockview uibezierpath *maskpath = [uibezierpath bezierpathwithroundedrect:theblockview.bounds                                             byroundingcorners:uirectcornertopright                                            cornerradii:cgsizemake(10.0, 10.0)]; cashapelayer *masklayer = [cashapelayer layer]; masklayer.frame = theblockview.bounds; masklayer.path = maskpath.cgpath; theblockview.layer.mask = masklayer;  //round fallingball view [[fallingball layer] setcornerradius:30]; 

but, funnily enough, though nice , rounded, views still rectangles. question is: how can make cgrectintersectsrect treat them shapes like? there function works same uses view's alpha detect collisions?

thanks time!

actually, let me answer own question!

ok, spent greater part of last 10 hours looking around, , came across post: circle-rectangle collision detection (intersection) - check out e.james has say!

i wrote function this: first, declare following structs:

typedef struct {     cgfloat x; //center.x     cgfloat y; //center.y     cgfloat r; //radius } circle; typedef struct {     cgfloat x; //center.x     cgfloat y; //center.y     cgfloat width;     cgfloat height; } mcrect; 

then add following function:

-(bool)circle:(circle)circle intersectsrect:(mcrect)rect {      cgpoint circledistance = cgpointmake(abs(circle.x - rect.x), abs(circle.y - rect.y) );      if (circledistance.x > (rect.width/2 + circle.r)) { return false; }     if (circledistance.y > (rect.height/2 + circle.r)) { return false; }      if (circledistance.x <= (rect.width/2)) { return true; }      if (circledistance.y <= (rect.height/2)) { return true; }      cgfloat cornerdistance_sq = pow((circledistance.x - rect.width/2), 2) + pow((circledistance.y - rect.height/2), 2);      return (cornerdistance_sq <= (pow(circle.r, 2))); } 

i hope helps someone!


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -