0
votes

I'm trying to build an Input Handler in Coco2D-x for a 3D game. I want to control the camera rotation using the mouse, like an FPS. To achieve this, I need to store the previous and current mouse position every frame to calculate the delta movement per frame and apply it as a rotation.

The only way that the documentation shows to get the mouse info, is through Mouse Events and the Event Dispatcher. I tried that, but it is not useful for the position because, that way, I will only refresh the previous and current position when the mouse moves. if the mouse doesn't move, I still have some delta movement (calculated N frames ago, when the mouse moved the last time) and the camera will move forever.

Is there any way to get the position of the mouse every frame? I'm also open to any suggestions on how do the camera movement with the mouse.

I'm using Cocos2d-x 3.13.1 and Visual Studio 2015 on Windows 10. The project is on C++.

Thanks.

2

2 Answers

0
votes

I am not an expert on Cocos2D, but from this:

The only way that the documentation shows to get the mouse info, is through Mouse Events and the Event Dispatcher. I tried that, but it is not useful for the position because, that way, I will only refresh the previous and current position when the mouse moves. if the mouse doesn't move, I still have some delta movement (calculated N frames ago, when the mouse moved the last time) and the camera will move forever.

I think that resetting the deltas to 0 after the camera rotation calculations will solve your problem using these events.

0
votes

I found a solution, maybe not the best, but the easiest one.

InputHandler is still in control of the mouse events, and it only stores the current position of the mouse. This position is updated only when the mouse is moved.

CameraController is asking, every frame, for the current position of the mouse. Before doing this, it's storing the previous one. That way, I can control if the mouse moves or not.

So basically I calculate the delta wherever is necessary (camera controller in this case) instead of calculate it every frame (as I intended with the Input Controller).

This is within the update function in CameraController

    mousePreviousPos = mouseCurrentPos;
    mouseCurrentPos = inputHandler->GetMousePos();
    mouseDelta = mousePreviousPos - mouseCurrentPos;

    if (mouseDelta.x != 0)
    {
        ApplyRotationAroundY(mouseDelta.x);
    }
    if (mouseDelta.y != 0)
    {
        ApplyRotationAroundX(mouseDelta.y);
    }

I will stay tunned for more or better solutions. Thanks!