I'm trying to implement keyboard controls to move a sphere(Player) with respondtokeypress.
Currently, when I press any key my character will move to the right by 0.1. This might be a noobie question but how can I move my character with w(up),a(left),s(down),d(right) in their respective directions using respondtokeypress?
Code:
class Player
{
private:
double x, y;
public:
Player(double a, double b){x=a;y=b;}
void respondtokeypress(char a)
{
a +=0.1;
a -=0.1;
b +=0.1;
b -=0.1;
glutPostRedisplay();
}
void draw()
{
//draw player
glLoadIdentity();
glColor3f (1.0, 0.0, 0.0);
glTranslatef(x,y,0);
glutSolidSphere(0.03,20,20);
}
};
void keyboard(unsigned char key, int x, int y)
{
Theseus.respondtokeypress(key);
glutPostRedisplay();
}
The code you posted will not compile, but at the same time you say that your object moves when you press a key. This does not make sense.
Also, the code you posted makes me think you don't understand the very basics of C++ programming (or any other programming language for that matter). I recommend you read a book or go through some tutorials.
The code you posted will not compile, but at the same time you say that your object moves when you press a key. This does not make sense.
Also, the code you posted makes me think you don't understand the very basics of C++ programming (or any other programming language for that matter). I recommend you read a book or go through some tutorials.
Regards
The code I posted is everything to do with keyboard inputs, I didn't think the rest mattered.
I do understand c++ basics but, I'm having a hard time figuring out how to use respondtokeypress correctly.
If anything could you explain how to use respondtokeypress with moving an object in 2d?
If it helps I have attached my coding.
void respondtokeypress(char a)
{
a +=0.1;
a -=0.1;
b +=0.1;
b -=0.1;
glutPostRedisplay();
}
What are you doing here in respect to a and b? a is char but you trying to add and subtract the same float number and there is no b declared anywhere within that method.
Assuming that respondtokeypress(char a) is called when a key is pressed and that a contains the char pressed, then the function could look something like this
Code:
void respondtokeypress(char a)
{
switch (a) {
//up
case 'w':
y -= 0.1;
break;
//down
case 's':
y += 0.1;
break;
//left
case 'a':
x -= 0.1;
break;
//right
case 'd':
x += 0.1;
break;
}
}
Depending upon the co-ordinate system, you might need to swap the-=/+= for left, right/up,down being the correct way.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
void respondtokeypress(char a)
{
a +=0.1;
a -=0.1;
b +=0.1;
b -=0.1;
glutPostRedisplay();
}
What are you doing here in respect to a and b? a is char but you trying to add and subtract the same float number and there is no b declared anywhere within that method.
Sorry for the confusion, I left out the Comments for:
Code:
void respondtokeypress(char a)
{
a +=0.1; // Left *Testing, to change when working*
//a -=0.1; //Right
//b +=0.1; //Up
//b -=0.1; //Down
glutPostRedisplay();
}
And I've made more errors, My understanding was that respondtokeypress(char a) was in fact the key that moved the character. This was indeed wrong as taking into consideration 2kaud's answer and Reading a book I know understand where I have gone wrong.
And I've made more errors, taking "2kaud's" answer I needed to implement a switch statement.
Originally Posted by 2kaud
Assuming that respondtokeypress(char a) is called when a key is pressed and that a contains the char pressed, then the function could look something like this
Code:
void respondtokeypress(char a)
{
switch (a) {
//up
case 'w':
y -= 0.1;
break;
//down
case 's':
y += 0.1;
break;
//left
case 'a':
x -= 0.1;
break;
//right
case 'd':
x += 0.1;
break;
}
}
Depending upon the co-ordinate system, you might need to swap the-=/+= for left, right/up,down being the correct way.
Thanks for this, it's helped me understand that I need a Switch Statement something which I won't forgot anytime soon .
And thanks to everyone for helping me I can be a right idiot at times
Last edited by MrBawzz; March 4th, 2014 at 01:19 PM.
Reason: Thanks
FYI: Instead of implementing a callback for glutKeyboardFunc, you could implement
a callback for glutSpecialFunc and catch the keys: GLUT_KEY_UP , GLUT_KEY_DOWN,
GLUT_KEY_RIGHT , and GLUT_KEY_LEFT.
The signature of the callback is slightly different. The first argument is of type int, instead
of unsigned char.
Thanks for this, it's helped me understand that I need a Switch Statement something which I won't forgot anytime soon
This illustrates another problem. Now our assistance has made you think a switch statement is something awesome, and you will surely try to use it soon again. In reality, the switch statement is probably the last statement that you would learn if you study C++, because it is rarely used, a common source of bugs, and was originally included in the language mostly for performance reasons, which I will not explain here.
Please understand that a programming language provides many ways of doing the same thing, and the bottom line is that you don't need a switch statement at all. What you instead need is basic understanding of C++, and you can demonstrate that you have acquired that by replacing the switch statement with the following and for each of them verify that it still works:
* if statements.
* if..else statements.
* while loops
* for loops
The latter two are not recommended solutions and the code may look ugly, but it is still a good way to verify your programming skill.
This illustrates another problem. Now our assistance has made you think a switch statement is something awesome, and you will surely try to use it soon again. In reality, the switch statement is probably the last statement that you would learn if you study C++, because it is rarely used, a common source of bugs, and was originally included in the language mostly for performance reasons, which I will not explain here.
Please understand that a programming language provides many ways of doing the same thing, and the bottom line is that you don't need a switch statement at all. What you instead need is basic understanding of C++, and you can demonstrate that you have acquired that by replacing the switch statement with the following and for each of them verify that it still works:
* if statements.
* if..else statements.
* while loops
* for loops
The latter two are not recommended solutions and the code may look ugly, but it is still a good way to verify your programming skill.
Regards
I understand that Switch Statements are not the answers to all my problems. But, for this type of problem and for the time being until I learn more about c++ a Switch statement will be adequate. In retrospect I think I need to learn more about c++ before posting my problems.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.