Issue with some 2D trig


#1

The green things are meant to align with the red lines, however the red lines are rotated incorrect apparently. The red lines are meant to connect c0 (the centre) to c1 (where the green dots are). The green dots are refered to as holders, and are the second to appear in the function.

f


-- Boring support functions 
local rad = math.pi / 180
local deg = 180 / math.pi

function UDimToVec2(UDim)
	return Vector2.new(UDim.X.Offset, UDim.Y.Offset)
end

function Vec2ToUDim(Vec2)
	return UDim2.new(0, Vec2.X, 0, Vec2.Y)
end

function CreateAFrame()
	local Frame = Instance.new("Frame")
	Frame.Parent = ScreenGui
	Frame.BorderSizePixel = 0 
	Frame.BackgroundColor3 = Color3.new(1, 1, 1)
	Frame.BackgroundTransparency = .5
	
	return Frame
end

local Trails = 10
local c1Magnitude = 100
local c2Magnitude = 200


function ExplodeFirework(c0)
	for i = 1, Trails do
		local InitialRotation = (360 / Trails) * i
		local c1 = c0 + (Vector2.new(math.cos(InitialRotation) * deg, math.sin(InitialRotation) * deg).unit * c1Magnitude)
		
		local Holder = CreateAFrame()
		Holder.Rotation = InitialRotation
		Holder.Position = Vec2ToUDim(c0)
		
		local Spark = CreateAFrame()
		Spark.Parent = Holder
		Spark.Name = "Spark"
		Spark.Position = UDim2.new()
		Spark.Size = UDim2.new(0, 3, 0, (c1 - c0).magnitude)
		Spark.BackgroundColor3 = Color3.new(1, 0, 0)

		local Holder = CreateAFrame()
		Holder.Position = Vec2ToUDim(c1)
		Holder.Size = UDim2.new(0, 5, 0, 5)
		Holder.Position = Vec2ToUDim(c1)
		Holder.BackgroundColor3 = Color3.new(0, 1, 0)

	end
end

#2

First of all, (* dec) needs to appear within the sine / cosine function, secondly, you should divide by it since you convert deg -> rad.


#3

I don’t think either of those things are correct? the sine and cosine functions spit it out in radians, no matter what its given, and then i just convert to degrees afterwards, which is fine?

And then the next thing I’m not sure what you’re saying? I’m not converting it to radians, I’m reversing that conversion. What do I need to divide?


#4

A sine / cosine takes an angle and turns it into a triangle side relation value. Since your input is an angle, you’ll have to convert InitialRotation to rad before using it within the sine / cosine.


#5

Oh I see!! Yes that fixed it, thank you very much!


#6

I did have it working, I then went AFK and came back to change some bits and I now have the same issue as before. My update code:

        local InitialRotation = (360 / Trails) * i * rad
		local c1 = c0 + (Vector2.new(math.cos(InitialRotation) * deg, math.sin(InitialRotation) * deg).unit * c1Magnitude)
		
		local Holder = CreateAFrame()
		Holder.Rotation = InitialRotation * deg
		Holder.Position = Vec2ToUDim(c0)

Thanks for your help!


#7

Remove the * deg multiplier outside of sin/cos. Again, the value sin/cos returns is not an angle, there’s no need to use radian math on it.


#8

It may help also to set the AnchorPoint of your Frames to Vector2.new(0.5,0.5)