opengl glut rotation keyboard directions - c++

i wanna make a small app that allow me to move the triangle to top/bottm left/right.
and when i press w -> rotate above and move above , s => rotate bottom and move bottom ,, d -> rotate right and moves right , a -> rotate left and move left ..
here is my code :
#include <GL/glut.h> // (or others, depending on the system in use)
float xpoint1 = 0.0f;
float xpoint2 = 50.0f;
float xpoint3 = 25.0f;
float ypoint1 = 0.0f, ypoint2 = 0.0f, ypoint3 = 20.0f;
double direction = 0.0;
void Keys(unsigned char key, int x, int y) {
if (key == 'a') {
if (xpoint1 != 0) {
xpoint1 -= 1.0f;
xpoint2 -= 1.0f;
xpoint3 -= 1.0f;
direction = 90.0;
}
}
else if (key == 'd') {
if (xpoint2 != 200) {
xpoint1 += 1.0f;
xpoint2 += 1.0f;
xpoint3 += 1.0f;
direction = 270.0;
}
}
else if (key == 'w') {
if (ypoint3 != 150) {
ypoint1 += 1.0f;
ypoint2 += 1.0f;
ypoint3 += 1.0f;
direction = 0.0;
}
}
else if (key == 's') {
if (ypoint3 != 0) {
ypoint1 -= 1.0f;
ypoint2 -= 1.0f;
ypoint3 -= 1.0f;
direction = 180.0;
}
}
glutPostRedisplay();
}
void resizeChange(int w, int h) {
glClearColor(1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glMatrixMode(GL_PROJECTION); // Set projection parameters.
glLoadIdentity();
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
glViewport(0, 0, w, h);
glMatrixMode(GL_MODELVIEW);
}
void lineSegment(void)
{
glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f(0.0, 0.4, 0.2); // Set line segment color to green.
//gluLookAt(0.0, 0.0, 5.0,
// 0.0, 0.0,0.0,
// 0.0, 1.0, 0.0
//);
glLoadIdentity();
glRotated(direction, 0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(xpoint1, ypoint1);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(xpoint2, ypoint2);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(xpoint3, ypoint3);
glEnd();
//degree += 1.0;
glFlush(); // Process all OpenGL routines as quickly as possible.
//glutSwapBuffers();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv); // Initialize GLUT.
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition(50, 100); // Set top-left display-window position.
glutInitWindowSize(600, 600); // Set display-window width and height.
glutCreateWindow("An Example OpenGL Program"); // Create display window.
glutDisplayFunc(lineSegment); // Send graphics to display window.
glutReshapeFunc(resizeChange);
//glutIdleFunc(lineSegment);
glutKeyboardFunc(Keys);
glutMainLoop(); // Display everything and wait.
}
the problem is when i try to change direction of the triangle it disappered ?! , whats the problem ?!

glRotated rotates around (0, 0). Actually rotate the triangle out of sight. You have to rotate the triangle first and then move it to its place in the world.
Do not change the vertex coordinates but add a translation matrix with glTranslate:
double tx = 0.0, ty = 0.0;
double direction = 0.0;
void Keys(unsigned char key, int x, int y)
{
if (key == 'a') {
tx -= 1.0;
direction = 90.0;
}
else if (key == 'd') {
tx += 1.0;
direction = 270.0;
}
else if (key == 'w') {
ty += 1.0;
direction = 0.0;
}
else if (key == 's') {
ty -= 1.0;
direction = 180.0;
}
glutPostRedisplay();
}
void lineSegment(void)
{
// [...]
glTranslated(tx, ty, 0.0);
glRotated(direction, 0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-25.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(25.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(0.0, 20.0);
glEnd();
// [...]
}

Related

Animate bouncing box using?

I need to make an animation where the box starts from (-15, 0) then going up to (-8, 15) and when it reach that, then it came down. and so on, just like a DVD screen saver, but i can't seem to find the formula for it to work.
Here's the code:
#include<windows.h>
#include<GL/glu.h>
#include<GL/glut.h>
#ifdef APPLE
#include<GLUT/glut.h>
#else
#include<GL/glut.h>
#endif
#include<stdio.h>
#include<stdlib.h>
#include <GL/freeglut.h>
//Variable menampung sudut awal
float xpos = -15.0;
float deltax = 0.5;
float ypos = 0.0;
bool balik = true;
bool turun = true;
//Variable mengatur perubahan sudut
void myInit(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-15.0f, 15.0f, -15.0f, 15.0f);
}
void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-15.0, 0.0);
glVertex2f(15.0, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-8.0, 15.0);
glVertex2f(-8.0, -15.0);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(8.0, 15.0);
glVertex2f(8.0, -15.0);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(0.0, 15.0);
glVertex2f(0.0, -15.0);
glEnd();
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.2 + xpos, 0.2 + ypos);
glVertex2f(-0.2 + xpos, -0.2 + ypos);
glVertex2f(0.2 + xpos, -0.2 + ypos);
glVertex2f(0.2 + xpos, 0.2 + ypos);
glEnd();
glutSwapBuffers();
}
void Timer(int) {
glutPostRedisplay();
glutTimerFunc(100, Timer, 0);
if (ypos < 15 && xpos <-8 && turun == true){
xpos = xpos + deltax;
ypos = (2.143*xpos) + 32.143;
}
else {turun = false;};
if (ypos > -15 && xpos <8 && turun == false){
xpos = xpos + deltax;
ypos = -1,875*xpos + ;
}
else turun = true;
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Animasi");
glutDisplayFunc(myDisplay);
myInit();
glutTimerFunc(0, Timer, 0);
//Perulangan
glutMainLoop();
return 0;
}
If you want the box to bounce you can just reverse the x or y velocity depending on which axis you hit. If you want the box to bounce with Gravity you will use vector parametric equations like so:
y = -16t^2 + vsin(x) + s.y and x = vcos(x) + s.x where t is time v is magnitude(velocity) and s is the starting position of the box which will be reset on every collision with the sides. Keep the magnitude the same to infinitely bounce. If the equation is confusing look up vector pre-calculus online.

openGL fixing camera?

This code makes cube. Code works correctly.
But, if I increase the vertex values, it does not show in the display. I want bigger cube, but bigger cube does not show in display. How can I fix it??
Thanks for answers...
#define A glVertex3f (-0.5, 0.5, -0.5)
#define B glVertex3f (-0.5, -0.5, -0.5)
#define C glVertex3f ( 0.5, -0.5, -0.5)
#define D glVertex3f ( 0.5, 0.5, -0.5)
#define E glVertex3f (-0.5, 0.5, 0.5)
#define F glVertex3f (-0.5, -0.5, 0.5)
#define G glVertex3f ( 0.5, -0.5, 0.5)
#define H glVertex3f ( 0.5, 0.5, 0.5)
#define _USE_MATH_DEFINES
#include <cmath>
#include <stdlib.h>
#include <math.h>
#include <glut.h>
float distance = 5.0;
int longitude = 0, latitude = 0, ainc = 5;
int lastx = -1, lasty = -1;
void display (void)
{
float xc, yc, zc;
int type = GL_POLYGON; // or GL_LINE_LOOP
xc = distance * cos (latitude /180.0*M_PI) * cos (longitude/180.0*M_PI);
yc = distance * sin (latitude /180.0*M_PI);
zc = distance * cos (latitude /180.0*M_PI) * sin (longitude/180.0*M_PI);
glLoadIdentity ();
gluLookAt (xc, yc, zc, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Sides of the cube as loops or polygons, in anti-clockwise order.
glColor3f (1.0, 0.0, 0.0);
glBegin (type); A; B; C; D; glEnd(); // front
glColor3f (0.0, 1.0, 0.0);
glBegin (type); H; E; F; G; glEnd(); // back
glColor3f (0.0, 0.0, 1.0);
glBegin (type); B; C; G; F; glEnd(); // bottom
glColor3f (1.0, 0.0, 1.0);
glBegin (type); A; D; H; E; glEnd(); // top
glColor3f (0.0, 1.0, 1.0);
glBegin (type); A; E; F; B; glEnd(); // left
glColor3f (1.0, 1.0, 0.0);
glBegin (type); D; C; G; H; glEnd(); // right
glutSwapBuffers ();
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 27: case 'q': case 'Q':
exit (EXIT_SUCCESS);
break;
}
}
void special (int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
distance *= 1.1;
break;
case GLUT_KEY_DOWN:
distance /= 1.1;
break;
}
glutPostRedisplay ();
}
void click (int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
lastx = x;
lasty = y;
}
}
void mouse (int x, int y)
{
if (x > lastx) {
longitude = (longitude + ainc) % 360;
} else if (x < lastx) {
longitude = (longitude - ainc) % 360;
}
if (y > lasty) {
latitude = (latitude + ainc) % 360;
} else if (y < lasty) {
latitude = (latitude - ainc) % 360;
}
lastx = x;
lasty = y;
glutPostRedisplay ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (65.0, (GLfloat) w / (GLfloat) h, 1.0, 20.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.0, 0.0, -5.0);
}
int main (int argc, char *argv[])
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow (argv[0]);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(50.0, 1.0, 3.0, 7.0);
glMatrixMode (GL_MODELVIEW);
glutDisplayFunc (display);
glutKeyboardFunc (keyboard);
glutSpecialFunc (special);
glutMouseFunc (click);
glutMotionFunc (mouse);
glutReshapeFunc (reshape);
glEnable (GL_DEPTH_TEST);
glutMainLoop ();
return EXIT_SUCCESS;
}
I suspect it's you perspective that's broken.
Your near and far planes are 3.0 and 7.0 units. If you move your points outside these planes they will be removed. You also use 1.0 and 20.0 when you reshape the window, but they won't be used unless there's a reshape event.
I don't know what values you use for your cube but I think you move outside of your defined range. Try increasing the difference between the near and far places and make sure your cube falls between them.
For more info on gluPerspective see the documentation.
To scale an object's vertices, you only have to multiply them by the same number. For example if you want to make a cube twice as large, multiply all vertices by 2.0.
If the cube becomes so large that it's front side goes behind the "camera" then it will not render, and if your settings do not render the "back side" of faces, then your cube will not render at all (because the camera will be inside it).
You can also use glScalef(float x, float y, float z)

Rotating around a point different from origin

I'm trying to code a camera with glTranslate/glRotate. To implement the look-up / look-down functions I need all the objects in my rendering space to rotate around a point (that is, where the "camera" is at), point which usually differs from the origin. Still, things keep rotating around the origin. Is there a way to specify a different point?
EDIT: Added code
Thanks for your fast reply. It seems that I can't get it working right no matter what, so I've decided to add my code; I'd much appreciate if someone could take a look at it and tell me what changes are needed in order to translate/rotate/translate back.
#include <iostream>
#include <cmath>
#include <GLUT/GLUT.h>
const double roaming_step = .13;
double z_offset = .0;
double y_offset = .0;
double x_offset = .0;
const double angle_step = 1.5;
double angle_xz = .0;
double angle_yz = .0;
bool keyStates[256] = { false };
void drawFloor()
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex3f(-3.0, -1.0, 3.0);
glVertex3f(-3.0, -1.0, -3.0);
glVertex3f(3.0, -1.0, -3.0);
glVertex3f(3.0, -1.0, 3.0);
glEnd();
}
void drawBalls()
{
glTranslatef(-3.0, -.5, -3.0);
glColor3f(.8, .1, .1);
for(int i = 0; i < 3; i++)
{
glPushMatrix();
glTranslatef(.0, -.5, i * 3);
for(int j = 0; j < 3; j++)
{
glPushMatrix();
glTranslatef(j * 3, .0, .0);
glutSolidSphere(.5, 20, 20);
glPopMatrix();
}
glPopMatrix();
}
}
void keyPressed(unsigned char key, int x, int y)
{
keyStates[key] = true;
}
void keyReleased(unsigned char key, int x, int y)
{
keyStates[key] = false;
}
void keyboardOperations()
{
if(keyStates['w'])
z_offset += roaming_step;
if(keyStates['s'])
z_offset -= roaming_step;
if(keyStates['a'])
x_offset += roaming_step;
if(keyStates['d'])
x_offset -= roaming_step;
if(keyStates['i'])
{
angle_xz -= angle_step;
if(angle_xz < .0)
angle_xz += 360.0;
}
if(keyStates['o'])
{
angle_xz += angle_step;
if(angle_xz >= 360.0)
angle_xz -= 360.0;
}
if(keyStates['u'])
{
angle_yz -= angle_step;
if(angle_yz < .0)
angle_yz += 360.0;
}
if(keyStates['j'])
{
angle_yz += angle_step;
if(angle_yz >= 360.0)
angle_yz -= 360.0;
}
if(keyStates['q'])
exit(0);
}
// I guess it has to be done in this function
// but didn't get how
void camera()
{
glLoadIdentity();
// Forward / Backward
glTranslated(.0, .0, z_offset);
// Left / Right
glTranslated(x_offset, .0, .0);
// XZ Rotation
glRotated(angle_xz, .0, 1.0, .0);
// YZ Rotation
glRotated(angle_yz, 1.0, .0, .0);
}
void display(void)
{
keyboardOperations();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera();
drawFloor();
drawBalls();
glutSwapBuffers();
}
void reshape(int width, int height)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, width, height);
GLdouble aspect = (GLdouble) width / (GLdouble) height;
gluPerspective(60, aspect, 1, 100);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("openGLtest3");
glClearColor(.0, .0, .0, .0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutIgnoreKeyRepeat(true);
glutKeyboardFunc(keyPressed);
glutKeyboardUpFunc(keyReleased);
glutMainLoop();
return 0;
}
In openGL, glRotation fuction takes origin as a reference. In order to rotate around a point (your camera coordinate in this case) you should translate your camera position to the origin (Translate all your objects accordingly) and then apply rotation function.And then you can translate your camera back (with all your objects)
lets say your camera position is (a,b,c) so your code should be something like :
foreach object
{
glPushMatrix();
glTranslate(a,b,c);
glRotate(...);
glTranslate(-a,-b,-c);
//render
glPopMatrix();
}

The object flips after 180 degree of rotation

I m new to OpenGL. I have a viewer for a point cloud.
When I rotate the point cloud around x-axis or z-axis , it gets flipped or inverted after 180 degrees and flips back again at 0 degrees.
From 0-180 degrees it works fine and from 180-360 degrees it flips.
The following is a code snippet for the paint event
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_center[0] = (m_minX+m_maxX)/2.0f;
m_center[1] = (m_minY+m_maxY)/2.0f;
m_center[2] = (m_minZ+m_maxZ)/2.0f;
m_radius = static_cast<float>(std::sqrt((m_maxX-m_center[0])*(m_maxX-m_center[0])
+(m_maxY-m_center[1])*(m_maxY-m_center[1])+(m_maxZ-m_center[2])*(m_maxZ-m_center[2])));
m_fDistance = m_radius/0.57735f; //where 0.57735f is tan(30 degrees)
m_dNear = m_fDistance - 3*m_radius;
m_dFar = m_fDistance + 3*m_radius;
glLoadIdentity();
glPushMatrix();
// glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-m_zoomFactor*2*m_radius, +m_zoomFactor*2*m_radius, -m_zoomFactor*2*m_radius, +m_zoomFactor*2*m_radius, m_dNear, m_dFar);
glMatrixMode(GL_MODELVIEW);
glRotatef(m_modelRotation.x/16.0, 1.0, 0.0, 0.0);
glRotatef(m_modelRotation.y/16.0, 0.0, 1.0, 0.0);
glRotatef(m_modelRotation.z/16.0, 0.0, 0.0, 1.0);
glScalef(m_modelScale.x,m_modelScale.y,m_modelScale.z);
switch(m_shapeMode)
{
case eShapePointCloud:
drawPoints();
break;
case eShapeSolid:
drawQuads();
break;
default:
qDebug()<<"No shape is specified, so use the points shape.";
drawPoints();
break;
}
glPopMatrix();
Here is the code for the mouse move event
void OglWidget::mouseMoveEvent(QMouseEvent *event)
{
GLfloat dx = GLfloat(event->x() - m_lastPos.x()) ;/// width();
GLfloat dy = GLfloat(event->y() - m_lastPos.y()) ;/// height();
if (event->buttons() & Qt::LeftButton)
{
setXRotation(m_modelRotation.x + 8*dy);
setZRotation(m_modelRotation.z + 8*dx);
this->setPropertyValue(1,m_modelRotation);
}
m_lastPos = event->pos();
}

Move a ball in bowling game once a key is pressed

I have been working with bowling game in C++.
I want only one thing that I want to move the ball only once a key is pressed and the bowl moves smoothly(not like now as it moves by pressing and holding down UP-KEY).
Here is the code: -
#include <GL/glut.h>
#include <cmath>
GLfloat posX = 0.07, posY = 0.1, posZ = 0.0,
firstx1 = 0.02, firsty1 = 0.3, firstx2 = 0.07, firsty2 = 0.3, firstx3 = 0.11, firsty3 = 0.3,
secondx1 = -0.16, secondy1 = 0.3, secondx2 = -0.21, secondy2 = 0.3, secondx3 = -0.27, secondy3 = 0.3,
thirdx1 = 0.3, thirdy1 = 0.3, thirdx2 = 0.35, thirdy2 = 0.3, thirdx3 = 0.4, thirdy3 = 0.3;
double x, y, angle;
#define PI 3.1415926535898
GLint circle_points = 50;
void bottle() {
glColor3f(0.0, 0.0, 1.0);
glPointSize(9.0);
glBegin(GL_POINTS);
glVertex3f(firstx1, firsty1, 0.0);
glVertex3f(firstx2, firsty2, 0.0);
glVertex3f(firstx3, firsty3, 0.0);
glVertex3f(secondx1, secondy1, 0.0);
glVertex3f(secondx2, secondy2, 0.0);
glVertex3f(secondx3, secondy3, 0.0);
glVertex3f(thirdx1, thirdy1, 0.0);
glVertex3f(thirdx2, thirdy2, 0.0);
glVertex3f(thirdx3, thirdy3, 0.0);
glEnd();
glFlush();
}
void circ() {
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= 300; i++) {
angle = 2 * PI * i / 300;
x = cos(angle) / 25;
y = sin(angle) / 20;
glVertex2d(x, y);
}
glEnd();
}
void display() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
bottle();
glPopMatrix();
glPushMatrix();
glTranslatef(posX, posY, posZ);
circ();
glPopMatrix();
glutSwapBuffers();
}
float move_unit = 0.01;
void keyboardown(int key, int x, int y) {
switch (key) {
case GLUT_KEY_RIGHT:
posX += 0.3;
break;
case GLUT_KEY_LEFT:
posX -= 0.3;
break;
case GLUT_KEY_UP:
posY += move_unit;
break;
case GLUT_KEY_DOWN:
posY -= move_unit;
break;
default:
break;
}
if ((posX >= firstx1 && posX <= firstx3)
&& (posY == firsty1 && posY == firsty2 && posY == firsty3)) {
firstx1 += 0.02;
firsty1 += 0.03;
firstx2 += -0.06;
firsty2 += 0.02;
firstx3 += 0.03;
firsty3 += 0.05;
}
if ((posX <= secondx1 && posX >= secondx3)
&& (posY == secondy1 && posY == secondy2 && posY == secondy3)) {
secondx1 += 0.02;
secondy1 += 0.02;
secondx2 += -0.06;
secondy2 += 0.02;
secondx3 += 0.03;
secondy3 += 0.05;
}
if ((posX >= thirdx1 && posX <= thirdx3)
&& (posY == thirdy1 && posY == thirdy2 && posY == thirdy3)) {
thirdx1 += 0.02;
thirdy1 += 0.03;
thirdx2 += -0.07;
thirdy2 += 0.02;
thirdx3 += 0.03;
thirdy3 += 0.05;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 300);
glutInitWindowPosition(150,250);
glutCreateWindow("Balling Game");
glutDisplayFunc(display);
glutSpecialFunc(keyboardown);
glutMainLoop();
}
You are relying on the keyboard listener thread to do your work for you. You will need to make a position update thread, and cache the last movement command you received. The update thread will need to run regularly (e.g. 60 times a second) and update the current position of the ball every time.
The best way to do that would be to make your program more object oriented. Then you would have a "draw" thread, which simply looked at the state of the objects in your scene. It would ask the Ball objects for their current position. The Ball would know where it is based on its velocity (direction plus speed), the time elapsed since the last change in velocity, and the position at which the last change in velocity occurred.
For a first step though just store the last direction pressed and update the position at regular intervals.