How to model trajectory with Beams?

With the new beams you can do some nifty things, I intend to use this red line to show where the projectile will land when thrown.

Question: Given only the start position and velocity, how should i arrange the attachments to correct model the trajectory?

18 Likes

You’ll want to set CurveSize0 and CurveSize1 to the same number, except their signs flipped. So for example, CurveSize0 = 10 and CurveSize1 = -10 (or the other way around, depending on your attachment setup).

We can now use some background knowledge about Beziér curves to find out the top height of the curve. Suppose the first attachment is at height Y0, and the second attachment is at height Y1. The top of the curve will be at Y = (Y0 + Y1)/2 + 3/4 * CurveSize. So in the example with the curve sizes above, where the parts are at Y0 = Y1 = 0, the height of the curve would reach up to Y = (0 + 0)/2 + 3/4 * 10 = 7.5 in its centre.

If you want to manipulate the top height of the curve yourself, you can use the following formula to figure out what the curve size should be:
CurveSize = 4/3 * (Y - (Y0 + Y1)/2)

Or just:
CurveSize = 4/3 * Ydiff
(where Ydiff is the amount that the curve should be raised in the middle)

You would then set CurveSize0 to CurveSize, and CurveSize1 to -CurveSize (or the other way around).

Images:

image

image

Since curve size is 12 here (+12, -12), the height of the curve is raised 9 in the middle

7 Likes

No why u removed it? I already implemented half!! :stuck_out_tongue:

2 Likes

Ahh, guess I’ll put it back. I just realised though that this won’t give you a realistic curve if the heights aren’t equal between the parts.

1 Like

Hmm… It hits right at the top, but at the other points, especially in the beginning, its not very accurate.
I think it’s because the direction it’s being tossed at isnt straight up…

I don’t think a projectile necessarily follows a bezier path. If you’re simply launching the part using the physics engine, you’re gonna have to do some ballistic projectile calculations first, and then set the beam points and curves (control points).

3 Likes

Yes, the path is calculated already. The issue is shaping a beizer curve that fits the resulting projectiles path.

Lets assume both points have same height to begin with.
If I set the attachments rotation same as the launch angle, what would be the top height of the curve?

If you know the launch angle, and you know the width between the start and end, then you can calculate the height from that using:

tan(angle) = height / width = 4/3 * CurveSize / width

CurveSize = 3/4 * width * tan(angle)

(angle is in radians)

you mean distance?

Yes

2 Likes

Seems to be too strong:

Here’s file if u wana see for yourself:
Trajectory_1.0.rbxl (16.1 KB)

2 Likes

image

wouldn’t that method model the trajectory like this? (i exaggerated the curve to display what i mean better)

(nvm, launch angle, not angle between start and finish)

I needed this for my project as well Lol, ill join it on the experimenting later when home.

1 Like

You’re in good company here!
We are all highly-skilled scientists doing quick progress on this!

Namnlös
Current progress

6 Likes

If you want this to work and be accurate, you have some algebra to do. Assuming this projectile is being launched and subject only to gravity (not wind resistance), what you probably want is for the beam to show the path of the centroid of the thing being thrown, which is a parabola. Beams use a cubic Bézier curve, which can fit any parabola section exactly, so that’s not a problem. The simplest thing to do is to express the path of your object first as a position function of time, normalized for t=[0,1]. You can then set that parametric parabola equal to the cubic Bézier equation (see wikipedia page for Bézier curve), and solve for the two unknown control points (the scalar distances from the endpoints, CurveSize0 and CurveSize1) in terms of your known control points (the path endpoints) and the launch velocity.

Your launch angle is presumably known, and your landing angle is easily gotten from the derivative of the parabola at t=1. Even though you’re probably launching things in many different directions, you should solve this as a 2D problem.

5 Likes

Hmm… Would you know how to get the maximum-Y value of a beizer curve?
More specifically, where both controlpoints are at same position, forming a parabola.

1 Like

Dude I want to take pathfinding and turn it into a beam path with an arrow at the end, it may involve using what is involved in this to get those curves.

1 Like

Yeah, you just solve for where the first derivative of the curve is 0 (specifically the Y component if you’re doing it all with 2D vectors). The wiki page already has the derivatives shown too.

The trajectory is simple to calculate!

Attachment0’s Orientation points in the direction of ‘vel’ - the velocity.

CurveSize0 = 9.81 * .2 * vel.magnitude
CurveSize1 = 0
Attachment0.Position = start
Attachment1.Position = start + vel*dt + Vector3.new(0,-g*dt^2/2,0)

where g = 9.81 * 20
and dt > 0, decides length of parabola

Who needs math?! I’m an expert at variable approximation!

59 Likes

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

2 Likes