Giving an angle, force

Assume that i have the angle of impact on this part (could really be any part). This is then a vector3

image

How can i add “force” to this angle. I want to create a vector3 which is basically applying force to this point, on a specified angle.

Here is what i have tried:

local function Force(ray, raycastAngle)
	local part = ray.Instance
	local force = 500
	
	local RelativeVector = part.CFrame.LookVector -- In this case the arm
	
	local ImpactAngle = -(RelativeVector - raycastAngle) 
    -- Since applyimpulse is based on a relative angle, impact angle is the angle
    -- we want

	ray.Instance:ApplyImpulse(ImpactAngle * force) -- flings the part with the force of 500, based on 
    -- the impactangle. This snippet is the one thats wrong
end

ApplyImpulse applies it on the entire part, which is fine. I just need the adding force part

Why I am doing this? Simple! Its for a gunsystem. when a character is ragdolled, i want to fling the part that is shot in a certain direction.

Thanks :slight_smile:

Not sure if this is what you want, but ApplyImpluseAtPosition() exists, which allows you to apply force to any position

I can try that, but the question still remains. How do i apply X force, given an impact angle

You apply the force based on the direction of the Ray itself, the direction of the arm does not matter:

ray.Instance:ApplyImpulseAtPosition(rayDirection * force, ray.Position)

You need to keep track of the direction of the ray, which is the Vector of the ray, but normalized. For example:

local rayOrigin = ...
local rayVector = (rayTarget - rayOrigin)

local rayResult = workspace:Raycast(rayOrigin, rayVector)

-- `.Unit` returns the "Normalized" Vector3, which points in the same direction,
-- but is only 1 unit long
local rayDirection = rayVector.Unit

That works!

The first value is an impact angle, the second is a equivalent raycast unit direction.
-56.15999984741211, -51, 0
-0.43277668952941895, 0.8305954933166504, 0.3504503667354584

But i do wonder, assume that i only have the angle. How do you calculate it then.

1 Like

You can’t. There’s no axis, an angle is just a number. You need a direction to apply a force, that’s what a force is, you can’t apply one with just an angle out of context.

1 Like


if this makes any sense

You can’t just have an angle. Angles are only meaningful in 2 dimensions, but we work in 3. You need 2 axes to have it make sense. The angle between these 2 lines is 45*,


This doesn’t provide any useful information, because if I rotate the diagonal line around the horizontal line, the angle between them doesn’t change even though I have an entirely different direction.

To get any meaningful data, we need 1 more which we’ll use as our pivot.
Here the pivot is the Y-axis (0,1,0)

Here the pivot is the Z-axis (0,0,1)

2 Likes

-56.15999984741211, -51, 0

An angle like this? The drawing was only a visual. My orignial script had a 3 Dimentional angle

What is raycastAngle in your original Script?

This isn’t an angle. An angle describes the difference in direction between 2 direction, if both start at the same point. It’s a single number. What you have there is (I assume) a vector, which is a direction. You can’t have a 3 dimensional angle, just like you can’t have 3 dimensional time (it’s just 1 number)

1 Like

I raycast from the camera, foward.

Before i do the thing i asked about, i make a visual of the raycast.

local Raycast = Instance.New("Part")

Raycast.CFrame = CFrame.new(origin + direction/2, origin + direction)

local raycastAngle = Raycast.Orientation

So yes the Unit is what i was looking for, but just for giggles i wanted to see how to do it the way i thought of doing it

What the other Exercitus is progress

Ah, right.

So in theory, remove the 0.

We have this left.
-56.15999984741211, -51

How do i tell the game to launch an object at this angle, with x Force

RelativeVector - raycastAngle is not an angle, nor is it a direction. It is meaningless, because RelativeVector is a “Forward Vector” that represents the direction the Part is facing (if you do Part.Position += Part.CFrame.LookVector it will move forward 1 unit), it’s components are from 0 to 1, and Part.Orientation represents actual rotations from -180 to 180. They do not represent the same type of data, and substracting one from the other doesn’t give anything useful.

1 Like

Can’t they just use the raycasts Normal multiplied by *-1 and then multiplied by a value to determine the intensity? If you make the normal result *-1 it would essentially just be the direction no? The normal is just a unit vector that is the rays “reflection” of the surface. If you multiply it be negative 1 you’ll get the direction as a unit vector. Then multiply it by a number and you have your knockback force.

Yeah. That is what i was sort of thinking. I forgot that after you use orientation, you have to add force to where its looking. knowing this, I could create an instance, change the orientation of the instance to the orientation and then shoot it using lookvector*force. Any ideas on how to not create an instance to save on RAM?

No, that is because then it fires not where i want to.


(red is normal)

It would fire outward or inward depending on how i write it.

What is the instance for? Why are you trying to do all of this instead of just keeping track of the ray direction?

Aaaaah now i get what it means. Never mind me, thanks for the help!

No, it would be 180 degrees within the ray direction. It will only be equal if the surface is perpendicular to the ray, making the normal and ray direction parallel.