SlideShare a Scribd company logo
1 of 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.
SOURCE CODE:
/*Collision Detection*/
#include<graphics.h>
#include<stdio.h>
#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:TCBGI");
draw_rectangle();
printf("nInput line coordinatesn");
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=%dn maxy=%3dn minx=%4dn miny=%6dn",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;
}
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
{
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;
}
}
}
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)
{
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;
}
OUTPUT:
collisiondetection
collisiondetection
collisiondetection

More Related Content

Viewers also liked

Embedded System Practical manual (1)
Embedded System Practical manual (1)Embedded System Practical manual (1)
Embedded System Practical manual (1)Niraj Bharambe
 
Advanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceAdvanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceNiraj Bharambe
 
Data Warehousing Practical for T.Y.I.T.
Data Warehousing Practical for T.Y.I.T.Data Warehousing Practical for T.Y.I.T.
Data Warehousing Practical for T.Y.I.T.Niraj Bharambe
 
Sixth sense technology
Sixth sense technologySixth sense technology
Sixth sense technologyNiraj Bharambe
 
Unit 1st and 3rd notes of java
Unit 1st and 3rd notes of javaUnit 1st and 3rd notes of java
Unit 1st and 3rd notes of javaNiraj Bharambe
 
Htmlnotes 150323102005-conversion-gate01
Htmlnotes 150323102005-conversion-gate01Htmlnotes 150323102005-conversion-gate01
Htmlnotes 150323102005-conversion-gate01Niraj Bharambe
 
Xml 150323102007-conversion-gate01
Xml 150323102007-conversion-gate01Xml 150323102007-conversion-gate01
Xml 150323102007-conversion-gate01Niraj Bharambe
 

Viewers also liked (10)

Html color codes
Html color codesHtml color codes
Html color codes
 
Embedded System Practical manual (1)
Embedded System Practical manual (1)Embedded System Practical manual (1)
Embedded System Practical manual (1)
 
Core java tutorial
Core java tutorialCore java tutorial
Core java tutorial
 
Advanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceAdvanced java practical semester 6_computer science
Advanced java practical semester 6_computer science
 
Data Warehousing Practical for T.Y.I.T.
Data Warehousing Practical for T.Y.I.T.Data Warehousing Practical for T.Y.I.T.
Data Warehousing Practical for T.Y.I.T.
 
Sixth sense technology
Sixth sense technologySixth sense technology
Sixth sense technology
 
Unit 1st and 3rd notes of java
Unit 1st and 3rd notes of javaUnit 1st and 3rd notes of java
Unit 1st and 3rd notes of java
 
Htmlnotes 150323102005-conversion-gate01
Htmlnotes 150323102005-conversion-gate01Htmlnotes 150323102005-conversion-gate01
Htmlnotes 150323102005-conversion-gate01
 
Xml 150323102007-conversion-gate01
Xml 150323102007-conversion-gate01Xml 150323102007-conversion-gate01
Xml 150323102007-conversion-gate01
 
4.129 tybsc it
4.129 tybsc it4.129 tybsc it
4.129 tybsc it
 

Recently uploaded

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Recently uploaded (20)

Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

collisiondetection