Mouse/Camera Movement

Hi all,


I have searched for help on this matter but haven't had much luck. I am brand new to Scene Kit (and to Cocoa in general) and I'm starting out by trying to make a Minecraft-like voxel engine. This is purely for the learning experience.


In Minecraft, the mouse cursor is locked to the center of the screen. Moving the mouse doesn't actually move the cursor, but rotates the player and camera according to the mouse movement.


This is what I want to do as well, but I am struggling in figuring out how to detect mouse movements (with a locked mouse in the center) in a way that could allow camera/player rotation. Essentially, I want mouse movements to rotate the player horizontally (and the camera, being a child of the player node, should rotate with it). But vertically, only the camera rotates and not the player.


I feel tracking areas are not the answer, since the cursor doesn't actually move. Would I implement a mouseMoved event (assuming it gets called even with a locked cursor), and detect the difference in mouse positions? With that, I could create a direction vector and use that for rotation.


I have heard that using the mouseMoved handling isn't the most efficient approach and am wondering if there's a better way to do this.


Thanks,

Jason

I am seriously struggling with this. Does anyone have any hints at least that could lead me in the right direction? The closest I got was using NSEvent.mouseLocation(), and it wasn't very close at all. Furthermore, I realized it's not a viable approach because the cursor will be locked in the center...

I believe what you are trying to accomplish is to set a lookatconstraint and use the gimbalLock parameter


An

SCNLookAtConstraint
object automatically adjusts a node’s orientation so that it always points toward another node. For example, you can use a look-at constraint to ensure that a camera or spotlight always follows the movement of a game character. To attach constraints to an
SCNNode
object, use its
constraints
property.



Hope this heps


Codger

Hi there,


Thanks for the response! I'm still a bit confused. SCNLookAtConstraint requires a target node to look at. However, the camera's node should be at the player's head and should look outward based on mouse movement. In other words, this is a first-person view and mouse movement should allow the player node to rotate (with the camera) and look around.


I used the Unity engine when I was starting out. In Unity, every frame, I used code that looked like this for horizontal rotation (C#):


transform.Rotate(0, Input.GetAxis("Mouse X") * Settings.cameraSensitivity, 0);


And for vertical rotation:


rotationY += Input.GetAxis("Mouse Y") * Settings.cameraSensitivity;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);


I understand that transform.Rotate can be done in SceneKit by adjusting the rotation or eulerAngles property on the node.

GetAxis is a bit different. It returned a value between -1 and 1 based based on mouse movement (depending on horizontal or vertical movement). So when the mouse was moving, a value other than 0 filled that spot and allowed the rotation to change.


I figure I could do something like this by overrding mouseMoved() (though I haven't succeeded yet), but if this SCNLookAtConstraint could do this task in a more performant way then that would be better.


I looked at gimbalLock and it seems to be in the right direction for what I need, but I still don't have a target to look at and I don't know what exactly to use as this target.

Mouse/Camera Movement
 
 
Q