2 回答

TA貢獻1816條經(jīng)驗 獲得超4個贊
/*Ball.h:定義結(jié)構(gòu)體和BALL相關(guān)的函數(shù)*/
#ifndef _BALL_VC_SUPER_ADMI_2009_12_13_INCLUDE_
#define _BALL_VC_SUPER_ADMI_2009_12_13_INCLUDE_
typedef struct tagPOINT3D //3D坐標系
{
float x;
float y;
float z;
}POINT3D;
typedef struct tagBALL //球
{
POINT3D cur_coor;
POINT3D speed;
float radius;
}BALL;
typedef struct tagBOX //球的長方體運動場所,中空
{
POINT3D angle_coor[8]; //坐標,從下往上,順時針
float length;
float width;
float height;
}BOX;
void init_box(BOX *mybox,POINT3D origi_coor,POINT3D LWH)
{
int i;
mybox->height=LWH.x;
mybox->width=LWH.y;
mybox->height=LWH.z;
for(i=0;i<8;i++)mybox->angle_coor[i]=origi_coor;
mybox->angle_coor[1].x=origi_coor.x+LWH.x;
mybox->angle_coor[2].x=origi_coor.x+LWH.x;
mybox->angle_coor[2].y=origi_coor.y+LWH.y;
mybox->angle_coor[3].y=origi_coor.y+LWH.y;
for(i=4;i<8;i++)mybox->angle_coor[i].z=origi_coor.z+LWH.z;
mybox->angle_coor[5].x=origi_coor.x+LWH.x;
mybox->angle_coor[6].x=origi_coor.x+LWH.x;
mybox->angle_coor[6].y=origi_coor.y+LWH.y;
mybox->angle_coor[7].y=origi_coor.y+LWH.y;
}
void move_in_box(BALL* myball,BOX mybox) //勻速運動
{
myball->cur_coor.x+=myball->speed.x;
myball->cur_coor.y+=myball->speed.y;
myball->cur_coor.z+=myball->speed.z;
}
void hit_wall(BALL* myball,POINT3D dir) //撞墻
{
myball->speed.x*=dir.x;
myball->speed.y*=dir.y;
myball->speed.z*=dir.z;
}
int coll_dete_rad_ign(BALL* myball,BOX mybox) //檢測(忽略半徑)
{
POINT3D dir;
dir.x=1;dir.y=1;dir.z=1;
if(myball->cur_coor.x<=mybox.angle_coor[0].x ||myball->cur_coor.x>=mybox.angle_coor[6].x)
dir.x=-1;
if(myball->cur_coor.y<=mybox.angle_coor[0].y ||myball->cur_coor.y>=mybox.angle_coor[6].y)
dir.y=-1;
if(myball->cur_coor.z<=mybox.angle_coor[0].z ||myball->cur_coor.z>=mybox.angle_coor[6].z)
dir.z=-1;
if(dir.x==-1 ||dir.y==-1 ||dir.z==-1){hit_wall(myball,dir);return 1;}
return 0;
}
#endif
//BALL.H文件結(jié)束
/*Ball.c:測試代碼*/
#include "Ball.h"
#include <stdio.h>
int main(void)
{
POINT3D origi;
POINT3D LWH;
BOX mybox;
BALL myball;
int i;
origi.x=0;origi.y=0;origi.z=0;
printf("矩形空間尺寸:");
scanf("%f%f%f",&LWH.x,&LWH.y,&LWH.z);
printf("球的初始坐標:");
scanf("%f%f%f",&myball.cur_coor.x,&myball.cur_coor.y,&myball.cur_coor.z);
printf("球的速度:");
scanf("%f%f%f",&myball.speed.x,&myball.speed.y,&myball.speed.z);
/* LWH.x=10;LWH.y=10;LWH.z=10;
myball.cur_coor.x=5;myball.cur_coor.y=5;myball.cur_coor.z=5;
myball.speed.x=0.2;myball.speed.y=0.5;myball.speed.z=1;*/
i=0;init_box(&mybox,origi,LWH);
printf("\n相關(guān)信息如下:\n");
printf("矩形空間尺寸--L:%f,W:%f,H:%f\n",LWH.x,LWH.y,LWH.z);
printf("球的初始坐標--X:%f,Y:%f,Z:%f\n",myball.cur_coor.x,myball.cur_coor.y,myball.cur_coor.z);
printf("球的初始速度--X:%f,Y:%f,Z:%f\n",myball.speed.x,myball.speed.y,myball.speed.z);
printf("碰撞點坐標如下:\n");
while(i<10)
{
if(coll_dete_rad_ign(&myball,mybox)==1)
{
i++;
printf("%f,%f,%f\n",myball.cur_coor.x,myball.cur_coor.y,myball.cur_coor.z);
}
move_in_box(&myball,mybox);
}
return 0;
}
- 2 回答
- 0 關(guān)注
- 159 瀏覽
添加回答
舉報