(OpenGL) Possible to achieve keyboard() functionality outside of keyboard()?
So, I have my keyboard function handling what happens during specific button presses, but I feel as though I can be more specific to which objects are effected by the button presses if I am somehow able to manipulate my objects in the same way, but from the checkKeyboard function. Essentially, this is what I am trying to emulate:
Code:
case 'e':
if (collided)
{
((Box*)gameObjects[1])->ChangeTexture();
treesTapped++;
points += 100;
break;
}
so I tried doing this in the checkKeyboard function:
Code:
else
{
collided = true;
if (b->alreadyCollided = false && keyDown['e']) //I believe that keyDown is just an bool array of size 255
{ //that gets ascii key presses, but I am using my professor's
treesTapped++; //project as a base for my final project, so I could be wrong.
points += 100; //I could post the entire class if that would help others help
b->ChangeTexture(); //me :)
b->alreadyCollided = true;
}
}
but it is worked less than I had hoped it would. Can someone maybe give me some guidance? Basically, I'm trying to make it so that whenever the player is colliding with the tree and they haven't collided with it before, the amount of trees tapped increments, the points accumulated is incremented by 100, the object's texture is changed, and the bool alreadyCollided is changed to true so that the player can't just get a large amount of points from one tree. This forum has already been so helpful in my journey through this project, and I am so grateful for everyone here. Thank you so much!!
Re: (OpenGL) Possible to achieve keyboard() functionality outside of keyboard()?
keyDown is an array of 255 elements of type bool indexed by the ASCII value of a character. When a key is pressed, keyboard() is entered which sets the required element of keyDown to true, and when key is released, keyboardUp() is entered which sets the required element of keydown to false. So the array keyDown determines which key(s) are down at any time.
Where in checkKeyboard() are you using this code? Where are you defining b? If this is from the suggested code in post #8 here http://forums.codeguru.com/showthrea...-for-quot-loop, then b is only available if the first if statement following the for is true for the block of the if statement.
It would be helpful if you posted all the code for the function checkKeyboard().
Re: (OpenGL) Possible to achieve keyboard() functionality outside of keyboard()?
Quote:
Originally Posted by
2kaud
keyDown is an array of 255 elements of type bool indexed by the ASCII value of a character. When a key is pressed, keyboard() is entered which sets the required element of keyDown to true, and when key is released, keyboardUp() is entered which sets the required element of keydown to false. So the array keyDown determines which key(s) are down at any time.
Where in checkKeyboard() are you using this code? Where are you defining b? If this is from the suggested code in post #8 here
http://forums.codeguru.com/showthrea...-for-quot-loop, then b is only available if the first if statement following the for is true for the block of the if statement.
It would be helpful if you posted all the code for the function checkKeyboard().
Here is my checkKeyboard():
Code:
void CheckKeyboard()
{
int o = 0;
if (keyDown['a'])
camera->RotateLeft();
if (keyDown['d'])
camera->RotateRight();
if (keyDown['w'])
camera->MoveForward();
if (keyDown['s'])
camera->MoveBackWard();
/*if (keyDown['e'])
camera->StrafeRight();
if (keyDown['q'])
camera->StrafeLeft();*/
vec3 p = camera->getPosition();
camera->Update();
for (auto i = gameObjects.begin(); i != gameObjects.end(); ++i)
{
if ((*i)->getType() == ObjectType::BOX)
{
auto b = (Box*)*i;
if (camera->getPosition().x >= (b->Position.x - b->getXBuffer()) && camera->getPosition().x <= (b->Position.x + b->getXBuffer()))
{
if (camera->getPosition().z >= (b->Position.z - b->getZBuffer()) && camera->getPosition().z <= (b->Position.z + b->getZBuffer())) //change to Z axis and get camera position instead of radius
{
if (b->collided1 == false) //Makes it so that boxes[0] does not have collision
{
collided = false;
}
else
{
collided = true;
if (b->alreadyCollided = false && keyDown['e'])
{
treesTapped++;
points += 100;
b->ChangeTexture();
b->alreadyCollided = true;
}
}
}
}
}
}
if (collided)
{
camera->position = p;
camera->Update();
}
if (camera->position.x >= 90.0f)
{
camera->position.x = 90.0f;
}
if (camera->position.x <= -90.0f)
{
camera->position.x = -90.0f;
}
if (camera->position.z >= 90.0f)
{
camera->position.z = 90.0f;
}
if (camera->position.z <= -90.0f)
{
camera->position.z = -90.0f;
}
}
Thank you again for helping me so much with this. I really, really appreciate it.
Re: (OpenGL) Possible to achieve keyboard() functionality outside of keyboard()?
Code:
if (b->alreadyCollided = false && keyDown['e'])
don't you mean
Code:
if (b->alreadyCollided == false && keyDown['e'])
Re: (OpenGL) Possible to achieve keyboard() functionality outside of keyboard()?
Quote:
Originally Posted by
2kaud
Code:
if (b->alreadyCollided = false && keyDown['e'])
don't you mean
Code:
if (b->alreadyCollided == false && keyDown['e'])
Wow, I feel so silly. It really is the little things, isn't it? Thank you so much.