collisiondetection

10
Practical No. 5 TITLE : Collision Detection AIM : Detecting a Collision of 2 2D-objects based on Min-max test or Bounding Box test. DESCRIPTION : Collision detection between 2D objects is effected by comparing the minimum-maximum x- and y- events for two shapes/objects. Min-Max Test: If A and B are 2 objects having extends(x-mina, x-maxa, y- mina, y-maxa) (x-minb, x-maxb, y-minb, y-maxb) then a horizontal overlap or touch condition is impossible if: x-minb > x-maxa OR x-mina > x-maxb. Similarly vertical overlap or touch condition is impossible if: y-minb > y-maxa OR y-mina > y-maxb.

Transcript of collisiondetection

Page 1: collisiondetection

Practical No. 5

TITLE: Collision Detection

AIM: Detecting a Collision of 2 2D-objects based on Min-max test or Bounding Box test.

DESCRIPTION:

● Collision detection between 2D objects is effected by comparing the minimum-maximum x- and y- events for two shapes/objects.

● Min-Max Test:If A and B are 2 objects having extends(x-mina, x-maxa, y-mina, y-maxa)

(x-minb, x-maxb, y-minb, y-maxb) then a horizontal overlap or touch condition is impossible if:

x-minb > x-maxa OR x-mina > x-maxb.

Similarly vertical overlap or touch condition is impossible if:

y-minb > y-maxa OR y-mina > y-maxb.

SOURCE CODE:

/*Collision Detection*/

#include<graphics.h>#include<stdio.h>

Page 2: collisiondetection

#include<conio.h>#include<dos.h>#include<math.h>

int lx,ly,lx1,ly1,l1_x,l1_y,l2_x,l2_y,l3_x,l3_y;int l1_x1,l1_y1,l2_x1,l2_y1,l3_x1,l3_y1;int l4_x,l4_y,l4_x1,l4_y1,maxx,maxy,minx,miny;float x,y,m,x3,y3;int getmax_x(int,int,int,int);int getmax_y(int,int,int,int);int getmin_x(int,int,int,int);int getmin_y(int,int,int,int);int collision(int,int,int,int,int,int);void draw_rectangle();

void main() {

int i,ch=32,co;int gm,gd=DETECT;//clrscr();

initgraph(&gd,&gm,"C:\\TC\\BGI");draw_rectangle();

printf("\nInput line coordinates\n");scanf("%d %d %d %d",&l4_x,&l4_y,&l4_x1,&l4_y1); maxx=getmax_x(lx,l1_x,l2_x,l3_x);maxy=getmax_y(ly,l1_y,l2_y,l3_y);minx=getmin_x(lx1,l1_x1,l2_x1,l3_x1);miny=getmin_y(ly1,l1_y1,l2_y1,l3_y1);line(l4_x,l4_y,l4_x1,l4_y1);

//moveto(500,500); //printf("maxx=%d\n maxy=%3d\n minx=%4d\n miny=%6d\n",maxx,maxy,minx,miny);

while(ch==32){

if(l4_x==l4_x1){

cleardevice();draw_rectangle();l4_y1=l4_y1+5;l4_y=l4_y+5;

Page 3: collisiondetection

}else{

if(l4_y==l4_y1){

cleardevice();draw_rectangle();l4_x1=l4_x1+5;l4_x=l4_x+5;

}}co=collision(l4_x1,l4_y1,maxx,maxy,minx,miny);if(co==0){

line(l4_x,l4_y,l4_x1,l4_y1);}ch=getche();

}getch();closegraph();

}void draw_rectangle(){

lx=230;ly=150;lx1=330;ly1=150;l1_x=230;l1_y=150;l1_x1=230;l1_y1=250;l2_x=230;l2_y=250;l2_x1=330;l2_y1=250;l3_x=330;l3_y=150;l3_x1=330;l3_y1=250;line(lx,ly,lx1,ly1);line(l1_x,l1_y,l1_x1,l1_y1);line(l2_x,l2_y,l2_x1,l2_y1);line(l3_x,l3_y,l3_x1,l3_y1);

}

int getmax_x(int x1,int x2,int x3,int x4){

if(x1>x2 && x1>x3 && x1>x4){

return x1;}else{

Page 4: collisiondetection

if(x2>x1 && x2>x3 && x2>x4){

return x2;}else{

if(x3>x1 && x3>x2 && x3>x4){

return x3;}else if(x4>x1 && x4>x2 && x4>x3){

return x4;}

}}return 0;

}

int getmax_y(int y1,int y2,int y3,int y4){

if(y1>y2 && y1>y3 && y1>y4){

return y1;}else{

if(y2>y1 && y2>y3 && y2>y4){

return y2;}else{

if(y3>y1 && y3>y2 && y3>y4){

return y3;}else if(y4>y1 && y4>y2 && y4>y3){

return y4;}

Page 5: collisiondetection

}}return 0;

}

int getmin_x(int xx1,int xx2,int xx3,int xx4){

if(xx1<xx2 && xx1<xx3 && xx1<xx4){

return xx1;}else{

if(xx2<xx1 && xx2<xx3 && xx2<xx4){

return xx2;}else{

if(xx3<xx1 && xx3<xx2 && xx3<xx4){

return xx3;}else if(xx4<xx1 && xx4<xx2 && xx4<xx3){

return xx4;}

}}return 0;

}

int getmin_y(int yy1,int yy2,int yy3,int yy4){

if(yy1<yy2 && yy1<yy3 && yy1<yy4){

return yy1;}else{

if(yy2<yy1 && yy2<yy3 && yy2<yy4)

Page 6: collisiondetection

{return yy2;

}else{

if(yy3<yy1 && yy3<yy2 && yy3<yy4){

return yy3;}else if(yy4<yy1 && yy4<yy2 && yy4<yy3){

return yy4;}

}}return 0;

}

int collision(int x,int y,int maxx,int maxy,int minx,int miny){

if((x>=minx && x<=maxx) && (y>=miny && y<=maxy)){

moveto(20,20);printf("collision detected");return 1;

}return 0;

}

Page 7: collisiondetection

OUTPUT:

Page 8: collisiondetection
Page 9: collisiondetection