fenomas / noa

Experimental voxel game engine.
MIT License
611 stars 87 forks source link

How do I set the rotation of the playerEntity? #149

Closed Trenki closed 3 years ago

Trenki commented 3 years ago

I want to change the rotation of playerEntity. Can you give me some suggestions?Thank you very much.

playerEntity

fenomas commented 3 years ago

Hi, are you wanting to rotate the mesh associated with the entity? Or to rotate which direction the entity moves?

If the former, then for any entity with the mesh component, you can get that mesh and change its rotation by setting the necessary babylon.js properties. E.g.:

var meshData = noa.entities.getMeshData(noa.playerEntity)
meshData.mesh.rotation.set(0, yRotation, 0)

If you want to change the player entity's movement direction, the heading property of the movement component tracks that. By default the receivesInput component updates this property each tick, and then the movement physics uses it when deciding how to move the entity.

Trenki commented 3 years ago

Thank you very much, Andy. That was really useful.At the same time, I also wanted to find the mesh of the camera so as to change the Angle of the camera, but I failed.

fenomas commented 3 years ago

The easiest way to get the camera heading is to read noa.camera.heading (radians). So, e.g. a minimal way to make the player entity mesh follow the camera would be:

noa.on('beforeRender', () => {
    noa.ents.getMeshData(noa.playerEntity).mesh.rotation.y = noa.camera.heading
})
Trenki commented 3 years ago

I didn't make myself clear.I want to use the following code to remove the camera from following Player, and then set the camera properties separately, as in Babylon.

// make cameraTarget stop following the player
noa.ents.removeComponent(noa.camera.cameraTarget, 'followsEntity')
// control cameraTarget position directly (or whatever..)
noa.ents.setPosition(noa.camera.cameraTarget, [x,y,z])
fenomas commented 3 years ago

Sorry, I'm pretty lost as to what you want to do here. If you want to make the camera stop following the player entity and control it directly, the code you posted should work for setting the position:

noa.ents.removeComponent(noa.camera.cameraTarget, 'followsEntity')
noa.ents.setPosition(noa.camera.cameraTarget, 5, 5, 5)

Do you mean that you also want to directly control the camera's rotation? If so, you can do that via noa APIs:

noa.camera.heading = 0.5
noa.camera.pitch = 0.2

However the engine will automatically apply mouse inputs to the camera rotation, and there's currently no API to disable that. You could work around it by setting the sensitivity to 0, which will effectively prevent the mouse from affecting the camera, and then undo it later...

// disables built-in mouse control of camera
noa.camera.sensitivityX = noa.camera.sensitivityY = 0

If you just want to bypass everything and get a reference to babylon's camera object, then you're looking for noa.rendering._camera. But you may find that directly updating the camera leads to e.g. temporal aliasing, or other issues that are difficult to fix.

Trenki commented 3 years ago

noa.camera.heading and noa.camera.pitch is what I am looking for, thank you, my problem has been solved.