- Home /
Predicting the path of a ball from it's forward vect
Hi all,
There are plenty of questions that seem similar to mine but I've not been able to factor them into a solution for my situation.
I have a camera with an invisible object parented to it, I then fire a ball from that position in it's forward direction using addforce.
What I need is a way to predict the trajectory of the ball and display a target object (just a cube at the moment) at that location, it needs to account for hitting obstacles in the way.
I've tried several solutions, the one that's been most successful is to create an invisible object every frame adding the same force in the forward direction and reporting the position at which it hits (then destroying it). This provides the effect I'm after but it's very laggy and unresponsive as it can take a while for the invisible projectiles to actually hit a surface.
Any help would be greatly appreciated.
Thank you.
Answer by Loius · Dec 14, 2012 at 10:15 PM
You might need to cheat it. It's possible to predict trajectory accurately, but hopefully you don't need perfection.
You have just a ball, right? You can use your level data (Colliders), multiple Spherecasts and Vector3.Reflect to approximate the ball's trajectory. Cast out a certain distance; if there's a collision, back up by an appropriate amount, reflect your direction around the vector from hit.point to the sphere's center assuming that point is on its surface (there's probably a function to get that vector), and continue casting until you run out of desired projection distance.
Hmm I'm not sure how this helps me, I need to know where the ball will hit the wall or floor, as in most games with a grenade arc for example.
Well I've never implemented it, or I'd give better particulars. If you need to simulate gravity as well just give your casts a bit of a downward slope - you'll have to to it iteratively in this case, to simulate gravity creating a 'slope' as the ball moves.
something like:
point : ball.position;
direction : ball.velocity;
simgravity : 0.0;
while( dist > 0 ) {
simgravity -= 9.81;
direction = (direction + simgravity).normalized;
Cast(from point to point + direction, max distance = 1.0)
if hit {
scale down gravity based on hit point relative to ball center
reflect direction around hit point -> ball center
}
dist -= 1;
}
You can make it immensely simpler by not worrying about reflection, and just calculate the arc until the first collision (Off the top of my head, I know $$anonymous$$GS4 does this). It's the same idea, just add gravity iteratively.
Answer by sparkzbarca · Dec 14, 2012 at 09:53 PM
itween
(google it, its a downloadable asset)
trajectory prediction instantly is actually alot of math.
tweening != trajectory prediction. With tweening, you already know the end value before starting.
Are you suggesting animating the ball to a point using itween rather than adding a force?
Your answer
Follow this Question
Related Questions
Gravity trouble: Falling slow, jumping fast 1 Answer
Convert Force into Velocity for 2D Player Jump 0 Answers
remove forward component from velocity vector 2 Answers
Setting limits to my trajectory 2 Answers
What is this variable type? 2 Answers