- Home /
Calculate a trajectory in all possible directions (2D).
I want to draw a trajectory from a Ball which the player can launch in all directions(up,down,right,left). For example the force could be (1,2) , (-3,4) and so on. I was searching for Formulars to calculate some points which I can draw a trajectory with in the web, but I was not able to find anything.
In the following Image you can see what I try to archive. The red ball in the middle is the player. By dragging the mouse a Force is being calculate. The Force can let the ball fly in all directions.
What Im trying to archive is to draw the "fly line".
Can someone explain me what I have to do please?
Answer by lgarczyn · Oct 13, 2019 at 11:35 PM
The other answer has an issue, it's that unity doesn't actually uses parabolas to simulate trajectories, but an approximation of them. If you use parabolas, the projectiles won't match your predictions, and you'll accumulate errors with time. This will get worse with a lower physics tickrate.
What you want is something like this:
Vector3 currentPos = startPosition;
Vector3 prevPos = currentPosition;
Vector3 currentVelocity = startVelocity;
for (int i = 0; i < stepCount; i++)
{
currentPos += currentVelocity * Time.fixedDeltaTime;
currentVelocity += Physics.gravity * Time.fixedDeltaTime;
//Replace by a TrailRenderer or LineRenderer
Debug.DrawLine(prevPos, currentPos, Color.Red, 1f);
prevPos = currentPos;
}
This only works with a drag factor of 0, but the unity drag approximation can also be easily copied.
Thank you for the answer. Can you tell me which drag factor I need to set to 0. At the moment Unity is drawing a staright line.
Is your Physics.Gravity set to anything? $$anonymous$$aybe StartVelocity is too big. $$anonymous$$aybe stepCount is too low, it should be around a 100 to 300.
The drag factor is 0 be default I think, but you can change it inside the Physic$$anonymous$$aterial of your projectile. It's probably not the issue here.
I don't know what I done wrong yesterday but it worked out perfectly today. Thank you very much :D.
Answer by RadonRaph · Oct 13, 2019 at 01:22 PM
Hello @OneCode-Official, your question is not very clear. If your object is in (0,0) and you apply a force (1, 2) m/s² it will be at (1,2) in 1 ms, at (2,4) in 2 ms etc If you have gravity you need to substract 9.81 at Y axis so same exemple (0,0) + (1,2) = 1ms (1,-7.81), 2ms (2, -17,62) etc You use https://en.wikipedia.org/wiki/Classical_mechanics from Newton :) Hope that help. Raph
Thank you for your answer, I think this could help me a lot. Sorry for not being very clear but my english is not that good.
I tried it out and it did not worked out for me. I updatet my question so you can understand better what I am trying to do. Nevertheless thank you very much.
Oh ok thats more clear. First step you need to get drag input:
Vector2 mouseDrag;
Vector2 BeginDrag;
void On$$anonymous$$ouseDown(){
BeginDrag = Input.$$anonymous$$ousePosition;
}
void On$$anonymous$$ouseDrag(){
mouseDrag = Input.$$anonymous$$ousePosition-BeginDrag;
}
With this you will get the mouse offset. If you inverse the vector you will get the direction of your ball and the magnitude of the vector is the "force". Then i wonder how you can draw that x) il will make my research :)
Thank you. I already managed to calculate the velocity of the ball. Drawing the path is the only problem I have at the moment
Your answer
Follow this Question
Related Questions
How to make an orbital movement as smoothly as possible? 0 Answers
strange unrealisticly physic problems about velocity/gravity acceleration 2 Answers
Drawn trajectory different than real trajectory 2 Answers
Calculate velocity to set to reach a target point on a plane considering the drag. 0 Answers
SImply yet tricky question about RELATIVE VELOCITIES... 1 Answer