Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualdEgle

Posted 15 April 2012 - 11:33 AM

Thank you guys.
I did find another way, it's quite light too:
On each line, that presents side of block we find collision point and check if it's really on side of our block:


int lx,ly;   //temporary point
float t_distance[4];

//FIND POINT ON TOP SIDE
ly = block->y;
lx = (block->y - y) / tan(angle) + x;

if( lx >= block->x && lx <= block->x + block->w && ly >= block->y && ly <= block->y + block->h)
  //find distance to it
  t_distance[0] = sqrt((float) (x - lx)*(x - lx) + (y - ly)*(y - ly));


//FIND POINT BOTTOM SIDE
ly = block->y + block->h;
lx = (block->y + block->h - y) /tan(angle) + x;

if( lx >= block->x && lx <= block->x + block->w && ly >= block->y && ly <= block->y + block->h)
//find distance to it
t_distance[1] = sqrt((float) (x - lx)*(x - lx) + (y - ly)*(y - ly));


//FIND POINT ON LEFT SIDE

lx = block->x;
ly = tan(angle) * (block->x - x) + y;

//check if temp point is onside of block
if( lx >= block->x && lx <= block->x + block->w && ly >= block->y && ly <= block->y + block->h)
//find distance to it
t_distance[2] = sqrt((float) (x - lx)*(x - lx) + (y - ly)*(y - ly));


//FIND POINT ON RIGHT SIDE
lx = block->x + block->w;
ly = tan(angle) * (block->x + block->w - x) + y;


if( lx >= block->x && lx <= block->x + block->w && ly >= block->y && ly <= block->y + block->h)
//find distance to it
t_distance[3] = sqrt((float) (x - lx)*(x - lx) + (y - ly)*(y - ly));
Then just find minimum in t_distance and there u have it!

Ohh...and by the way my blocks aren't rotating and are just represented as x,y,width and height

#1dEgle

Posted 15 April 2012 - 11:31 AM

Thank you guys.
I did find another way, it's quite light too:
On each line, that presents side of block we find collision point and check if it's really on side of our block:


int lx,ly;   //temporary point
float t_distance[4];

//FIND POINT ON TOP SIDE
ly = block->y;
lx = (block->y - y) / tan(angle) + x;

if( lx >= block->x && lx <= block->x + block->w && ly >= block->y && ly <= block->y + block->h)
  //find distance to it
  t_distance[0] = sqrt((float) (x - lx)*(x - lx) + (y - ly)*(y - ly));


//FIND POINT BOTTOM SIDE
ly = block->y + block->h;
lx = (block->y + block->h - y) /tan(angle) + x;

if( lx >= block->x && lx <= block->x + block->w && ly >= block->y && ly <= block->y + block->h)
//find distance to it
t_distance[1] = sqrt((float) (x - lx)*(x - lx) + (y - ly)*(y - ly));


//FIND POINT ON LEFT SIDE

lx = block->x;
ly = tan(angle) * (block->x - x) + y;

//check if temp point is onside of block 
if( lx >= block->x && lx <= block->x + block->w && ly >= block->y && ly <= block->y + block->h)
//find distance to it
t_distance[2] = sqrt((float) (x - lx)*(x - lx) + (y - ly)*(y - ly));


//FIND POINT ON RIGHT SIDE
lx = block->x + block->w;
ly = tan(angle) * (block->x + block->w - x) + y;


if( lx >= block->x && lx <= block->x + block->w && ly >= block->y && ly <= block->y + block->h)
//find distance to it
t_distance[3] = sqrt((float) (x - lx)*(x - lx) + (y - ly)*(y - ly));
Then just find minimum in t_distance and there u have it!

PARTNERS