Sign in to follow this  

Class design

This topic is 3736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm using C++, ODE, and Glut for an AI project I'm working on. However, I'm having a bit of trouble deciding how to properly set up my classes. I have a camera class that I've successfully implemented. It's behaving as I would like it. However, I'm having trouble trying to implement an input class. At the moment, I have some code just hacked together to give me simple input. For example, I have the Camera.Move() function within a MouseCallback() function (which is in the main.cpp file):
void MouseMovement(int x, int y)
{

	if (LeftMouseButton == true)
	{
		// do something
	}

	if (RightMouseButton==true)
	{
		// do something
	}

}
The problem is that this function is not tied in with my main game loop. I'll need to explicitly call a function within the main look if I want consistent data on my mouse and keyboard state. Does anyone know the proper method for implementing input? My guess is that I should create a class for each different type of input (mouse, keyboard, controller, wacom). Then manipulate class variables when an input callback function is triggered. Then within each class that needs to know the mouse and keyboard data, use that data as needed. For example

Player::move {
  if (Mouse->RightKeyPressed == True) {
    Player.x_pos++; //or something like that
  }
}
Somehow this just feels "wrong" though. It doesn't feel abstracted enough. It seems odd that a player class would be directly addressing the mouse class. Maybe I'm just thinking about it too much. Thanks for the help.

Share this post


Link to post
Share on other sites
Quote:
Original post by decepto
Somehow this just feels "wrong" though. It doesn't feel abstracted enough. It seems odd that a player class would be directly addressing the mouse class. Maybe I'm just thinking about it too much.

That does "feel wrong" to me too ;)

I would take the "raw input" (e.g. Key 'W' down / Right mouse click) and translate that into "application-specific input" (e.g. Move foward please), then give the app-specific input to the player class.

This way the player doesn't have to communicate directly with the input devies, it just gets told what the user's intention is. Another abstraction elsewhere deals with translating device-specific (raw) input into app-specific input.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
Quote:
Original post by decepto
Somehow this just feels "wrong" though. It doesn't feel abstracted enough. It seems odd that a player class would be directly addressing the mouse class. Maybe I'm just thinking about it too much.

That does "feel wrong" to me too ;)

I would take the "raw input" (e.g. Key 'W' down / Right mouse click) and translate that into "application-specific input" (e.g. Move foward please), then give the app-specific input to the player class.

This way the player doesn't have to communicate directly with the input devies, it just gets told what the user's intention is. Another abstraction elsewhere deals with translating device-specific (raw) input into app-specific input.


What's nice about that is it makes it fairly easy to record player movement as you don't record the input just the corresponding actions. Also, this makes it easy to script the player movement if you want to create a reproducible demonstration.

Share this post


Link to post
Share on other sites

Hi,

As stated earlier, it doesn't feel right to let your camera class or player class to worry about specific things such as mouse, keyboard or joystick etc.

I'd create a certain InputEvent class, which would be used instead of reading mouse or keyboard directly.


class InputEvent
{
int EventID; //such as MOVE_FORWARD
bool KeyPressed; //if the key was pressed or released
float Value; //used by mouse for example to turn
}




Using this involves few steps in the game:

- setup : binding input events to actions

- reading data from input devices
- translating the input to actions according to the bindings
- pass the translated actions to the game

This makes is really easy to change the key bindings (from external file for example). Also, it hides/virtualized the source of the input.

You'll need to do some work with the input devices so that every key on keyboard and every mouse button, mouse movement etc create a different input event.

Hope this was helpful.

Share this post


Link to post
Share on other sites
Sign in to follow this