PDA

View Full Version : add circle object to keyEvent



12-14-2003, 02:01 PM
Hi i was just wondering how i would attach an object that has been translated scaled etc to the keyListener events, up,down keys etc, I understand the main concept and have implemented the keyListaener but how do i get it to know my objects position etc, basically its a circle at a start position and i shoulkd be able to move it around the screen using left, right keys etc.

//////////current part of code all working so far
///Implements KeyListener
public void keyPressed(KeyEvent e)
{
switch (e.getKeyCode())
{
case KeyEvent.VK_RIGHT:
moveX -=0.1f;
System.out.println(" value of moveX is " + moveX );
break;
case KeyEvent.VK_LEFT:
// ADD YOUR CODE HERE
break;
case KeyEvent.VK_UP:
moveX -= 0.1f;
System.out.println(" value of moveX is " + moveX );
break;
case KeyEvent.VK_DOWN:
// ADD YOUR CODE HERE
break;
case KeyEvent.VK_SPACE:
break;
}
}


///////Object Circle
/////CREATE CIRCLE OBJECT FOR SHIP FOR NOW
public void DrawCircle()
{

double radius;
radius = 1.5;
gl.glLineWidth(3);
gl.glBegin(GL_LINE_STRIP);
gl.glColor3f(0,0,1);

for(double a= 0 ; a <(2 * Math.PI) ; a+=(Math.PI/180))
{
double x = Math.sin(a) * radius;
double y = Math.cos(a) * radius;
gl.glVertex2d(x,y);
}
gl.glEnd();
}


/////////MAIN DRAWING ROUTINE
gl.glPushMatrix();
//CREATE GROUND OBJECT
gl.glPushMatrix();
gl.glTranslated(0.0, -3.5, 0.0);
DrawSine();
gl.glPopMatrix();
//END GROUND OBJECT

//CREATE SHIP OBJECTS START POSITION
gl.glPushMatrix ();
gl.glTranslated(-5.5,0, 0.0);
gl.glScaled(0.3,0.3,0.3);
gl.glRotated(0,90,0,0);
DrawCircle();
gl.glPopMatrix();

Any help is much appriciated.
Fred.

mikael_aronsson
12-14-2003, 11:19 PM
Hi !

the normal way to do it is to keep the movement/rotation in a variable and just before you render it you use:
glPushMatrix();
glTranslate/glScale/glRotate(...)

Then you render the object and call:
glPopMatrix();

Then you don't need to modify your object in any way.

Mikael

12-15-2003, 03:12 AM
Thanks very much, i had tried to do that but was implementing it in the break structure so it did not work, thanks very much!!!!