Object Space versus World Space, Normals, CFrames, Math, Functions

I’ve an NPC Insect, which climbs up and down walls in the correct orientation (Ask for it), but will need more math for all possible surfaces; slopes, or even a globe (for another project)…

And the new Raycast returns Normals (And No, I don’t know either; what the new BruteForce, Ray parameter is about)…

  • Anybody got links to niffy functions or well built models with the CFrame To-World-or-Object math or any other math stuff for this, or tutorials…? I’m not grasping it…

In return: I give all my code away… or hit me up for extremely, high, IQ, AI Scripts anytime… I love making them for free; and the only thing I’m pro at…

Thanks in advance,
RealAi Inc.

3 Likes

Unless there’s something specific you want to ask, it’s kind of hard to help. Try searching in the community resources and tutorials for stuff on CFrames, cross product, dot product, etc.

I guess for the world/object space CFrame stuff you could check out this thread from yesterday: I have tried for DAYS please help (Cross P) - #6 by Astr0Derp

It’s not exactly an introduction to any of this stuff but it explains the thought process behind solving these kinds of geometry problems and what we can use the tools for that we know from vector math.

Speaking of which, all of this is based on trig, vector math, geometry and linear algebra. If you want a more solid foundation you can study that besides game programming, khan academy is a great resource and I’d recommend you start with trig if you haven’t done that already and then vector math. Just keep in mind you don’t actually need to study this, it’s just if you’re curious about how the math works out. It’s also not a great way to learn if you’re just interested in how it’s applied in games, or at least I know from my own experience that the formulas only really start to make sense when I get to apply it for a project. I much prefer just learning what I need for a specific project.

That actually gives me an idea, it’d be great if there was a compilation of various math formulas that you learn in school and how they can specifically be used for programming games. I think that’d be really motivating for a lot of students.

Again, if there’s something more specific you want to ask, feel free to comment here or even make a new thread :slight_smile:

Well, yeah, I need formulas… I’ll try that link…

I’m old. Started in Machine Language, which only had ADD, SUBTRACT, and Multiply/Divide by 2, so I didn’t need math: We just fudged everything…

So for the RTS wargame on a Spherical Earth:
This places a Model correctly on a Hex of the globe; given that the hex is facing it’s Front, up; toward the “sky”:

code

local rel = c:toObjectSpace(tpart.CFrame);
tpart.CFrame = origin:toWorldSpace(rel);

But to Turn a Unit already on the globe toward an enemy unit, I have no idea…
My hack: I put both units mathematically, on a flat Earth; use CFrame LookAt; then back on the globe…

But if things get going with lots of units, I need more efficiency…
I just don’t got formulas…

The Insect AI on a flat-Earth; also wants to treat obstacles like spheres, and crawl on them; or on the underside of a dinning room table…, so normal CFrame.LookVectors don’t work…

I got stuff to give:

This deforms a cube made of checker-boards into a sphere…

function cubeToSphere(x,y,z)
return Vector3.new(
xmath.sqrt(1-y^2/2-z^2/2+(y^2z^2)/3),
ymath.sqrt(1-z^2/2-x^2/2+(x^2z^2)/3),
zmath.sqrt(1-x^2/2-y^2/2+(x^2y^2)/3)
);
end

Um…
Professional Game Designer; who knows an EXCELLLECT rOBLOX Artist; looking for Mathematician…

Let’s make beautiful music…

(Um… I don’t know how to use the Show-text-as-code delimiter. either…)

(1) Test124c41 - Roblox

1 Like

Both RaycastResult.Position and RaycastResult.Normal return a Vector3 value. You’ll likely want to use RaycastResult.Position.

Intro to Raycasting
RaycastResult

1 Like

Well, yeah, that works for completely vertical walls to nudge the spider to be perpendicular to the wall before orienting him facing up…

but not slopes or upside-down…

#code

function GetNormal(N) – Be Normal; actually. Square-up to a Normal
move(bin.Position - N) – Aim; actually
wait(.1)
end

1 Like

Normal will return a Vector3, which appears to be the side of which the ray is hitting. You could probably just do some math to compare the orientation of the hit part itself and your insect to match the orientation.

Though if you’re wanting to make that sort of slope system where it’s height can change based on multiple parts, I would raycast from core points (such as the top and bottom), then use an average of that to calculate the orientation for the insect.

1 Like

Here’s an example on a ball:

Animation5


local InputS = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local unit1 = game.Workspace.Unit1
local unit2 = game.Workspace.Unit2
local ball = game.Workspace.Ball
local ballRadius = ball.Size.X / 2

--Moves a unit to a world-space point, facing a specific way and with their upVector a specific way (all in worldspace)
function moveUnitTo(unit: Model, point: Vector3, upVector: Vector3, frontVector: Vector3)
	local cf = CFrame.lookAt(point, point + frontVector, upVector)
	unit:SetPrimaryPartCFrame(cf)
end

--Given the surface normal at a point on the surface of the ball, return the point on the ball.
function getBallPoint(surfaceNormal: Vector3): Vector3
	return ball.Position + surfaceNormal * ballRadius
end

--Given a point on the surface and a desired point to face towards (all in world space), 
--	get the world space facing direction / LookVector that looks towards the target
--	while keeping the UpVector the same as the surface normal at that point.
function getSurfaceFacingDirection(surfacePointFrom: Vector3, surfacePointTo: Vector3): Vector3
	local translation = surfacePointTo - surfacePointFrom
	local translateDir = translation.Unit --This goes through the sphere. We want this, except perpendicular to the surface normal.
	
	local surfaceNormal = (surfacePointFrom - ball.Position).Unit
	local rightVector = translateDir:Cross(surfaceNormal) --This will be perpendicular to surface normal *and* the facing direction
	local facingDirection = rightVector:Cross(surfaceNormal) --This will be perpendicular to surface normal and rightVector. That means it's out result!
	
	return facingDirection.Unit
end

--Moves a unit to a point on the ball where there's a specific upVector 
--	and looking towards a point in world space. 
-- The surface is entirely defined by it's upVector / normal at the point, 
--	because I'm assuming it's a ball where it's easy to generate the point from that.
function moveUnitToOnSurface(unit: Model, upVector: Vector3, frontVector: Vector3)
	moveUnitTo(unit, getBallPoint(upVector), upVector, frontVector)
end

while wait() do
	if InputS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
		if mouse.Target == ball then
			moveUnitToOnSurface(unit2, mouse.Hit.p.Unit, getSurfaceFacingDirection(mouse.Hit.p, unit1.PrimaryPart.Position))
		end
	else
		if mouse.Target == ball then
			moveUnitToOnSurface(unit1, mouse.Hit.p.Unit, getSurfaceFacingDirection(mouse.Hit.p, unit2.PrimaryPart.Position))
		end
	end
end

It doesn’t quite adapt to all surfaces because some complex surfaces would require pathfinding. On a sphere it works well though. It’d work OK but not perfectly on a bumpy sphere. Oh and it breaks if the target point is exactly opposite the starting point, just handle that special case with an if statement.

Code formatting is done with three backticks before and end of code block, e.g.

```
print(“hello, code formatting!”)
do
print(“indentation FTW!”)
end
```

becomes

print("hello, code formatting!")
    print("indentation FTW!")
6 Likes

Thank you very much. Yes, spider shoots short Ray forward and another; down('cliff ahead?)
Will work on this today…

And; holy moly; never heard of a back tick b4 in my life, but found it; upper left on keyboard...

Also, So Sorry, but you mentioned it…
I just read; new PathFind (I have been away) and it says:

Call Path; creates, what seems to be, a global NavMesh, for all NPCs (Should other NPCs only call an ASync?)
If an NPC gets a Blocked Waypoint then Call Path; and call an ASync

So, should other NPCs take note, abandon their old Waypoints at that time, and re-start by calling an ASync;
or go on about their merry way, with old Waypoints…?

Asking for a friend (The spider is already creepy enough, and just climbs obstacles)

1 Like