當前位置:首頁 » 編程語言 » c語言實現碰撞計算
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言實現碰撞計算

發布時間: 2023-02-08 07:53:16

A. c語言實現兩小球碰撞

我沒有TC,沒辦法畫。說一下原理。
畫兩個圓,表示小球。
半徑比k,半徑r1,r2,
圓沿兩條直線,速度v1,v2
時間步長dt
畫圓:
loop:
setcolor(WHITE); // 繪圖色
circle(x1, y1, r1);
circle(x2, y2, r2);
停0.05秒
setcolor(BLUE); // 背景色
circle(x1, y1, r1);
circle(x2, y2, r2);
更新x1, y1,x2, y2
goto loop;

畫直線用:
line(int x0, int y0, int x1, int y1);

寫字用
settextstyle(1, 0, 0);
outtextxy(120, 120, "Very Good");

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

int main()
{
int gdirver, gmode;
gdriver=DETETC;
initgraph(&gdriver, &gmode, "");
setbkcolor(BLUE); // 設屏幕底色
cleardevice();
畫圓:
loop:
setcolor(WHITE); // 繪圖色
circle(x1, y1, r1); // 畫新圓
circle(x2, y2, r2);
停0.05秒
setcolor(BLUE); // 用背景色塗掉圓
circle(x1, y1, r1);
circle(x2, y2, r2);
更新x1, y1,x2, y2
判斷相碰,是碰則 break;
goto loop;
用上面方法畫碰後的圓,區別在於
更新x1, y1,x2, y2
getch();
closegraph();
}

B. C語言編程:兩小球動態碰撞(鍵盤控制碰撞次數)

easy~~~~
--------------------
int n;
scanf("%d",&n);
for (int j=0; j<n; j++)
{
for(i=0; i<185; i++)
{
putimage(70+i, 170, buf, COPY_PUT); /*左邊球向右運動*/
putimage(500-i, 170, buf, COPY_PUT); /*右邊球向左運動*/
}
for(i=0;i<185; i++)
{
putimage(255-i, 170, buf, COPY_PUT);

putimage(315+i, 170, buf, COPY_PUT);
}
}

C. 走迷宮的游戲,怎樣判斷人物與障礙物碰撞,(c語言).各位大蝦,幫幫忙了,我想多參考一下

int map[32][42]={
{0,0,0,0,0,0,0,0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0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,0,1},
{0,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,1,1,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,1,1,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,1,1,0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,1},
{0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,1,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1}
};
void Welcome()
{
setcolor(BLUE);
rectangle(150,165,507,390);
rectangle(145,160,512,395);
settextjustify(CENTER_TEXT,TOP_TEXT);
setcolor(GREEN);
settextstyle(0,0,4);
outtextxy(320,120+16*4,"GoGoGo");
setcolor(RED);
settextstyle(0,0,2);
outtextxy(320,120+16*10,"Huang Xin Ping");
setcolor(WHITE);
settextstyle(0,0,2);
outtextxy(320,120+19*10,"QQ:77967292");
setcolor(WHITE);
settextstyle(0,0,2);
outtextxy(327,127+21*10,"Email:[email protected]");
setcolor(RED);
settextstyle(0,0,2);
outtextxy(320,120+24*10,"My Teacher:Lei Jue");
bioskey(0);
cleardevice();
}
void Happy()
{
char str[8]={10,20,30,40,50,60,70,80};
int i;
struct fillsettingstype save;
setbkcolor(BLUE);
cleardevice();
for(i=0;i<13;i++)
{
setcolor(i+3);
setfillstyle(i,2+i);
bar(100,150,200,50);
bar3d(300,100,500,200,70,1);
pieslice(200, 300, 90, 180, 90);
sector(500,300,180,270,200,100);
delay(100000);
}
bioskey(0);
cleardevice();
}
void DrawBlock(int x,int y,int color)
{
int m,n,i;
setbkcolor(BLACK);
setcolor(BLACK);
setfillstyle(XHATCH_FILL,color );
fillellipse(10*x+65,10*y+65,8,8);
setcolor(DARKGRAY);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i<=545;i+=10)
{
rectangle(i,40,i+10,49);
rectangle(i,441,i+10,432);
}
for(i=40;i<=431;i+=10)
{
rectangle(50,i,59,i+10);
rectangle(560,i,550,i+10);
}
setlinestyle(DOTTED_LINE,0,THICK_WIDTH);
for(m=0;m<41;m++)
{
for(n=0;n<32;n++)
{
if(map[n][m]==0)
{
setcolor(LIGHTGRAY);
rectangle(12*m+61,12*n+52,12*m+67,12*n+57);
}
}
}
}
enum direct{Left,Up,Right,Down};
enum bool{False,True};
enum bool Moveable(int x,int y,enum direct dir)
{
switch(dir)
{
case Left:
if(x>0) return True;
break;
case Up:
if(y>0) return True;
break;
case Right:
if(x<490/10-1)return True;
break;
case Down:
if(y<365/10-1)
return True;
}
return False;
}
void MoveBlock(int*x,int*y,enum direct dir)
{
DrawBlock(*x,*y,BGCOLOR);
switch(dir)
{
case Left:
(*x)--;break;
case Up:
(*y)--;break;
case Right:
(*x)++;break;
case Down:
(*y)++;break;
}
delay(5000);
DrawBlock(*x,*y,FORECOLOR);
}
void Welcome();
void Happy();
void Close();
void Prscore();
void Drawmap();
void GameOver();
void DrawBlock();
void Name();
void Bangquan();
void Time();

void main()
{
int x=0,y=0;
int key;
enum bool fGameOver=False;
int driver=VGA,graphmode=VGAHI;
initgraph(&driver,&graphmode,"c:\\turboc2");
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
Welcome();
Happy();
Prscore();
Name();
Bangquan();
Time();
Drawmap();
DrawBlock(x,y,FORECOLOR);
for(;;)
{
if(bioskey(1)) key=bioskey(0);
else key=0;
switch(key)
{
case VK_LEFT:
if(Moveable(x,y,Left))
MoveBlock(&x,&y,Left);times--;Prscore();
break;
case VK_RIGHT:
if(Moveable(x,y,Right))
MoveBlock(&x,&y,Right);times--;Prscore();
break;
case VK_UP:
if(Moveable(x,y,Up))
MoveBlock(&x,&y,Up);times--;Prscore();
break;
case VK_DOWN:
if(Moveable(x,y,Down))
MoveBlock(&x,&y,Down);times--;Prscore();
break;
case VK_ESC:
fGameOver=True;
clrscr();
GameOver();
}
if(fGameOver)
break;
}
Close();
}
void GameOver()
{
setbkcolor(BLACK);
setcolor(RED);
settextstyle(0,0,4);
outtextxy(320,20+16*10,"GAME OVER!");
setcolor(GREEN);
settextstyle(0,0,3);
outtextxy(320,70+16*10,"ji xu nu li!");
setcolor(WHITE);
circle(300,330,40);
setcolor(WHITE);
circle(317,320,6);
setcolor(WHITE);
circle(280,320,6);
setcolor(WHITE);
arc(300,340,220,360,15);
bioskey(0);
cleardevice();
}
void Prscore()
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,560,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"Times:%d",times);
outtextxy(120,20,str);
}
void Drawmap()
{
}
void Name()
{
char str[20];
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"Name:%s",name);
outtextxy(480,20,str);
}
void Bangquan()
{
char quan[20];
setcolor(6);
settextstyle(0,0,1);
sprintf(quan,"Author:%s",bangquan);
outtextxy(290,18,quan);
}
void Time()
{
char tim[20];
setcolor(6);
settextstyle(0,0,1);
sprintf(tim,"Time:%ld",time);
outtextxy(290,28,tim);
}
void Close()
{
closegraph();
}

D. 如何用c語言實現兩運動物體碰撞後其中一個不動 另一個被彈開的程序

將動量、能量和摩擦力等因素考慮進去,先把函數關系列出來,然後再用C語言計算結果,把結果畫出來就行了。

E. 如何用c語言編寫三個小球碰撞的程序,第一個小球碰撞第二個小球後立即停止運動,第二個開始運動...

我擦 等我回去修煉幾年 再來做這題

F. 如何用c語言編寫一個三個小球碰撞的程序啊

#include <stdio.h>
#include <graphics.h>
int main()
{
int i,j,gdriver,gmode,size;
void *buf;
gdriver=DETECT;
initgraph(&gdriver,&gmode,"d:\\tc2\\bgi");
setbkcolor(BLUE);
cleardevice();
setcolor(LIGHTRED);
setlinestyle(0,0,1);
setfillstyle(1,10);
circle(31,200,30);
floodfill(31,200,12);
size=imagesize(0,169,62,231);
buf=malloc(size);
getimage(0,169,62,231,buf);
putimage(500,269,buf,COPY_PUT);
putimage(320,170,buf,COPY_PUT);

while(1)
{
for(j=0;j<4;j++)
for(i=0;i<260;i++)
{
if(kbhit()) {closegraph();exit(0);}
switch(j)
{
case 0:
putimage(i,170,buf,COPY_PUT);
break;
case 1:
putimage(321+i,170,buf,COPY_PUT);
break;
case 2:
putimage(581-i,170,buf,COPY_PUT);
break;
case 3:
putimage(260-i,170,buf,COPY_PUT);
break;
}
}
}
}

G. 用C語言設計:兩個小球碰撞的物理特性

判斷兩個球的圓心之間的距離是否小於兩半徑之和就可以了,如果小於則碰撞.

H. 用C語言編程2小球在屏幕上隨機碰撞,能給代碼的最好,事後重謝!

你的程序有三個錯誤:
1.
main
應改為
main()

2.
scanf("%d%d",a,b);
應改為
scanf("%d%d",&a,&b);

3.
sum=a+b; 應該寫在scanf("%d%d",&a,&b);的後面

程序修改如下:

#include<stdio.h>
main()
{
int a,b,sum;
scanf("%d%d",&a,&b);
sum=a+b;
printf("sum is %d",sum);
}

一個更標準的程序如下:

#include<stdio.h>
int main(void)
{
int a,b,sum;
scanf("%d%d",&a,&b);
sum=a+b;
printf("sum is %d",sum);
return 0;
}

**********************************************
#include<stdio.h>

int main(void)
{
float a,b;
scanf("%f%f",&a,&b);
printf("%g+%g=%g",a,b,a+b);
return 0;
}

I. c語言三維碰撞的演算法

我是這樣想的,先判斷兩個物體是否接觸,如果相互接觸,則先把兩個物體的速度按x,y,z方向分解,然後對應的方向上,運用動量守恆,能量守恆,決定碰撞後的速度,然後在把得到的x,y,z方向上的速度進行合成就行了。

J. 用c語言編寫小球動態碰撞的程序

你是想用OpenGl還是Direct Draw的圖形開發庫。