The days of the school are gone, whining. Say interview a few companies, really not ready on the, the result is of course small sad urge. Fortunately there is a place to live, even if it is to move bricks, it is possible to build their own buildings, roar.
OK, let me share an interesting question, say that he is interesting, because the meaning of this topic is very important, good not to sell the lawsuit, first look at the topic:
Three-dimensional space, y-axis upward, the gravitational acceleration of the size of G, known as a (x1, Y1, Z1), B (x2, y2, Z2) is a three-dimensional space in two points, now from a point to launch an object, hope that the object after the time t, fall at the B Point, please give the calculation of the emission velocity
With this in mind, please forget all about the unity-related API, because it's about physics and geometry, not the Unity API, the engine. (I interviewed the company, although recruit unity, but even a unity of knowledge did not ask Ah!) )
How do you do it? Before I think about the solution to the problem, let me first talk about the practical value of this purpose, which is absolutely useful when you are doing AI. Even if you do not write AI, take a simple example, Angry Birds, can achieve the "golden finger" function, give time, angle and strength are calculated,
how nice. For example, when you do a dynamic cut animation, for example, you need a shot to hit an object, if you write the shell accuracy is not high, then the scene animation is not finished (of course, there are other programs can be avoided), I mean
That is, you can use this scheme to achieve the precise attack of shells, and everything is so natural, there is gravity, there are parabolic, how beautiful trajectory ah!
Well, I said so much, is to remind you that if you encounter some interesting algorithms or functions in the study, can be combined into the actual project, so that the value of his play.
Well, it's time to write the answer. The pseudo code is as follows:
The Velocity component Vx,vy,vz,abs (float value) of the initial velocity v in the x, Y, Z axis is the absolute value function
Vx = ABS (X2-X1)/t;
Vy =abs (y1-y2)/t+ (1/2f) gt;
Vz = ABS (X2-X1)/t;
V =vector3 (VX,VY,VZ);
V is the result of our request. It looks like it's no big deal, so let's take a look at how to put him in the project.
We own the variable: time t, coordinate P1, coordinate P2, with these three, a perfect trajectory will appear!
The code is as follows:
usingUnityengine;usingSystem.Collections; Public classTestspeed:monobehaviour { Public floatTime=3;//represents the length of time from point A to B PublicTransform Pointa;//Point A PublicTransform POINTB;//Point B Public floatg=-Ten;//Gravitational Acceleration//Use this for initialization PrivateVector3 speed;//Initial velocity Vector PrivateVector3 Gravity;//Gravity vector voidStart () {transform.position= Pointa.position;//Place The object at point a//calculate the initial velocity by a formulaSpeed =NewVector3 ((pointb.position.x-pointa.position.x)/Time , (POINTB.POSITION.Y-POINTA.POSITION.Y)/time-0.5f*g*time, (POINTB.POSITION.Z-POINTA.POSITION.Z)/Time ); Gravity= Vector3.zero;//Gravity initial velocity is 0 } Private floatDtime=0; //Update is called once per frame voidfixedupdate () {gravity.y= G * (dTime + = time.fixeddeltatime);//V=at//Analog DisplacementTransform. Translate (speed*time.fixeddeltatime); Transform. Translate (Gravity*time.fixeddeltatime); }}
Before you click Run, assign a and b two points to the value. Then run, and a perfect curve is drawn down.
This article link: http://www.cnblogs.com/jqg-aliang/p/4806017.html, reprint please declare the source, thank you!
Unity an interesting face question