2 回答
TA贡献1816条经验 获得超4个赞
/*Ball.h:定义结构体和BALL相关的函数*/
#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文件结束
/*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相关信息如下:\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 关注
- 116 浏览
添加回答
举报