Mugen87 / yuka

JavaScript library for developing Game AI.
https://mugen87.github.io/yuka/
MIT License
1.13k stars 91 forks source link

Orientation/up-vector of YUKA-vehicle, ideas for a better solution? #33

Closed Cghost96 closed 4 years ago

Cghost96 commented 4 years ago

Hi,

I wanted to hear if someone have any ideas for a better solution than the one i’ve come up with.

Background of the problem:

I’m making a 3D-simulator for a company. Basically it’s a water-pool and in the pool there will be robots that can either follow a 2D preprogrammed route/path or the robots can be controlled like an ROV in 3D-space. I’m planning to use THREE.js combined with YUKA.js for the simulator and so far it looks very promising.

Illustration of the pool with drawn world space axes and a preprogrammed route/path (on “Floor 1”): image

Illustration of two of the robots with AxesHelper and ArrowHelper added to them: image

Explanation of problem:

The robots are YUKA Vehicles and I’m using FollowPathBehaviour and OnPathBehaviour. When the robot follows a path on the floor (i.e. when the y-axis of the robot and the world y-axis points in the same direction and the robot.up-vector is positive y) there’s no problem; the robot rotates/orients itself as it should:

image

The problem occurs when I want the robot to follow a 2D-path on one of the pool walls. I want the robot y-axis to always be orthogonal to the surface it’s on and to point inwards in the pool. To do that I set the robot.up-vector to x, y or z based on what surface in the pool it is running on. Here I’ve created a 2D-path on “Outlet 1” and set the up-vector to positive x. As you can see the robot “flips” after the turn to make sure the x-axis always points in positive world y-axis, I want to avoid this flipping.

image

My solution:

The solution I’ve come up with is to check the rotation of the robot in the render-loop and change the up-vector based on that. It works but I wanted to check if someone have any idea for a better solution? Is there any way to “trick” the robot into thinking that the world x-axis or z-axis is up instead of world-y, and then always let the up-vector be positive y locally?

Mugen87 commented 4 years ago

Well, the up vector exists for exact this purpose (meaning to configure the game entity's local coordinate system). So I think you are doing it correctly.

Instead of checking the rotation of the robot in the animation loop, there might be the option to do this on certain events. However, this of course depends on your application.