动态显示字符雨、图形的自转和碰撞_OpenGL

结果

先把后面的字符雨关了,开起来动图太大了…
在这里插入图片描述
在这里插入图片描述

梳理逻辑

字符雨:是多条字符串的集合,每条字符串【坐标、字符串内容、速度】
五角星图形:【中心位置+形状顶点坐标+两轴速度+半径】
五角星还可以添加质量,然后碰撞函数的速度处理里把完全弹性碰撞的相关速度计算公式放进去。懒得加质量了,就当同质量的碰撞,速度互换。还可以根据旋转速度和边界的碰撞,调整速度的变化,根据速度和转速还有碰撞的边界,产生不同的变化,说是这么说,不搞不搞。

首先要明确的点:虽然每一个图形都有许多需要绘制出来的内容,但只需要根据图形唯一坐标的位置,在相对位置的坐标处绘制即可。所需要的信息就只有:图形唯一坐标,各要素相对距离。所以移动方面,只需要考虑该图形坐标的移动,然后每一帧根据坐标再把其他内容绘制到屏幕上。

字符雨:只需考虑每一条字符串的某一个位置,然后往上或者往下绘制其他字符。

五角星:只需要考虑核心位置,然后根据核心位置确定五个顶点,再连线。
移动:坐标位置+=设定的速度,芜湖!这样只用控制速度正负。
碰撞检测:两个五角星之间距离小于某值,则判断两者碰撞。直接分开比较两轴的间距了,直接取更大的半径为碰撞条件。用勾股定理算距离的话,xy相减了还要平方呢…有时候两个星星会互相穿过,应该是来不及处理了吧…
现在的碰撞检测函数是

void PositionBetweenTwoStar(Star &star, Star &star2) {
	if (abs(star.xPos - star2.xPos) <= max(star.r, star2.r) && abs(star.yPos - star2.yPos) <= max(star.r, star2.r)) {
		/*int 4字节 32位  异,=1,!=0,即两者速度正负相反*/
		if (((1 << 31 & star.vx) ^ (1 << 31 & star2.vx)) != 0) {
			star.vx ^= star2.vx ^= star.vx ^= star2.vx;//star.vx ^= star2.vx; star2.vx ^= star.vx; star.vx ^= star2.vx;	
		} else {
			star.vy = -star.vy; star2.vy = -star.vy;
		}
		if (((1 << 31 & star.vy) ^ (1 << 31 & star2.vy)) != 0) {
			star.vy ^= star2.vy ^= star.vy ^= star2.vy;
		} else {
			star.vx = -star.vx; star2.vx = -star.vx;
		}
		
	}
}

逻辑是,两者的速度相乘是负数,说明是反向。两者速度相乘是正,说明是同向。
不必用相乘,同向,int的首位字节相同 --异或运算。 不同就是1,是反向。两星星在某轴速度相反,则交换速度。当同质量的。
动量守恒:mV1+mV2=mV1’+mV2’ 能量守恒:0.5mV12+0.5mV22=0.5mV1’2+0.5mV2’2
V1-V1’=V2’-V2  V12-V1’2=V2’2-V22  (V1+V1’)(V1-V1’)=V12-V1’2 V1+V1’=V2+V2’   V2+V2’-V1’-V1’=V2’-V2  2V2=2V1’
V1’=V2  V2’=V1
本来还在碰撞后调节转速的,但没看到舒服的结果,就算啦。
检测调用是

void StarCollisionCheck() {

	for (int i = 0; i < STAR_NUM; i++) {
		for (int j = i+1; j < STAR_NUM; j++) {
			PositionBetweenTwoStar(star[i], star[j]);
		}		
	}
}

比n2/2多些,时间复杂度O(n2),明明知道不需要判断全部,但不知道怎么优化嗷。以后再看书学吧。

绘图:自旋效果,只需要画出来看到就行了。
旋转的话,就用之前学的二维变换。只需要一个自旋效果,那么就是旋转加上平移两种转换。平移到原点,旋转顶点,平移回去,OK!不知道有没有可能不用平移回去,那就不以原点为旋转中心,然后转换矩阵怎么设置呢…算了算了,先用老办法。反正旋转是个头疼的问题,三角函数和浮点型都有,这对运算一点都不友善。直线,用上次学的直线画法,一个个像素往上打吧~

全部代码

#include <gl/glut.h>
#include <gl/GLU.h>
#include <gl/GL.h>
#include <windows.h>

#include <math.h>
#include <time.h>
#include <iostream>

#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glut.lib")
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
using namespace std;

/*参数定义*/
#pragma region Setting
const int MAX_CHAR = 128;
const int STR_NUM = 86;//字符串数量
const int STR_SIZE = 20;//字符串长度/字符数
const int STR_SPACE = 15;//字符串间隔
const int WINDOW_WIDTH = 1024;
const int WINDOW_HEIGHT = 640;
const int STAR_NUM = 3;//改星星数量,只用改这里。

static int colorLerp;
struct Raindrop {
	int x;
	int y;
	int speed;
	char str[STR_SIZE];
}rain[STR_NUM];
struct Point {
	int x;
	int y;
};
#pragma endregion

/*Bresenham画线法*/
#pragma region Line
void drawOrdinayLine(int x0, int y0, int x1, int y1, int flag) {
	int i;
	int x, y, dx, dy, e;
	dx = x1 - x0;
	dy = y1 - y0;
	e = -dx;
	x = x0; y = y0;
	for (i = 0; i <= dx; i++) {
		switch (flag) {
		case 0:
			glVertex2i(x, y);  break;
		case 1:
			glVertex2i(x, 2 * y0 - y); break;//增量为(y-y0)则,实际增量应取相反方向为y0-(y-y0)=2y0-y
		case 2:
			glVertex2i(y, x); break;//这里也要交换
		case 3:
			glVertex2i(2 * y0 - y, x); break;
		}
		x++;
		e = e + 2 * dy;
		if (e >= 0) {
			y++;
			e = e - 2 * dx;
		}
	}
}
void Swap(int *a, int *b) {
	*a ^= *b;
	*b ^= *a;
	*a ^= *b;
}
void BresenhamLine(int x0, int y0, int x1, int y1) {
	int i;
	if (x0 > x1) {//总是从左端开始
		Swap(&x0, &x1);
		Swap(&y0, &y1);
	}
	if (x0 == x1) {
		if (y0 > y1) {//从y0开始“往上”画,所以不允许y1更低;
			Swap(&y0, &y1);
		}
		for (i = y0; i < y1; i++) {
			glVertex2i(x0, i);
		}
		return;
	}
	float k = (1.0*(y1 - y0)) / (x1 - x0);//根据斜率调整取点
	if (0 <= k && k <= 1) {  //直接画
		drawOrdinayLine(x0, y0, x1, y1, 0);
	} else if (-1 <= k && k < 0) {//以y=y0作B点的对称点
		drawOrdinayLine(x0, y0, x1, y1 + 2 * (y0 - y1), 1);
	} else if (k > 1) {//交换x,y的坐标值。
		Swap(&x0, &y0);//可以直接交换传入函数的参数,这为了直观。
		Swap(&x1, &y1);
		drawOrdinayLine(x0, y0, x1, y1, 2);
	} else if (k < -1) {
		//交换xy
		Swap(&x0, &y0);
		Swap(&x1, &y1);
		//交换两个点
		Swap(&x0, &x1);
		Swap(&y0, &y1);
		drawOrdinayLine(x0, y0, x1, y1 + 2 * (y0 - y1), 3);
	}
}
#pragma endregion

/*采用行向量矩阵,把P'=M*P的变换矩阵M和列向量P转置,CT=(AB)T=BT*AT,得到的结果也是行向量*/
void MultiplyMatrixAB(int a[3][3], float b[3][3], float c[3][3], int row) {
	int i, j, k, sum;
	for (i = 0; i < row; i++) {
		for (k = 0; k < 3; k++) {
			sum = 0;
			for (j = 0; j < 3; j++)
				sum += a[i][j] * b[j][k];
			c[i][k] = sum;
		}
	}
}
#pragma region Star
struct Star {
	Point point[5];
	int xPos;//中心x
	int yPos;//中心y
	int vx;
	int vy;
	int r;
	int rotationSpeed;
	int angle = 0;
}star[STAR_NUM];
void Translation2D(Point shape[], int n, int deltax, int deltay, Point Rshape[]) {
	float trans[3][3] = { 1,0,0,0,1,0,0,0,1 };
	trans[2][0] = deltax;
	trans[2][1] = deltay;
	int pnt[1][3] = { 0,0,1 };
	int i;
	float result[1][3] = { 0 };
	for (i = 0; i < n; i++) {
		pnt[0][0] = shape[i].x;
		pnt[0][1] = shape[i].y;
		MultiplyMatrixAB(pnt, trans, result, 1);
		Rshape[i].x = result[0][0];
		Rshape[i].y = result[0][1];
	}
}
void Rotation2D(Point shape[], int n, float angle, Point Rshape[]) {
	angle *= 3.14 / 180;
	float rotate[3][3] = { 0,0,0,0,0,0,0,0,1 };
	rotate[0][0] = rotate[1][1] = cos(angle);
	rotate[0][1] = sin(angle);
	rotate[1][0] = -rotate[0][1];
	int pnt[1][3] = { 0,0,1 };
	int i;
	float result[1][3] = { 0 };
	for (i = 0; i < n; i++) {
		pnt[0][0] = shape[i].x;
		pnt[0][1] = shape[i].y;
		MultiplyMatrixAB(pnt, rotate, result, 1);
		Rshape[i].x = result[0][0];
		Rshape[i].y = result[0][1];
	}
}
void InitStar(Star &star) {
	star.yPos = rand() % WINDOW_HEIGHT;
	star.xPos = rand() % WINDOW_WIDTH;
	star.vx = rand() % 6 + 3;
	star.vy = rand() % 6 + 3;
	star.r = rand() % 15 + 15;//15~30
	star.rotationSpeed = rand() % 10;
}
void DrawStar(Star &star) {
	star.point[0].x = star.xPos - star.r * sin(36 * 3.14 / 180); star.point[0].y = star.yPos - star.r * cos(36 * 3.14 / 180);
	star.point[1].x = star.xPos; 								 star.point[1].y = star.yPos + star.r;
	star.point[2].x = star.xPos + star.r * sin(36 * 3.14 / 180); star.point[2].y = star.yPos - star.r * cos(36 * 3.14 / 180);
	star.point[3].x = star.xPos - star.r * cos(18 * 3.14 / 180); star.point[3].y = star.yPos + star.r * sin(18 * 3.14 / 180);
	star.point[4].x = star.xPos + star.r * cos(18 * 3.14 / 180); star.point[4].y = star.yPos + star.r * sin(18 * 3.14 / 180);
	//glRectf(star.xPos-10, star.yPos-10, star.xPos+10, star.yPos+10);//中心显示
	glBegin(GL_POINTS);
	Translation2D(star.point, 5, -star.xPos, -star.yPos, star.point);
	Rotation2D(star.point, 5, star.angle += star.rotationSpeed, star.point);
	if (star.angle > 360)star.angle = 0;
	Translation2D(star.point, 5, +star.xPos, star.yPos, star.point);

	for (int i = 0; i < 4; i++) {
		BresenhamLine(star.point[i].x, star.point[i].y, star.point[i + 1].x, star.point[i + 1].y);
	}
	BresenhamLine(star.point[0].x, star.point[0].y, star.point[4].x, star.point[4].y);
	glEnd();
}
void MoveStar(Star &star) {
	star.xPos += star.vx;
	star.yPos += star.vy;
	if ((star.xPos > WINDOW_WIDTH &&star.vx > 0) || (star.xPos < 0 && star.vx < 0)) star.vx = -star.vx;
	if ((star.yPos > WINDOW_HEIGHT &&star.vy > 0) || (star.yPos < 0 && star.vy < 0)) star.vy = -star.vy;
}
void PositionBetweenTwoStar(Star &star, Star &star2) {
	if (abs(star.xPos - star2.xPos) <= max(star.r, star2.r) && abs(star.yPos - star2.yPos) <= max(star.r, star2.r)) {
		/*int 4字节 32位  异,=1,!=0,即两者速度正负相反*/
		if (((1 << 31 & star.vx) ^ (1 << 31 & star2.vx)) != 0) {
			star.vx ^= star2.vx ^= star.vx ^= star2.vx;
		} else {
			star.vy = -star.vy; star2.vy = -star.vy;
		}
		if (((1 << 31 & star.vy) ^ (1 << 31 & star2.vy)) != 0) {
			star.vy ^= star2.vy ^= star.vy ^= star2.vy;
		} else {
			star.vx = -star.vx; star2.vx = -star.vx;
		}
				/*
		if (((1 << 31 & star.rotationSpeed) ^ (1 << 31 & star2.rotationSpeed)) == 0) {//同转向
			if (star.rotationSpeed > 0) {
				star.rotationSpeed--;
				star2.rotationSpeed--;
			} else {
				star.rotationSpeed++;
				star2.rotationSpeed++;
			}
			if (star.rotationSpeed > star2.rotationSpeed) {
				star2.rotationSpeed = -star2.rotationSpeed;//速度更小的 反过来
			} else {
				star.rotationSpeed = -star.rotationSpeed;//不然就是star速度更小。 先减速,然后反过来
			}
		} else {//反转向
			if (star.rotationSpeed > 0) {
				star.rotationSpeed++;
				star2.rotationSpeed--;
			} else {
				star.rotationSpeed--;
				star2.rotationSpeed++;
			}

		}*/		
	}
}
void StarCollisionCheck() {
	for (int i = 0; i < STAR_NUM; i++) {
		for (int j = i + 1; j < STAR_NUM; j++) {
			PositionBetweenTwoStar(star[i], star[j]);
		}
	}
}
#pragma endregion

char CreateCh() {
	char temp = 0;
	switch (rand() % 3) {
	case 0:
		temp = rand() % 26 + 'A';
		break;
	case 1:
		temp = rand() % 26 + 'a';
		break;
	case 2:
		temp = rand() % 10 + '0';
		break;
	default:
		break;
	}
	return temp;
}
void Init() {
	srand((unsigned)time(NULL));
	//初始化字符串
	for (int i = 0; i < STR_NUM; i++) {
		for (int j = 0; j < STR_SIZE; j++) {
			rain[i].str[j] = CreateCh();//对每一字母赋值
		}
	}
	//初始化坐标以及速度
	for (int i = 0; i < STR_NUM; i++) {
		rain[i].x = i * STR_SPACE;
		rain[i].y = rand() % WINDOW_HEIGHT;
		rain[i].speed = rand() % 4 + 3;
	}
	for (int i = 0; i < STAR_NUM; i++) {
		InitStar(star[i]);
	}
}
void ChangChar() {
	for (int i = 0; i < STR_NUM; i++) {
		rain[i].str[rand() % STR_SIZE] = CreateCh();
	}
}
void DrawChar(const char c) {
	static int isFirstCall = 1;
	static GLuint lists;
	if (isFirstCall) { // 如果是第一次调用,执行初始化
		// 为每一个ASCII字符产生一个显示列表
		isFirstCall = 0;
		// 申请MAX_CHAR个连续的显示列表编号
		lists = glGenLists(MAX_CHAR);
		// 把每个字符的绘制命令都装到对应的显示列表中
		wglUseFontBitmaps(wglGetCurrentDC(), 0, MAX_CHAR, lists);
	}
	glCallList(lists + c);
}
void CreatRain() {
	//glPushMatrix();
	ChangChar();
	for (int i = 0; i < STR_NUM; i++) {
		for (int k = 0; k < STR_SIZE; k++) {
			glColor3ub(0, 255 - colorLerp * k, 255 - colorLerp * k);
			glRasterPos2i(rain[i].x, rain[i].y + STR_SPACE * k);
			DrawChar(rain[i].str[k]);
		}
	}
	//glPopMatrix();
}
void MoveRain() {
	for (int i = 0; i < STR_NUM; i++) {
		rain[i].y -= rain[i].speed;
		if (rain[i].y < -STR_SIZE * STR_SPACE) {
			rain[i].y = WINDOW_HEIGHT;
			rain[i].speed = rand() % 4 + 3;
		}
	}
}
void Display() {
	glClear(GL_COLOR_BUFFER_BIT);//清除之前的内容,不加的话,之前的像素都会积累
	CreatRain();
	MoveRain();
	for (int i = 0; i < STAR_NUM; i++) {
		MoveStar(star[i]);
		switch (i) {
		case 0:
			glColor3f(1.0, 0.0, 0.0);
			break;
		case 1:
			glColor3f(1.0, 1.0, 0.0);
			break;
		case 2:
			glColor3f(0.0, 1.0, 0.0);
			break;
		case 3:
			glColor3f(1.0, 0.0, 1.0);
			break;
		default:
			glColor3f(1.0, 1.0, 1.0);
			break;
		}
		DrawStar(star[i]);
	}
	StarCollisionCheck();
	glutPostRedisplay();
	glutSwapBuffers();
}
void main(int argc, char **argv) {
	colorLerp = 255 / STR_SIZE;
	Init();
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("Boring day||OpenGL真难用");
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT);
	glutDisplayFunc(Display);
	glutMainLoop();
}

另外,看别人字符雨视频里用其他的图形库,好简洁的样子…
烦。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值