Object3d lookAt and Vector3 apply Euler methods threejs example
For todays post on a threejs project example I wanted to make some quick code that has to do with how the look at method of the object3d class is not always a kind of golden hammer kind of solution when it comes to setting the orientation of an object. For the most part the look at method works well, and is very easy to use. I just call the method off of the object that is based off of the object3d class, typically a camera or mesh object, and pass the position that I want the object to look at. The position can be given in the form of a Vector3 object, or a set of number literal values, and more often than not the method will in fact have the object look at the point in space. However in some cases the end result may not work the way that I want it to, so I can not just turn my brain off and not think about this kind of stuff at all. In certain situations I will need to work out some other kind of situation when it comes to setting the rotation of an object3d based object.
This example then should help to show what I mean by this as it is a whole bunch of objects positioned around the origin and the are all made to look at the origin with the look at method. However two objects of interest might be at the top of the over all area where one plain is positioned one way and the other is flipped around completely. This is not always such a bad thing, but it is if what I want to do is make an animation in which this kind of object is of that of a plane that might make a motion like that of a loop in space. The end result is that the plane will flip have way threw which is of course not at all how it would happen in reality.
This threejs example of the lookAt, and applyEuler methods in threejs and what to know first
There is reading more on the Object3d look at method and object3d class in general
I have wrote posts on the Object3d lookAt method as well as the object3d class in general. I use the look at method all the time in just about all of my source code examples when it comes to setting the rotation of a camera, however it can also be used with objects in general beyond just that of cameras. In this example I am using the look at method to set to orientation of mesh objects which like cameras are also based on the object3d class.
Read up more on the apply Euler method of Vector3 as well as Vector3 in general.
In this example I am not just using the look at method, but also the apply Euler method of the Vector3 class along with the Euler class to set the position of mesh objects. With that said I have wrote a post on the apply Euler method itself, as well as another on the Vector3 class in general. When it comes to using the apply Euler method I need to have a Vector3 instance with a value other than 0,0,0 and when calling the method I need to pass an instance of the Euler class which is yet even another class that is worth looking into further.
Source code is on github
The source code for this example can be found in my test threejs repository. This is also where I park the source code examples that I write about in my many other posts on threejs.
Version Numbers matter
When I first made this example I was using r135 of threejs, and the last time I came around to do some editing of this post I was using r146.
1 - airplane.js r0, airplane-wrap.js r0, and demos
In this section I am making use of an airplane module that I use to create a model of a kind of airplane type object. I then have another module that I can use to create a collection of these kinds of objects that I have pulled out into its one file apart from the airplane module. I am then making a few demos that make use of one or more of these files in an effort to come up with some kind of system for showing what the problem is to begin with, and also working out a solution for the problem.
The airplane module r0
There is showing what the problem is, and figuring out how to deal with the problem. However before I can start to do any of that I will want to have a way to create some kind of object that does not symmetrical. That is that I will want to have some kind of object where it is clear what side is the top, bottom, and front at least. So I came up with some quick code that will be used to create this kind of object.
Nothing fancy with this module, just a create method that when called will create a group with a bunch of nested mesh objects as children.
The airplane wrap module r0
This is a module that I am using to create a collection of these airplane objects, and then use the apply Euler method to position the objects around the origin. I am then also using the object3d look at method to have all of them face the origin as well.
1.1 - Demo of airplane wrap
I then have this one demo of the airplane module, and the wrap collection module that does a good job of showing what the problem with the object3d look at method is. Although the method works well there is one little problem with it where I will not be able to just one the look at method along to set the rotation of the objects. As you can see the airplanes will all of a sudden flip over in a way that I do not want them to for the kind of situations in which I would use this airplane module, or any kind of object like it for that matter.
So now that I have a nice visual idea of what the problem is to begin with it is now a matter to work out at least one or more solutions to help with the process of addressing this kind of problem that has to do with setting position and rotation of objects.
1.2 - Demo using a new position and rotation helpers
With what it is that I have worked out this far it is clear to me that there is not much of anything wrong with what I am doing to set the position of these airplane objects. For the most part the process of using the vector3 apply Euler method works well for setting a local position of an object in space. The main problem here is how to go about setting the rotation of the objects rather than the position. I really do like the object3d look at method, it does very much make the process of setting rotation very easy most of the time. However there are going to be situations in which I am just going to have to work something else out and this is a good example of that kind of situation.
This example helped me get a good visual idea of what the deal is with the use of the look at method and how it sets rotation for objects. The result is that the look at method will work just fine for most situations in which I would want to use it, but there are a few use cases in which it will now work the way that I would want it to. For example using it to set the rotation of a biplane model that might do a loop or two in a scene, when it comes to that I will need to do a bit more than just use the look at method, for find some other solution completely actually.