tModLoader Creating dust at random points in a line between two positions

POCKETS

Terrarian
I've been able to create dust vortexes, dust rings, dust ovals, and many other effects with dust, but this one has me stumped.

I need to spawn dust between two projectiles at random points between them in a line. Projectile one spawns projectile two. I've passed in the parent projectile's whoAmI through NewProjectile's ai[1] (ai[0] is occupied). I then store that info using Projectile parentProj = Main.projectile[(int)projectile.ai[1]].

I'm only beginning to understand vectors, but I'm assuming to get the direction from the child projectile to the parent, I would need to do something like this - Vector2 direction = projectile.DirectionTo(parentProj.Center). Assuming this is correct, I now at least have this vector facing towards the other projectile. At this point, I would assume I would need to get the distance between the two. I'm a little fuzzy on that one, but to get the distance from a projectile to the player, you would do something like float distanceSQ = projectile.DistanceSQ(player). Replacing "player" with "parentProj.Center" (I'm assuming yet again) would give me the squared distance from one projectile to the other?

EDIT: So this is apparently incorrect. I believe the proper distance between the two would be worked out like this - Vector2 distance = direction + parentProj.Center

Now, the only thing I need to work out is how to get random points in between the two. The above code does work, and I can get the dust to spawn on the parent projectile's position based on the distance from the child projectile, but I can't seem to understand how to get random points in between the two...

This is about as far as I've been able to go. I lack trig knowledge, and with my limited knowledge of Vectors, I'm just not sure how to tie all this in to the Vector2 position the dust requires. .

Any help in this regard would be very much appreciated :)
 
Last edited:
Luckly this is a rather simple thing! You can use lerp (Linear Interpolation).
C#:
Vector2 RandomPoint = Vector2.Lerp(Value1, Value2, Amount);
It's very simple, simply input the start point at value1, the end point at value2, then use any float number from 0 to 1 for amount. Let me show a example.
If your points are (0,0) and (1,1), and you wanted to do right in the middle, it would look like:
C#:
Vector2 RandomPoint = Vector2.Lerp(new vector2(0, 0), new vector2(1, 1), 0.5f); // Will generate a vector2 at (0.5, 0.5)
You can use a simple random number generator to make a random value from 0 to 1 to plug into the amount section
All in all, it should look like this:
C#:
var rand = new Random(); // Need to have 'using System;'
float randAmount = (float)rand.NextDouble(); // Generates a random double between 0.0 and 1.0 type casted to a float
Vector2 RandomPoint = Vector2.Lerp(projectile.Center, parentProj.Center, randAmount);
This should generate what you are after!
 
This should generate what you are after!

Thank you! I'm curious if there's a way to add more positions in between the two? It looks a little too sporadic, but does work.

EDIT: Never mind! I got it working the way I needed it ;-)

Very cool, nonetheless. Exactly what I was looking for.

You can use lerp (Linear Interpolation)

I will do some reading on this. Thank you for the explanation :)
 
Back
Top Bottom