hey guys,
heres my problem:
im aplying a force to an object (throwing balls kinda like angry birds) & i would like 2 show the player the trajectory the throwable object will take till it hits the ground.
how can i calculate the positions (5 positions in mid-flight) the object will pass once ts thrown?
You can do this using some quite simple equations of motion sometimes called SUVAT equations as S is displacement, U is initial velocity, V is final Velocity, A is acceleration and T is time.
Anyway I wasn’t sure how to describe how to do this as I was taught it over a whole module in maths so it would be a rather brief description if I tried but you can see the list of equations under suvat equations [here][1]
So as I can’t really describe the process easily, heres a script similar to what you want. It only works in 2D currently (facing from front using the x and y axis) and should be attached to something at the starting position of your object. It may need some editing as it uses Impulse which is ‘instantaneous force’ which is probably what you are actually using but I’m not sure.
var tSample = new Array();
var TrajVec = new Array();
var PrevD = 10.0;
var Samples = 5;
var GivenImpulse = Vector2(1, 1); //uses impulse rather than force, this is equal to a instantaneous force
private var u : Vector2;
var m = 1.0; //mass of object thrown
private var line : LineRenderer;
function Start () {
line = gameObject.AddComponent ("LineRenderer");
line.SetWidth(0.1, 0.1);
line.SetVertexCount(Samples+1);
for(var j = 0; j < Samples; j++){
line.SetPosition(j, Vector3(0, 0, 0));
}
}
function Update () {
calcTraj();
line.SetVertexCount(Samples+1);
}
function UpdateLine () {
for(var j = 1; j < Samples+1; j++){
line.SetPosition(j, Vector3(TrajVec[j-1].x, TrajVec[j-1].y, 0));
}
}
function calcU (I : Vector2) {
u = Vector2(I.x/m, I.y/m);
}
function calcTraj () {
var ys : float;
calcU(GivenImpulse);
tSample.Clear();
TrajVec.Clear();
for(var xs = PrevD/Samples; xs <= PrevD; xs += PrevD/Samples){
tSample.Push(xs/(u.x));
}
for(var i = 0; i < tSample.length; i++){
ys = u.y * tSample <em>+ (-9.81 * tSample <em>* tSample _* 0.5);_</em></em>
_ TrajVec.Push(Vector2(PrevD/Samples + (PrevD/Samples * i), ys));
* } UpdateLine(); } hopefully you can get that working, if not feel free to ask me and I’ll try to help, Scribe [1]: https://sentynel.com/media/old/equations.html*_
<p>Some Notes</p><p>Increasing PrevD will increase the length of the line</p><p>Increasing Samples will make the line smoother which you will have to change to suit your needs.</p><p>GivenImpulse is currently fixed so you should look at linking that to the distance and direction of the mouse from your object I guess.</p><p>Make sure you change the mass variable to be whatever the rigidbody component says it is</p><p>That should clear things up!</p><p>Scribe</p>
<p>Some Notes</p><p>Increasing PrevD will increase the length of the line</p><p>Increasing Samples will make the line smoother which you will have to change to suit your needs.</p><p>GivenImpulse is currently fixed so you should look at linking that to the distance and direction of the mouse from your object I guess.</p><p>Make sure you change the mass variable to be whatever the rigidbody component says it is</p><p>That should clear things up!</p><p>Scribe</p>
– Scribe