Detect whether a part is heading towards another and at what rate

Hello, I am currently stuck on a problem.

Overall, I want to achieve the doppler effect on light, Redshift & Blueshift. In a nutshell, when an object heads away from you at relativistic speeds, it will appear as if the object has tinted red, and vice versa.
Would look something like this:
image
Light waves move to the right relative to observers.

My issue is that I’m not certain where to start, and how to code the intensity of one’s speed heading away or towards an object. I have tried using Velocity.Magnitude but that only prints out positive numbers, and no negative (heading away), which doesn’t differentiate between an object moving away or towards a part with its velocity.
image

This would be the relativistic doppler redshift formula for objects traveling transverse, which is what i will be using. I haven’t seen many posts talking about this in the forum, only found about unrelated raycasting and sound waves.

Any help would be awesome! :slight_smile:
Thank you.

What you’ll want is to take the position of the point and subtract it with the position of the part in question. Magnitude will only get the distance between the two points. Also velocity is deprecated and should no longer be used for new work. Instead, you’ll want to use AssemblyLinearVelocity.

1 Like

Thanks for replying,
is there a way to convert Vector3 to a single number?
I’ll try your suggestion.

Distance is the only reason you’d want to do that. But I guess you could calculate the average with all 3 axis of the Vector3. But realistically, you’ll probably want to run your code with each axis of the Vector3.

I’ve tried it and it doesn’t really work as I expected, I tried some variations of

local Shift = rel.Position - p.Position -- rel = ball pov, p = other parts
			p.Color = Color3.fromRGB(
				255 / math.sqrt(1- (Shift.X^2 / c^2))
				,255 / math.sqrt(1- (Shift.Y^2 / c^2))
				,255 / math.sqrt(1- (Shift.Z^2 / c^2))
			)

Including simplifying to

local Shift = rel.Position - p.Position -- rel = ball pov, p = other parts
			p.Color = Color3.fromRGB(
				255 / Shift.X
				,255 / Shift.Y
				,255 / Shift.Z
			)

And the result I got:

This is from the red ball’s frame of reference. (Ignore the stretching/contracting map)
Increasing the velocity to the same amount for each axis results in different colors, mainly secondary colors. I assume the axis is mixed up with the colors (XYZ) + (RGB).

Another thing I’ve noticed is that all parts get shifted an equal amount of color, I’m not entirely sure what has happened but the front-facing side of the ball is supposed to receive the bluest and red for the backside.

You can use dot product to get a direction positive or negative.

image

Example for movement to detect forward movement or backwards movement strafing:

BTW, I think I made a mistake it’s not really calculus not sure just vectors.