teclas: "W" move nave na direcçao da "ponta" desta, "A" roda a nave para a esquerda, "D" roda a nave para a direita "Espaço" dispara tiros
Problema 1 - ao rodar e avançar a nave a nave nao roda sobre o seu centro mas sim sobre o ponto (0,0,0).
Problema 2 - os tiros ao rodar a nave perdem a direcçao da nave.
Código :
#include<glut.h>
#include <time.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
using namespace std;
float X_CENTRE = 0.0;
float Y_CENTRE = 0.0;
float rot = 0.0;
float AsteroidX[6];
float AsteroidY[6];
float StarX[250];
float StarY[250];
float angle = 0;
float bulletAngle = 0;
bool fired = false;
float BulletLocX = 0.0;
float BulletLocY = 0.0;
bool collision = false;
bool deathcount[6];
float killed = 0.0;
bool stardrawn = false;
float score = 0.0;
char ascore [6];
void drawship()
{
glColor3f(1, 1, 1);
glLoadIdentity();
glRotatef(angle,0.0,0.0,1.0);
glBegin(GL_POLYGON);
glVertex2f( X_CENTRE, Y_CENTRE + 5);
glVertex2f( X_CENTRE + 5, Y_CENTRE - 9);
glVertex2f( X_CENTRE, Y_CENTRE -2);
glVertex2f( X_CENTRE -5, Y_CENTRE-9);
glEnd();
glFlush();
}
void XYrand()
{
for(int i = 0; i < 6; i++){
float k = rand() % 200;
k = k - 100;
if(k < -90){
k = -90;
}
if(k > 90){
k = 90;
}
AsteroidX[i] = k;
}
for(int i = 0; i < 6; i++){
float z = rand() % 200;
z = z - 100;
if(z < -90){
z = -90;
}
if(z > 90){
z = 90;
}
AsteroidY[i] = z;
}
}
void drawAsteroid()
{
glColor3f(0.45, 0.2, 0.0);
glLoadIdentity();
for(int i = 0; i < 6; i++){
if(deathcount[i]){
glBegin(GL_POLYGON);
glVertex2f(AsteroidX[i] + 9,AsteroidY[i]);
glVertex2f(AsteroidX[i], AsteroidY[i] - 9);
glVertex2f(AsteroidX[i] - 9, AsteroidY[i]);
glVertex2f(AsteroidX[i], AsteroidY[i] + 9);
glEnd();
}
}
glFlush();
}
void drawBullet()
{
glPushMatrix();
glTranslatef(X_CENTRE,Y_CENTRE,0);
glRotatef(angle,0.0,0.0,1.0);
glColor3f(1, 0, 0);
glPointSize(3.0);
glBegin(GL_POINTS);
glVertex2f(X_CENTRE+BulletLocX , Y_CENTRE+BulletLocY);
glEnd();
glPopMatrix();
}
void renderBitmapString( float x, float y, float z,void *font, char *string) {
char *c;
glRasterPos3f(x, y,z);
for (c=string; *c != '\0'; c++) {
glutBitmapCharacter(font, *c);
}
}
void drawScore()
{
sprintf(ascore,"Score: %f",score);
glLoadIdentity();
}
void keyinput(int key, int x, int y)
{
switch(key){
case GLUT_KEY_UP:
Y_CENTRE = Y_CENTRE + 1;
break;
case GLUT_KEY_DOWN:
Y_CENTRE = Y_CENTRE - 1;
break;
break;
case GLUT_KEY_LEFT:
angle = angle + 5;
break;
case GLUT_KEY_RIGHT:
angle = angle - 5;
break;
}
}
void shoot(unsigned char key,int x, int y){
switch(key){
case 32:
if(fired != true){
bulletAngle = angle +90;
bulletAngle = (bulletAngle * 3.142)/180;
}
fired = true;
break;
}
}
void bulletPath(){
BulletLocX = BulletLocX + (5*cos(bulletAngle)/10);
glutPostRedisplay();
BulletLocY = BulletLocY + (5*sin(bulletAngle)/10);
glutPostRedisplay();
}
void CollisionTest()
{
for(int i = 0; i < 6; i++){
if(deathcount[i])
{
int j = AsteroidX[i] - BulletLocX;
int k = AsteroidY[i] - BulletLocY;
if((k < 9 && k > -9)&&(j < 9 && j > -9))
{
collision = true;
deathcount[i] = false;
AsteroidX[i] = 200;
AsteroidY[i] = 200;
killed++;
score++;
break;
}
}
}
}
void reshape(int width, int height)
{
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, width, height);
glOrtho(-100, 100, -100, 100, -100.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glPointSize(1.0);
drawBullet();
drawAsteroid();
drawship();
drawScore();
if (fired)
{
bulletPath();
}
CollisionTest();
if(BulletLocX > 100.0 || BulletLocX < -100.0 || BulletLocY > 100.0 || BulletLocY < -100.0 || collision == true)
{
collision = false;
fired = false;
BulletLocX = X_CENTRE;
BulletLocY = Y_CENTRE;
}
if(killed == 6){
for(int i = 0; i < 6; i++){
deathcount[i] = true;
killed = 0;
XYrand();
drawAsteroid();
}
}
renderBitmapString( -90,90,0.0,GLUT_BITMAP_HELVETICA_12,ascore);
glutSwapBuffers();
}
void init(void)
{
srand((unsigned)time(0));
XYrand();
for(int i = 0; i < 6; i++){
deathcount[i] = true;
}
glClearColor (0.0, 0.0, 0.0, 0.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (400, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Asteroids");
init();
glutDisplayFunc(display);
glutSpecialFunc(keyinput);
glutKeyboardFunc(shoot);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutMainLoop();
return 0;
}
Agradeço a ajuda ! Cumps












