Phaser 3 : sprite movement like Endless Sky NPC - game-engine

I'm trying to replicate the NPC movement in Endless Sky. Checkout Endless Sky Github's Repository using Phaser 3.
The characteristic of the NPC is that they have drag and rotate to face the direction they intend to travel. I can do this with the player character using
player.body.setAngularVelocity(0);
player.body.setAcceleration(0)
scene.physics.velocityFromAngle(player.angle - 90, 400, player.body.acceleration)
But I can't seem to get it right with an npc - and by "get it right" I mean a smooth movement of the sprite turning/tweening to face the new coordinates and then moving towards it.
The closest I had was the sprite seemed to be moving correctly but the image/texture of the sprite was frantically changing angle, sometimes doing a 360 rotation. Perhaps I have a fundamental misunderstanding of the body.angle / body.rotation as well as the math involved?
At the moment, I am passing the npc a random vector to travel to:
enemy.stateMachine.transition('patrolling', [Phaser.Math.Between(-3500, 3500), Phaser.Math.Between(-3500, 3000)])
-3500 and 3000 being the bounds of my game world.
Any help appreciated.

Related

Godot 3d About Velocity

I have 3d ball game. The ball continues to go forward.When the left or right rotation key is pressed, it moves in that direction as much as the pressed amount. Ball kinematicbody and using velocity. Everything works perfectly when the road is straight
I am confused what should I change about velocity when switching from straight to curve path.Any help is appreciated
Looking for help with vectors and velocity

Getting direction of object moved by the physics engine

i am using spritekit to learn about physics behaviour in games, when a sprite node ball bounces off a surface, the physics engine will automatically calculate its new direction.
how do i get this new direction in code?
i am using swift, xcode spritekit. but object c spritekit (cocos2d) is fine too
(the aim is to get a fire trail behind the bouncing ball, but i dont know how to rotate the particle emitter based on ball's vector of movement. the emitter is currently added as a child of the ball node)
To get the "direction" you should read the velocity property of the physicsBody of your node.
To rotate your particle emitter, you should modify the zRotation property of the node.
Well, the most best idea for getting a rotation of a node is getting eulerAngles. But since it showed radians, I converted to degrees. This is the code that I got when getting the rotation of a camera Node
print(round(GLKMathRadiansToDegrees(Float(gameController.camera.eulerAngles.z))))
Hope This helps

Processing camera frustum() and perspective() rotations

How can I make a camera that I can move around and rotate around its own axis in processing 2+?
I have a camera that I can move around in the world space and have some kind of rotation:
frustum(-10,10,-10,10,10,2000);
translate(camX,camY,camZ);//I move around by adding to these values when a button is pressed
rotate(angleX,1,0,0);//same here...
rotate(angleY,0,1,0);
rotate(angleZ,0,0,1);
Bu the problem with this is that the rotation is centered in the scene, meaning that I get very strange rotations when moving further away from the scene's center coordinates. Why does that happen when I have translated before rotating?
Thanks to Nicolás Carlo and his suggestion to watch This Youtube playlist made by Jorge Rodriguez I was able to fix what I almost made right the first time.
All I had to do was really just to do some simple trigonometric calculations to get the forward-vector from the angles I got, and then just add the camera position to that for the centerX,centerY,centerZ values in camera();
Ex. where camPos is the current position 3D-Vector and vecForward is the calculated 3D-forward vector that I needed.
vecForward.x = cos(yaw)*cos(pitch);
vecForward.y = sin(pitch);
vecForward.z = sin(yaw)*cos(pitch);
camera(camPos.x,camPos.y,camPos.z, camPos.x+vecForward.x,camPos.y+vecForward.y,camPos.z+vecForward.z, 0,1,0);
If some of you do not know, Pitch is the up-down angle and Yaw is left-right angle of the camera.
And as a last note here,I highly suggest watching Rodriguez "Math for game developers" that Carlo suggested since every video explains at least one of the most important/oftenly used mathematical solutions at a time to different problems and then giving an example in the end.

Three.js: Camera Collision on Terrain

How should I go about adding camera collision to a terrain in three.js.
The terrain is from 'mrdoob's three.js' examples and is randomly generated and I am currently converting it to height map.
I am thinking of implementing the collision as follows:
Create a 'box' object around the camera
If the box object is not touching the terrain, move the camera down.
If the box object IS touching the terrain, keep the Y axis of the camera.
How should I go about doing this?
The theory is that you send a ray from the location you are (camera position) straight down. You find the intersection point and based on the distance you decide what to do. Implementation wise I cannot help you but THREE.Ray should help you.

Rotate sprite opposite of phone rotation with cocos2d?

I am trying to rotate a sprite exactly the opposite of phone rotation so that the sprite stays upright relative to the ground, regardless of the position of the phone.
It works for the most part, but the variation in accelerometer readings, no matter how still the phone is, makes the sprite "bouncy". Basically my code is very simple - I just multiply the acceleration reading by -90 whenever I do an accelerometer reading:
_pink.rotation = acceleration.y * (-90);
This works, but even with the phone sitting on a tablet, it bounces back and forth due to inconsistent accelerometer readings. How can I make it smooth? I am aware of KFilteringFactor, which I implemented but it just made the movement slower, so it didnt keep up with the opposite of phone movement. Maybe I was using it wrong.
Try this
float angle = angle-90;
[pink setRotation:angle];