Need urgent help with moving ball

Basically I placed my textures and everything on the window and now I want my sphere or the ball to move left or right, I have the special key code, etc but for some reason still my ball isn’t moving left or right!!?

Why is this happening please check my code

I WILL HIGHLIGHT THE IMPORTANT MOVEMENT RELATED CODES IN BOLD

//Windows - uncomment the includes below

#include “shared/gltools.h” //OpenGL toolkit - in the local

shared folder
#include <math.h> //These were included in

glm.cpp so I kept them here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include “shared/glm.h”
#include <iostream> //Needed for console output

(debugging)

GLfloat Ballx = 0.0;
GLfloat Bally = 0.0;
GLfloat Speed = 3.0;

//
// texture.cpp
//
//
// Created by Philip Trwoga on 09/11/2012.
// Copyright © 2012 Philip Trwoga. All rights reserved.
// DO NOT DISTRIBUTE WITHOUT THE AUTHOR’S PERMISSION

//Windows uncomment the includes below

//Mac includes + frameworks OpenGL and GLUT - comment out for Windows
//#include <iostream>
//#include <stdio.h>
//#include <OpenGL/glu.h>
//#include <GLUT/glut.h>
//#include “gltools.h”
//#include <OpenGL/gl.h>
//#include <OpenGL/glext.h>
//#include <OpenGL/gl3ext.h>

//note that this needs gltools.h and gltools.cpp to be included in the shared directory

//can define any number of textures here - we just have 2 images
//these are just integers so you can use them as such
#define IMAGE1 0
#define IMAGE2 1
#define IMAGE3 2
#define IMAGE4 3
#define IMAGE5 4

#define TEXTURE_COUNT 5
GLuint textures[TEXTURE_COUNT];

//below is simply a character array to hold the file names
//note that you may need to replace the below with the full directory root depending on

where you put your image files
//if you put them where the exe is then you just need the name as below
const char *textureFiles[TEXTURE_COUNT] = {“padhamsBG.tga”,

“grass_diff.tga”,“brick_texture.tga”,“brick_texture.tga”,“brick_texture.tga”};

//for lighting if you want to experiment with these

GLfloat mKa[4] = {0.11f,0.06f,0.11f,1.0f}; //ambient
GLfloat mKd[4] = {0.43f,0.47f,0.54f,1.0f}; //diffuse
GLfloat mKs[4] = {1.0f,1.0f,1.0f,1.0f}; //specular
GLfloat mKe[4] = {0.5f,0.5f,0.0f,1.0f}; //emission

// Useful lighting colour values
GLfloat whiteLightBright[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat redLight[] = { 1.0f, 0.0f, 0.0f, 1.0f };
GLfloat whiteLightLessBright[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat lightPos[] = { 100.0f, 100.0f, 50.0f, 1.0f };

//we need these for the texture loader
//they are to do with the image format and size
GLint iWidth, iHeight, iComponents;
GLenum eFormat;
// this is a pointer to memory where the image bytes will be held
GLbyte *pBytes0;

//end of intialisation

//------------------------------------//
void drawTexturedQuad(int image)
{
//add some lighting normals for each vertex
//draw the texture on the front
glEnable(GL_TEXTURE_2D);
// glFrontFace(GL_CW); //use glFrontFace(GL_CW) to texture the other side - not needed

here as we set this elsewhere
glColor3f(0.8, 0.8, 0.8);
glEnable( GL_TEXTURE_2D );
//bind the texture
glBindTexture(GL_TEXTURE_2D, textures[image]);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0,0.0);
glVertex3f(-50.0, 0.0,100.0);
glTexCoord3f(1.0,0.0,0.0);
glVertex3f(50.0, 0.0,100.0);
glTexCoord2f(1.0,1.0);
glVertex3f(50.0, 100.0,100.0);
glTexCoord2f(0.0,1.0);
glVertex3f(-50.0, 100.0,100.0);
glEnd();
glDisable( GL_TEXTURE_2D );

}

// Called to draw scene

//FUNCTION FOR MY BALL

void ball(){
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 1.0);
//specify the vertices (points in 3D space) of the shape - note that these are 2D points
glutSolidSphere( 5.0, 20.0, 20.0);
glEnd();
glFlush();
}

[b]void SpecialKeys(int key, int x, int y) // a function for the movement key - car moves
{

if(key == GLUT_KEY_UP){
Bally = Bally + Speed;

}

if(key == GLUT_KEY_DOWN){
Bally = Bally - Speed;

}

if(key == GLUT_KEY_LEFT){
Ballx = Ballx - Speed;

}

if(key == GLUT_KEY_RIGHT){
Ballx = Ballx + Speed;

}

glutPostRedisplay();

}[/b]

void RenderScene(void)
{

// Clear the window with current clearing colour
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glMatrixMode(GL_MODELVIEW);


		glPushMatrix();
   //rotate and then translate the quad 
glScalef(4,4,4);
   glTranslatef(0.0, -20.0, 50.0);
//try setting to -185.0 to see the back of the quad
	ball();
glPopMatrix();

// a test point
//use for locating points in your view
/* glEnable(GL_POINT_SMOOTH);
glPointSize(10.0);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex3f(0.0f, 0.0f, 100.0f);
glEnd();
*/

///////////////////////////////////////////////////////////////////// backgroujnd
glPushMatrix();
//rotate the quad slightly
glTranslatef(0.0, -190.0, -350.0);
// glRotatef(5.0, 0.0, 1.0, 0.0);
glScalef(10.0,5.5,1.0);
drawTexturedQuad(IMAGE1);
glPopMatrix();

///////////////////////////////////////////////////////// grass

glPushMatrix();
   //rotate and then translate the quad 
glTranslatef(-10.0, -300.0, 50.0);
glRotatef(-70.0, 1.0, 0.0, 0.0);
//try setting to -185.0 to see the back of the quad
glScalef(8.0,7.0,1.5);
drawTexturedQuad(IMAGE2);
glPopMatrix();

////////////////////////////////////////////////////////////// main wall 


 glPushMatrix();
   //rotate and then translate the quad 
glTranslatef(-5.0, -20.0, 0.0);
//glRotatef(-45,0.0 ,1.0, 0.0);
//try setting to -185.0 to see the back of the quad
glScalef(2.4,0.5,1.0);
drawTexturedQuad(IMAGE3);
glPopMatrix();



	////////////////////////////////////////////////////////////// left brick

 glPushMatrix();
   //rotate and then translate the quad 
glTranslatef(-290.0, -35.0, 50.0);
glRotatef(100.0, 0.0 ,1.0, 0.0);
//try setting to -185.0 to see the back of the quad
glScalef(4.0,0.88,1.0);
drawTexturedQuad(IMAGE4);
glPopMatrix();

///////////////// right brick

 glPushMatrix();
   //rotate and then translate the quad 
glTranslatef(310.0, -35.0, 50.0);
glRotatef(-90, 0.0 ,1.0, 0.0);
//try setting to -185.0 to see the back of the quad
glScalef(4.0,0.88,1.0);
drawTexturedQuad(IMAGE5);
glPopMatrix();

}

// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
//textures

GLuint texture;
// allocate a texture name
glGenTextures( 1, &texture );
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
//make sure any TGA has no alpha channel - photoshop is a good converter to targa 

(TGA)
//the gltLoadTGA method is found in gltools.cpp and is from the OpenGL SuperBible

book
//there are quite a few ways of loading images
// Load textures in a for loop
glGenTextures(TEXTURE_COUNT, textures);
//this puts the texture into OpenGL texture memory
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
for(int iLoop = 0; iLoop < TEXTURE_COUNT; iLoop++)
{
// Bind to next texture object
glBindTexture(GL_TEXTURE_2D, textures[iLoop]);

    // Load texture data, set filter and wrap modes
    //note that gltLoadTGA is in the glm.cpp file and not a built-in openGL function
    pBytes0 = gltLoadTGA(textureFiles[iLoop],&iWidth, &iHeight,
                         &iComponents, &eFormat);
    
    glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, 

GL_UNSIGNED_BYTE, pBytes0);

        //set up texture parameters
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    //try these too
   // glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
    // glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    free(pBytes0);
}

//enable textures
glEnable(GL_TEXTURE_2D);


glEnable(GL_DEPTH_TEST);	// Hidden surface removal    
glFrontFace(GL_CCW);// Counter clock-wise polygons face out
glEnable(GL_CULL_FACE);		// Do not calculate inside

// glCullFace(GL_FRONT_AND_BACK);

// Enable lighting

// glEnable(GL_LIGHTING);
glEnable(GL_POINT_SMOOTH);
// Setup and enable light 0
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLightLessBright);
glLightfv(GL_LIGHT0,GL_DIFFUSE,redLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);

// Enable colour tracking
glEnable(GL_COLOR_MATERIAL);

// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

// Black blue background
glClearColor(0.0f, 0.0f, 0.03f, 1.0f );

}

void TimerFunc(int value)
{
glutSwapBuffers();
glutPostRedisplay();
glutTimerFunc(25, TimerFunc, 1);

}

void ChangeSize(int w, int h)
{
GLfloat fAspect;

// Prevent a divide by zero
if(h == 0)
    h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

// Calculate aspect ratio of the window
fAspect = (GLfloat)w/(GLfloat)h;

// Set the perspective coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// field of view of 45 degrees, near and far planes 1.0 and 1000
//that znear and zfar should typically have a ratio of 1000:1 to make sorting out z 

depth easier for the GPU
gluPerspective(55.0f, fAspect, 1.0, 1000.0);
// Modelview matrix reset
glMatrixMode(GL_MODELVIEW);
//pull the eye position back to view the scene
gluLookAt(0.00,0.00,400.0,//eye
0.00,0.00,0.00,//centre
0.00,1.00,0.00);//up
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);// a 4:3 ratio
glutCreateWindow(“Textures Tutorial”);
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
glutTimerFunc(25, TimerFunc, 1);
SetupRC();
glutMainLoop();
glutSpecialFunc(SpecialKeys);
return 0;
}

Am I putting the special KEYS FUNCTION CODE in the wrong section?

Please I’ve wasted over 2 HOURS TRYING TO FIGURE OUT HOW TO MOVE THE BALL left or right! I USED MY OLD CW CODE TO MAKE A SPHERE AND MOVE THE BALL AND IT WORKED BUT FOR SOME REASON IT DOESN’T WITH MY CODE WITH TEXTURES…

hi,

thats verry simple:
you are changing the value for e.g. Ballx
but you are never using it to set the balls position … so how should it move ??

maybe something like:

glTranslatef( Ballx, Bally, 50.0);

cu
uwi

[QUOTE=uwi2k2;1246118]hi,

thats verry simple:
you are changing the value for e.g. Ballx
but you are never using it to set the balls position … so how should it move ??

maybe something like:

glTranslatef( Ballx, Bally, 50.0);

cu
uwi[/QUOTE]

hey

I tried putting

glTranslatef( Ballx, Bally, 50.0);

everywhere I can but it’s not working still where am I actually supposed to put it ):

This is what I tried doing so far


void RenderScene(void)
{

// Clear the window with current clearing colour
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glMatrixMode(GL_MODELVIEW);


glPushMatrix();
glTranslatef( Ballx, Bally, 50.0);
glPopMatrix();
glFlush();

		glPushMatrix();
   //rotate and then translate the quad 
glScalef(4,4,4);
   glTranslatef(0.0, -20.0, 50.0);
//try setting to -185.0 to see the back of the quad
	ball();

glPopMatrix();

// a test point
//use for locating points in your view
/* glEnable(GL_POINT_SMOOTH);
glPointSize(10.0);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex3f(0.0f, 0.0f, 100.0f);
glEnd();
*/

///////////////////////////////////////////////////////////////////// backgroujnd
glPushMatrix();
//rotate the quad slightly
glTranslatef(0.0, -190.0, -350.0);
// glRotatef(5.0, 0.0, 1.0, 0.0);
glScalef(10.0,5.5,1.0);
drawTexturedQuad(IMAGE1);
glPopMatrix();

///////////////////////////////////////////////////////// grass

glPushMatrix();
   //rotate and then translate the quad 
glTranslatef(-10.0, -300.0, 50.0);
glRotatef(-70.0, 1.0, 0.0, 0.0);
//try setting to -185.0 to see the back of the quad
glScalef(8.0,7.0,1.5);
drawTexturedQuad(IMAGE2);
glPopMatrix();

////////////////////////////////////////////////////////////// main wall 


 glPushMatrix();
   //rotate and then translate the quad 
glTranslatef(-5.0, -20.0, 0.0);
//glRotatef(-45,0.0 ,1.0, 0.0);
//try setting to -185.0 to see the back of the quad
glScalef(2.4,0.5,1.0);
drawTexturedQuad(IMAGE3);
glPopMatrix();



	////////////////////////////////////////////////////////////// left brick

 glPushMatrix();
   //rotate and then translate the quad 
glTranslatef(-290.0, -35.0, 50.0);
glRotatef(100.0, 0.0 ,1.0, 0.0);
//try setting to -185.0 to see the back of the quad
glScalef(4.0,0.88,1.0);
drawTexturedQuad(IMAGE4);
glPopMatrix();

///////////////// right brick

 glPushMatrix();
   //rotate and then translate the quad 
glTranslatef(310.0, -35.0, 50.0);
glRotatef(-90, 0.0 ,1.0, 0.0);
//try setting to -185.0 to see the back of the quad
glScalef(4.0,0.88,1.0);
drawTexturedQuad(IMAGE5);
glPopMatrix();

}

But thank you so much anyway, I am kind of getting the hang of it, I just don’t know where to put it can you please tell me where

Kind Regards

Try this -


glPushMatrix();

   glScalef (4.0, 4.0, 4.0);
   glTranslatef (Ballx, Bally-20.0, 50.0);

   ball();

glPopMatrix();

[QUOTE=Carmine;1246137]Try this -


glPushMatrix();

   glScalef (4.0, 4.0, 4.0);
   glTranslatef (Ballx, Bally-20.0, 50.0);

   ball();

glPopMatrix();

[/QUOTE]

still not working ):

I just don’t understand why, what am I doing wrong I included everything I was supposed to

thanks for helping me anyway

Any ideas maybe I’m repeating the code in this bit

is this bit right or wrong?

void RenderScene(void)
{

// Clear the window with current clearing colour
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT

glMatrixMode(GL_MODELVIEW);

glPushMatrix();
//rotate and then translate the quad
glScalef(4,4,4);
glTranslatef(0.0, -20.0, 50.0);
//try setting to -185.0 to see the back of the quad
ball();
glPopMatrix();

Anybody?

It’s been a week and I still haven’t solved this problem i absolutely hate programming and opengl i just want to get this assignment over and done with, i’ve had enough.

i declared ballX and bally in the translation code for the ball

glTranslatef (Ballx, Bally-20.0, 50.0)

But it still ain’t moving.

I want the ball to move left and right with arrow keys, once I figure this out I will move the ball forward with the z key. but on z axis… Logically it’s easy but for some reason in programming it’s so hard

You should just be able to say and type

if ball (variable) == key > right… ball move right by x (the integer the ball has to move on) but no it has to be complicated.

[FONT=Courier New]


int main (int argc, char* argv[])
{
    glutInit            (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize  (800, 600);
    glutCreateWindow    ("Textures Tutorial");
    glutReshapeFunc     (ChangeSize);
    glutDisplayFunc     (RenderScene);
    glutTimerFunc       (25, TimerFunc, 1);
    glutSpecialFunc     (SpecialKeys);
    SetupRC ();
    glutMainLoop ();

//  glutSpecialFunc (SpecialKeys);

    return 0;
}

[/FONT]

Am I putting the special KEYS FUNCTION CODE in the wrong section?

I would think that ‘glutSpecialFunc’ should appear before ‘glutMainLoop’, as shown above.

Here’s a couple of basic things to try.

  1. Put a print statement in the ‘SpecialKeys’ function to examine the values of BALLX and BALLY each time an arrow key is pushed. Are you getting the values you expected?

  2. Put a print statement (or use a debugger) to examine the values of BALLX and BALLY in the ‘RenderScene’ function. Once again, are you getting what you expected?

  3. Explicitly put numbers in the ‘RenderScene’ function for BALLX and BALLY. Is the ball moving the way you expected?

Good luck.

[QUOTE=Carmine;1246346][FONT=Courier New]


int main (int argc, char* argv[])
{
    glutInit            (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize  (800, 600);
    glutCreateWindow    ("Textures Tutorial");
    glutReshapeFunc     (ChangeSize);
    glutDisplayFunc     (RenderScene);
    glutTimerFunc       (25, TimerFunc, 1);
    glutSpecialFunc     (SpecialKeys);
    SetupRC ();
    glutMainLoop ();

//  glutSpecialFunc (SpecialKeys);

    return 0;
}

[/FONT]

I would think that ‘glutSpecialFunc’ should appear before ‘glutMainLoop’, as shown above.

[/QUOTE]

OMG THANK YOU SO MUCH IT WORKS NOW IT MVOES LEFT RIGHT AS WELL NOT I NEED TO DO ONE FOR Z AXIS IT MOVES BUT THE SHAPE OF THE BALL SHIFTS A BIT BUT I WILLT RY AND CORRECT THAT THANK YOU SO MUCH!!! AND THANKS TO EVERYBODY ELSE

I hope I don;t have problems in the future where I have to bother you.

I was stressed out for a week because I put mainloop before special keys

thanks so much again

hi did you sort out the ball moving to hit the wall?

Yes thank you so much to everybody

but now i have another problem well i dont know how to move or animate the ball

void keyPressed (unsigned char key, int x, int y) {

	    if (key == 'z') { 

			Ballz = Ballz - Speed;
			Bally = Bally + 2.8;

}

the above happens when i press the z key and it increments it once but it stops, how do I keep on animating the ball so it continues until it hits the target