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 (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

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 

Recently uploaded (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

collisiondetection