PDA

View Full Version : Basic Movement on the Screen



Arkeshen
10-14-2011, 10:13 AM
I just started with openGL and I'm just trying to get a Rectangle to move back and forth on the screen whenever the 'a' & 'd' keys are pressed. I know that I need a Switch Statement but I don't know how to get the movement to happen.

If anyone could help me learn this, that would be appreciated.

Arkeshen
10-14-2011, 03:55 PM
I don't know the rules on "Bumping" in this forum, But I realy could use some help. I have been surfing the web for info about this, but all I have found for OpenGL is 3d this, 3d that, nothing about just moving something back and forth.

Alfonse Reinheart
10-14-2011, 04:36 PM
Well, here's a rule that applies on any forum:

Don't bump something you posted less than six hours after your post. At least give everyone a day.

Arkeshen
10-14-2011, 06:10 PM
I don't mean to sound rude, but since you're are inclined to to tell me an unwritten forum rule, Can you instead help me with my question?

Alfonse Reinheart
10-14-2011, 09:51 PM
Well, I was going to, but then you decided to bump your question six hours after you posted it. However, I can tell you why you haven't gotten an answer.

You haven't told us what you have.

In order to be able to do what you want, you need to do the following things in order:

1: Render a rectangle successfully to the screen.

2: Be able to position that rectangle arbitrarily.

3: Retrieve keyboard input successfully.

So where are you in these steps? Can you draw a rectangle? Do you have all of these down, but you don't know how to put them all together?

Let's assume that you've passed 1&2 and are stuck on step 3.

OpenGL is a rendering API. It doesn't handle input; that simply isn't its job. Therefore, what are you trying to use to get input? Are you using FreeGLUT to create your window and want to know how to get input with it? Are you using GLFW or something else?

In short, your question isn't being answered because you haven't provided us enough information to know what we're being asked to give you.


since you're are inclined to to tell me an unwritten forum rule

It's not a forum rule; it's simple courtesy. Someone who went to sleep just as you made your post hasn't even woken up yet. They don't need to see you bumping a question they haven't even seen yet, demanding faster answers to something that they cannot possibly have answered yet.

I know you feel your problem is urgent, but we are people, not a magical answer box.

Arkeshen
10-15-2011, 07:16 AM
I can draw/Render a rectangle to the screen with the glutrectf, I also understand how to position it properly, as well as keyboard input with switch statements. But I don't understand how to implement those statement to achieve movement.

As per the next question, to the best of my knowledge, I am Using FreeGLUT.

biller23
10-15-2011, 11:59 AM
To achieve a "movement", you need to add an offset value to the current position. Suppose that the object is in the point 'p' = (x, y), and the change of position is 'offset' = (-2, 0), then the new position p' is:
p' = p + offset

Arkeshen
10-15-2011, 02:44 PM
I'm sure if that anyone else in my shoes could understand that, but I'm just not getting it. I'm sure that is a good answer, but I don't understand. When it comes to code, I only understand what I read, Not what I write. Is there a simpler explanation that you can provide?

Alfonse Reinheart
10-15-2011, 03:52 PM
There isn't a simpler possible explanation outside of writing it for you.

You can position a rectangle at an arbitrary location. So, you make that location come from a variable instead of being a hard-coded number. Your key-detection code then detects that the key is pressed and changes the variable appropriately.

MaxH
10-15-2011, 08:16 PM
I can draw/Render a rectangle to the screen with the glutrectf, I also understand how to position it properly, as well as keyboard input with switch statements. But I don't understand how to implement those statement to achieve movement.
If you really can do everything above, you are very close to solving your problem.
I can outline a simple way to move your rectangle. Then you can elaborate on it.
Declare variables x1 and x2 to be global, initializing them to draw the rectangle in its starting position.
Something like below. dx is the amount the rectangle is going to move every time you push the 'x' key.

float x1 = -3.0, x2 = 4.0, dx = 0.1;
Apparently you've set up a routine to handle keyboard inputs using a switch statement.
Add a case to the switch statement like:

case 'x' : x1 = x1 + dx; x2 = x2 + dx; break;
In your display function you'll have something like:

glRectf (x1, 1.0, x2, 5.0)
Anytime you push the 'x' key the rectangle will be redrawn 0.1 units to the right.
You can easily set up other keys to move it to the left, or up and down.
I also like to have reset keys which put my objects back in their original positions.
Have a look at the first few NeHe Tutorials.
They show you how to rotate things instead of translating them.

Good luck.