Trigonometry help needed

Hello everyone !

I need to create a ray // to the floor, starting from the character, which goes in the direction of the mouse, how could i do that ?

I tried a lot of different stuff, but can’t seem to get it right, for example I can’t do ViewportPointToRay because i’d need the length of this vector to create a ray // to the floor after.

Here is an example of the data I know in green (the rest could be possible but idk how) :

Please don’t bully me with my skills in paint

Every idea is appreciated :slight_smile:

1 Like

Um um um

I’m pretty sure you need 3 pieces of information to solve for those unknowns you have (3 sides, 2 sides + angle, or 2 angles + side).

You only have one angle and one side length. But i’m pretty sure your Z at the top can be solved for in Roblox. Are you using Mouse.Hit or something like it (I’ve never used it before). If you can, try to get the mouse position and the character position. Then the length of Z is just

local Z = (CharacterPosition -  MousePosition):Magnitude()

I think. That’s assuming I am using the right syntax, please double check it, it’s been a while since I’ve dealt with vectors.

Oh yeah and you use sine law to solve for the remaining sides.

1 Like

Thanks for your reply, I’m not using mouse.Hit but UserInputService:GetMouseLocation() and ViewportPointToRay. Problem with mouse.Hit.p is that it might not be the exact distance of z.
Example :


Z1 is the distance i’d need and Z2 is the distance i’d get from using mouse.Hit.p :confused:

I don’t really know why u guys are having trouble. In fact, it doesn’t need trigonometry. Here I will give u a formula

Direction: Target - Origin

Target should be the Vector3 position of mouse.hit (mouse.hit.Position) and origin a Vector3 that is the position the ray will come out of

The image in the reply above shows that something simple like this won’t work.

If @Lord_Monever is looking for Z1 as shown, there are no Vector3s available to solve.

1 Like

But I already know the direction, what I need is the length of y from my first godpaint image to create a ray // to the floor

1 Like

Alright I think I figured it out. I just need to know, do you know the following in the image:

Vectors C, M, and R?

So I know the pos of the character and the camera, the direction and length of the vector from the char to the camera and the direction of the vector from the camera to the mouse.
I don’t know the length from that vector, neither the direction/length of the vector // to the floor going from the char to the mouse (and ofc the position of the mouse to create a // vector to the floor)

TLDR : I know the length and direction of C, and the direction of R.

The rest could be calculated but idk how

Actually I think I’ll try this Line segment-plane intersection accuracy code, it might be the right thing for me

1 Like

Okay just to make it super extra crispy clear, since I’m getting lost in these diagrams.

Can you clarify what you are solving for. Also I think you mentioned that mouse.Hit or whatever gives you “Z2” or what I’m writing as Vector “M”, so I’m getting even more confused by that lol

But anyways try out that line segment intersection thing and see if it works.

Yeah that code works ! Sorry for confusing you haha, I hope that you got better in paint with this post at least :smiley:

1 Like

Lol yes there’s always room to improve in paint in my case.

I’m still going to try and solve it nicely (I saw the post you linked and it seems a little over-aggressive). If I don’t reply to this thread again then I’ve probably lost too many braincells to continue D:

1 Like

Okay after a few slices of trees I am back. I think I figured it out, it’s a nice one-liner. I’m not joking.

Now I’m going to define C, O, and M as the camera position, character position, and mouse position respectively. Not vectors pointing towards each other.

We need a constant k in order to use the one-liner. That’s because V is horizontal so its y-coordinate is the same as the character’s.

local C = Camera.Position -- wherever you get this from
local O = Character.HumanoidRootPart.Position
local M = Mouse.Position -- wherever you get this from

local k = (O.Position.Y - C.Position.Y)/(M.Position.Y - C.Position.Y) 

-- Now the one-liner
local V = C - O + k*(M - C)

-- and if you want the magnitude (distance)
print(V:Magnitude()) 

@DogPooIEat yeah in retrospect that makes a lot more sense now. I guess I was having too much fun drawing in paint.

Besides, a one-liner is more interesting than having the internals do it for you. I understand if it’s a paragraph of code but this solution is just as short.

I’m not really into roblox studio trigonometry but I use real life one so maybe you can implement sin(x+y)= sin x.cos y+ sin y . cos x