Jump to content

  • Log In with Google      Sign In   
  • Create Account


rendering space ships


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
53 replies to this topic

#21 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 30 June 2013 - 05:37 PM

I have incorporated your code, but I cannot get the ships to rotate around their centers and to follow their noses when moving up and down. here is my code so far.

void
 
 
DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
{
glPushMatrix();
glTranslatef(
 
position_X, position_Y, position_Z);
glRotatef(
 
rotation,0.0f,0.0f,1.0f);
glColor3f(
 
color,0.0f,0.0f);
glBegin(
 
GL_LINE_LOOP);
glVertex3f(-1.5f,-0.25f,0.0f);
glVertex3f(-1.75f,-0.5f,0.0f);
glVertex3f(-2.0f,-0.5f,0.0f);
glVertex3f(-1.5f,0.5f,0.0f);
glVertex3f(-1.0f,-0.5f,0.0f);
glVertex3f(-1.25f,-0.5f,0.0f);
glVertex3f(-1.5f,-0.25f,0.0f);
glEnd();
glPopMatrix();
}
void
 
 
Render(void)
{
glClear(
 
GL_COLOR_BUFFER_BIT);
DrawShip(ship_01_POSITION_X,ship_01_POSITION_Y,ship_01_POSITION_Z,ship_01_ROTATION,ship_01_COLOR);
DrawShip(ship_02_POSITION_X,ship_02_POSITION_Y,ship_02_POSITION_Z,ship_02_ROTATION,ship_02_COLOR);
glutSwapBuffers();
}

here is my movement code

void
 
 
ship_left_two()
{
ship_01_ROTATION+=2.5f;
}
void
 
 
ship_right_two()
{
ship_01_ROTATION-=2.5f;
}
void
 
 
ship_up_two()
{
ship_01_POSITION_Y+=0.1f;
}
void
 
 
ship_down_two()
{
ship_01_POSITION_Y-=0.1f;
}
void
 
 
ship_up()
{
ship_02_POSITION_Y+=0.1f;
}
void
 
 
ship_down()
{
ship_02_POSITION_Y-=0.1f;
}
void
 
 
ship_left()
{
ship_02_ROTATION+=2.5f;
}
void
 
 
ship_right()
{
ship_02_ROTATION-=2.5f;
}
 

 

 



Sponsor:

#22 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 01 July 2013 - 03:57 PM

thanks for all the help marc. do you have anymore input to give me?



#23 marcClintDion   Members   -  Reputation: 431

Like
0Likes
Like

Posted 01 July 2013 - 04:19 PM

I'm working on it right now, hold please.


Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, because you know that the testing of your faith produces perseverance. Let perseverance finish its work so that you may be mature and complete, not lacking anything.


#24 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 01 July 2013 - 04:52 PM

cool thanks



#25 marcClintDion   Members   -  Reputation: 431

Like
0Likes
Like

Posted 01 July 2013 - 04:52 PM

It looks like the ships points are being defined off-center.   Once you've fixed this we can proceed to rotating them properly to face their trajectories
//=======================================================================================================
      glBegin(GL_LINE_LOOP);
          glVertex3f(-1.5f,-0.25f,0.0f);
          glVertex3f(-1.75f,-0.5f,0.0f);
          glVertex3f(-2.0f,-0.5f,0.0f);
          glVertex3f(-1.5f,0.5f,0.0f);
          glVertex3f(-1.0f,-0.5f,0.0f);
          glVertex3f(-1.25f,-0.5f,0.0f);
          glVertex3f(-1.5f,-0.25f,0.0f);
     glEnd();
//=======================================================================================================
The rest is looking good so far...
 
Edit: +1.5 to all the X values centers them on the Y-axis so they rotate properly.

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, because you know that the testing of your faith produces perseverance. Let perseverance finish its work so that you may be mature and complete, not lacking anything.


#26 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 01 July 2013 - 04:55 PM

I fixed the off center problem. but I still cant get the ships to follow their nose, it just moves up and down regardless of where it is pointing at.



#27 marcClintDion   Members   -  Reputation: 431

Like
0Likes
Like

Posted 01 July 2013 - 06:29 PM

Alright, this will be a bit more involved.  I've worked on a couple of different ways to do this over the years but sometimes these things can become overly complicated when you start running into oddities that require extra code to deal with all the weird errors that pop up when angles switch from 0 to 360 or when things switch from positive to negative so to start off with let's try just roughing out the proper angle for a given direction. 

 

So to start off, let's just define an angle that is attached to a particular direction, this will cause the ship to snap instantly to the proper rotation but we can work on how to make it a smooth transition afterwards. 

 

For UP,   the angle is 0.0

For LEFT,   the angle is 90.0

For DOWN, the angle is 180.0

For RIGHT,  the angle is 270.0

 

//=================================================================

ship_down_two() { ship_01_POSITION_Y -= 0.1;

ship_01_ROTATION   = 180.0; }

//=================================================================


Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, because you know that the testing of your faith produces perseverance. Let perseverance finish its work so that you may be mature and complete, not lacking anything.


#28 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 01 July 2013 - 06:37 PM

here is my code for my previous rotation routine

void
 
 
update(int value)
{
angle+=0.1f;
 
 
if(angle > 360.0f)
{
angle-=360;
}
glutPostRedisplay();
glutTimerFunc(25,update,0);
}


#29 marcClintDion   Members   -  Reputation: 431

Like
0Likes
Like

Posted 01 July 2013 - 06:41 PM

I was helping someone else on another thread to get a model of his rotating towards where he touches the screen with his finger.  Since I already had your code pasted into a simple framework I decided to work on his using the ship code we have been working on.  Here are the results so far.  It is still in the preliminary stages but it is working. 

 

//===========================================================================================================================

void Render(void)

{

       
//============================= - The next seven lines of code seems to have round 1 taken care of.
incrementTime += 0.001;  
       //--------------------------------
       
       distanceBetweenModels[1] = ship_02_POSITION[1] - ship_01_POSITION[1];                              
       distanceBetweenModels[0] = ship_02_POSITION[0] - ship_01_POSITION[0];                                    
 
       //------------------------------------------------------------------------------------------------------
 
       targetRotation = atan2(distanceBetweenModels[0], distanceBetweenModels[1])* 180 / 3.14159;        
 
       
       blendedRotation = (startRotation * (1.0 - sliderTime)) + (targetRotation * sliderTime);                        
 
       if(sliderTime <= 1.0) // This will prevent the rotation from over-shooting
       {
             sliderTime += incrementTime;                                                                                                
       }
 
       //=====================================================================================================================
      glPushMatrix();
 
              DrawShip(ship_01_POSITION_X, ship_01_POSITION_Y, ship_01_POSITION_Z, ship_01_ROTATION, ship_01_COLOR);
 
      glPopMatrix();
      //-----------------------------------------------------------------------------------------------------------------------------------------------------------------
      glPushMatrix();
 
             DrawShip(ship_02_POSITION_X, ship_02_POSITION_Y, ship_02_POSITION_Z, ship_02_ROTATION, ship_02_COLOR);
 
      glPopMatrix();
       //=========================================================================================
 
}

//===========================================================================================================================

 

What happens here is ship_01 rotates towards ship_02 when the program starts up.  There is nothing yet to update the program so that it will keep happening as the program parameters changes.  It is basically, for now,  a two frame animation that causes a smooth rotation to take place.  It has to be updated still so that it can be an infinite number of frames that take place one after the other as things progress.  We'll have to see how this goes but I think it will work better than my other solutions up to this point.


Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, because you know that the testing of your faith produces perseverance. Let perseverance finish its work so that you may be mature and complete, not lacking anything.


#30 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 01 July 2013 - 06:56 PM


For UP, the angle is 0.0

For LEFT, the angle is 90.0

For DOWN, the angle is 180.0

For RIGHT, the angle is 270.0



//=================================================================

ship_down_two()
{
ship_01_POSITION_Y -= 0.1;

ship_01_ROTATION = 180.0;
}

//=================================================================

well now it snaps to a particular direction but it still does not follow its nose smoothly.



#31 marcClintDion   Members   -  Reputation: 431

Like
0Likes
Like

Posted 01 July 2013 - 07:10 PM

EDIT: Nope!  the following doesn't work yet, it just keeps spinning around.

 

EDIT_2: Now it works! Reversing the positive and negative signs that are highlighted green fixed it

I tested it with some fairly ridiculous numbers like 5000 and -5000 and it still worked, so I think it's good.

 

//==================================================================================================

 

We could do something like the following with a variable called "targetAngle"

 

//=================================================================

ship_down_two() { ship_01_POSITION_Y -= 0.1;

targetAngle   = 180.0; }

//=================================================================

update(int value)

{

 

if(ship_01_ANGLE < targetAngle) { angle += 1.0; // When this was -= the ship kept spinning endlessly, this note is left here so people can see how a little thing can introduce bugs }

if(ship_01_ANGLE > targetAngle) { angle -= 1.0; // When this was += the ship kept spinning endlessly }

 

 

 

}


Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, because you know that the testing of your faith produces perseverance. Let perseverance finish its work so that you may be mature and complete, not lacking anything.


#32 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 02 July 2013 - 06:28 PM

thanks marc, anymore input?



#33 marcClintDion   Members   -  Reputation: 431

Like
1Likes
Like

Posted 02 July 2013 - 11:17 PM

You're going to have to name one of the ships after me Phil.


Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, because you know that the testing of your faith produces perseverance. Let perseverance finish its work so that you may be mature and complete, not lacking anything.


#34 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 03 July 2013 - 11:01 AM

thanks for all the help but I am still stuck.



#35 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 04 July 2013 - 02:47 PM

I have almost solved my problem, I just need a little more help.



#36 marcClintDion   Members   -  Reputation: 431

Like
0Likes
Like

Posted 05 July 2013 - 12:34 AM

Name it.  I've been having fun with this.


Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, because you know that the testing of your faith produces perseverance. Let perseverance finish its work so that you may be mature and complete, not lacking anything.


#37 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 05 July 2013 - 02:45 PM

I just need to get the ship to follow its nose, it has something to do with the translate or rotate command.  I have used all the code you have given me. thank once again for all  your help. I was thinking about using trigonometric matrices instead of the glRotatef command.



#38 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 07 July 2013 - 03:35 PM

do you have anymore input marc?



#39 marcClintDion   Members   -  Reputation: 431

Like
0Likes
Like

Posted 07 July 2013 - 03:59 PM

Things are getting a bit confusing here with bits of code that have all been updated and changed.  I'm not sure where anything stands, controls, algorithms, etc...

We might have to start bouncing the full source code back and forth so that we are on the same page with everything, otherwise I'm having to guess at what's what.


Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, because you know that the testing of your faith produces perseverance. Let perseverance finish its work so that you may be mature and complete, not lacking anything.


#40 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 07 July 2013 - 04:08 PM

here is all of my code

#include
 
 
<glut.h>
using
 
 
namespace std;
GLfloat
 
 
angle=0.0f;
GLfloat
 
 
up=0.5f;
GLfloat
 
 
up_two=0.0f;
GLfloat
 
 
ship_01_POSITION_X=0.0f;
GLfloat
 
 
ship_01_POSITION_Y=0.0f;
GLfloat
 
 
ship_01_POSITION_Z=0.0f;
GLfloat
 
 
ship_01_ROTATION=0.0f;
GLfloat
 
 
ship_02_POSITION_X=1.5f;
GLfloat
 
 
ship_02_POSITION_Y=0.0f;
GLfloat
 
 
ship_02_POSITION_Z=0.0f;
GLfloat
 
 
ship_02_ROTATION=0.0f;
GLfloat
 
 
ship_01_COLOR=1.0f;
GLfloat
 
 
ship_02_COLOR=1.0f;
GLfloat
 
 
ship_01_bullet_POSITION_X=0.0f;
void
 
 
SetupRC()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glOrtho(-10.0f,10.0f,-10.0f,10.0f,-1.0f,1.0f);
}
void
 
 
ship_left_two()
{
ship_01_ROTATION+=2.5f;
}
void
 
 
ship_right_two()
{
ship_01_ROTATION-=2.5f;
}
void
 
 
ship_up_two()
{
ship_01_POSITION_Y+=0.1f;
}
void
 
 
ship_down_two()
{
ship_01_POSITION_Y-=0.1f;
}
void
 
 
ship_up()
{
ship_02_POSITION_Y+=0.1f;
}
void
 
 
ship_down()
{
ship_02_POSITION_Y-=0.1f;
}
void
 
 
ship_left()
{
ship_02_ROTATION+=2.5f;
}
void
 
 
ship_right()
{
ship_02_ROTATION-=2.5f;
}
void
 
 
bullet()
{
up+=0.1f;
 
 
if(up >= 10.0f)
{
up=0.5f;
}
}
void
 
 
bullet_two()
{
up_two+=0.1f;
 
 
if(up_two >= 10.0f)
{
up_two=0.5f;
}
}
void
 
 
update(int value)
{
angle+=0.1f;
 
 
if(angle > 360.0f)
{
angle-=360.0f;
}
glutPostRedisplay();
glutTimerFunc(25,update,0);
}
void
 
 
DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
{
glPushMatrix();
// glTranslatef(-position_X,-position_Y, -position_Z);
glRotatef(
 
rotation,0.0f,0.0f,1.0f);
glTranslatef(
 
position_X, position_Y, position_Z);
glColor3f(
 
color,0.0f,0.0f);
glBegin(
 
GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(0.0f,-0.25f,0.0f);
glEnd();
glPopMatrix();
}
void
 
 
DrawBullet(GLfloat position_X,GLfloat position_Y,GLfloat position_Z,GLfloat rotation)
{
glPushMatrix();
glColor3f(1.0f,0.0f,0.0f);
glRotatef(
 
rotation,0.0f,0.0f,1.0f);
glTranslatef(
 
position_X,position_Y,position_Z);
glPointSize(2.0f);
glBegin(
 
GL_POINTS);
glVertex3f(0.0f,up++,0.0f);
glEnd();
glPopMatrix();
}
void
 
 
Render(void)
{
glClear(
 
GL_COLOR_BUFFER_BIT);
 
glPushMatrix();
DrawShip(ship_01_POSITION_X,ship_01_POSITION_Y,ship_01_POSITION_Z,ship_01_ROTATION,ship_01_COLOR);
glPopMatrix();
glPushMatrix();
DrawShip(ship_02_POSITION_X,ship_02_POSITION_Y,ship_02_POSITION_Z,ship_02_ROTATION,ship_02_COLOR);
glPopMatrix();
 
glPushMatrix();
DrawBullet(ship_01_bullet_POSITION_X,ship_01_POSITION_Y,ship_01_POSITION_Z, ship_01_ROTATION);
glPopMatrix();
 
glutSwapBuffers();
}
 
 
void
 
 
handleKeypress(unsigned char key, int x, int y)
{
 
 
switch(key)
{
 
 
case 27:
exit(0);
 
 
break;
 
 
case 32:
bullet();
 
 
break;
 
 
case 'w':
ship_up();
 
 
break;
 
 
case 'x': 
ship_down();
 
 
break;
 
 
case 'a':
ship_left();
 
 
break;
 
 
case 'd':
ship_right();
 
 
break;
 
 
case 's':
bullet_two();
 
 
break;
}
}
void
 
 
mySpecialKeys(int key, int x, int y)
{
 
 
switch(key)
{
 
 
case GLUT_KEY_LEFT:
{
ship_left_two();
 
 
break;
}
 
 
case GLUT_KEY_RIGHT:
{
ship_right_two();
 
 
break;
}
 
 
case GLUT_KEY_UP:
{
ship_up_two();
 
 
break;
}
 
 
case GLUT_KEY_DOWN:
{
ship_down_two();
 
 
break;
}
}
}
void
 
 
main(int argc, char* argv[])
{
 
 
glutInit(&argc, argv);
glutInitDisplayMode(
 
GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutInitWindowPosition(300,100);
 
 
glutCreateWindow("Asteroids");
glutDisplayFunc(Render);
glutKeyboardFunc(handleKeypress);
glutSpecialFunc(mySpecialKeys);
SetupRC();
glutTimerFunc(25,update,0);
glutMainLoop();
}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS