CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2016
    Posts
    12

    (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!!
    Last edited by Aideux; November 23rd, 2016 at 01:09 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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().
    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!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Nov 2016
    Posts
    12

    Re: (OpenGL) Possible to achieve keyboard() functionality outside of keyboard()?

    Quote Originally Posted by 2kaud View Post
    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.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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'])
    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!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Nov 2016
    Posts
    12

    Re: (OpenGL) Possible to achieve keyboard() functionality outside of keyboard()?

    Quote Originally Posted by 2kaud View Post
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured