How do I find a vector point between two other vector points?

So here’s the deal, I made an entity that slides from point A to point B and back, based on a “base” entities position.

The base, is a 4x4 prop_physics wall, and the ent is a 3x3 prop_physics wall.

Now, what I’m trying to do is grab the base’s position, and find the positions of two opposing corners.

Then, I need to find a vector point between those two vectors that changes based on time.

I already did all of this, successfully, the only problem is that I used a static vector offset added to the base’s position with the Angles set to (0,0,0) on the base.

So when I would rotate the base and try to calculate it, the points would be wrong because the corner’s position offset change with the yaw rotation.

So I’m left with two options, snap the angle to the nearest 45 degrees and use more vector offsets that vary

OR

dynamically calculate the positions of the corners and variable point between them.

I want to do the latter if possible.

The only reason I’m asking facepunch is before I do a lot of unnecessary math and complicated equations, is there a simple way to achieve this?

Here’s some example code of what I plan to do with this code.

I plan on moving the 3x3 across the 4x4 from corner to corner, based on time (like a sliding feature)

My original code has a pretty large scope, so this is better.

[lua]
local mul = math.cos((math.pi * t) / 2) – where t = time
local x = 23.725402832031 * mul
local y = x * -1

sent:SetPos( base:GetPos() + Vector(x,y, 0) ) – 11.899916887283 originally
sent:SetAngles(base:GetAngles())
[/lua]

So to summarize, I know the base’s position, i’m trying to move the ent sitting on top from corner to corner over time.

The corner positions change when you rotate the yaw angle (clockwise movement) so a one-size-fits-all vector offset won’t work.

I need to find a dynamic way to calculate the offsets and find any given point between that range.

Here’s a visual aid to show what I’m talking about also.

Yes, I know it’s a crappy paint png.

Another note is that the base will always be flat, so the only angle that concerns us is the yaw angle.

Thanks Facepunch, if it wasn’t for you guys I’d probably be raking the shattered remains of this monitor into the trashcan right about now.

[editline]11th August 2017[/editline]

http://imgur.com/a/qJF8j

[editline]11th August 2017[/editline]

Alrighty, so I read through all of this and from what I understand, your getting the pos vectors of the base 4x4, and then calculating the corner positions so that the 3x3 can move on top of it.

My suggestion to you is to parent the 4x4 to the 3x3. This way, you won’t have to calculate what position the 4x4 corners are, you already know where they are with a static vector.

Example:

Imagine the world map as an x,y grid. You have the 4x4 rotating like it does. You’re doing all this math to calculate corners etc. But remember, the world itself isn’t moving.

Apply that logic to the 4x4. Make the 4x4 the “world” of the 3x3. You’re basically moving the 4x4 back and forth from (20,20) to (-20,-20). Except, the 4x4 is the world, and the 3x3 is the 4x4. Get it? Parent the 4x4, therefore, making the vectors static for when you change the position of the 3x3 relative to the 4x4.

Make the 4x4 the parent of the 3x3.

(That’s the way you should do it)

You can also do it using the Unit circle, though you would have to get hacky by making the unit circle go into the direction of the corner and then get the opposite value vector of that.

https://wiki.garrysmod.com/page/Trigonometry (Look for unti circle)

image doesn’t work. From what I understand you’re trying to calculate the position of the two other corners knowing only two points diagonal to each other? There are easier ways to calculate this, but using some vector math it’s still pretty easy:

Suppose you have a pair of vectors, v1 and v2. Now let’s define a function f:[0,1]->V (in this case, V=R^2, useless maths to know for you right now). We want this to be a linear funciton where f(0) = v1 and f(1) = v2. The function we’re looking for is this one:

f(t) = (1-t)v1 + tv2

the mid point between these two vectors can be found at t=0.5. At this point some linear algebra is needed to calculate a tiny bit of extra information (we’re really just calculating basis vectors, where the basis matrix has determinant 1, as to not “skew” length in any direction). Since we aren’t given any information about angles (which makes this problem impossible in higher dimensions since it will give us infinitely many possible solutions - think infinitely many possible rotations), more vector math: yay.

The derivative of f(t) is df/dt = v2-v1. Without explaining why (you can google this one), the gradient of the normal vector is -1/(v2-v1), or nicely said: 1/(v1-v2). You can very this result my taking the dot product, and checking this is 0. Using this we can now create a second function to find the two other corners. Name this f2:[-0.5,0.5]->V, and let it be defined as follows:

f2(t) = f(0.5) + t/(v1-v2)

The two other corners can now be found at f(-0.5) and f(0.5). I haven’t proof checked this post, so I might have to come back and double check but it seems fine to me as I’m writting it. I’ve left it in pure maths talk so you have the joy of getting it to work without someone just straight up giving you the answer. Enjoy.

[editline]12th August 2017[/editline]

Really fucking hope I understood what you meant - otherwise I just wrote a load of crap out for nothing :v:

You wrote a bunch of crap for nothing. He isn’t that stupid. He’s already figured that out. He was trying to calculate it based on angle and changing position. I told him an easier way. You should reread his post.

Nah I was hoping I explained this clearly but it seems I did a bad job, sorry about that.

I’m trying to find a set of opposing corners knowing only the position and yaw angle of the base ent.

Then, with those corners I want to use their positions as a range for the 3x3 to travel across.

It’ll move on a variable point on the line between those two vectors based on the current time of the game.

I’ll take some pics of it so you guys can understand what I mean.

I understand what you mean, what I’m telling you is to dtich the yaw calculation and just make te 4x4 the parent and set the pos of the 3x3 relative to the 4x4

Here’s the pics:

The first is what it looks like normally (put into point A or the first corner) the second picture represents it in the opposite corner with the opposite x and y vector offset. This is all based on base:GetPos() which is the midpoint of the prop.

From the third picture onward it shows what happens when you rotate it to any angle other than the default (0,0,0) spawn angle; there’s overlap, it doesn’t seamlessly meet the corner like it does in the default angle etc.

I want to have it always hug the same corner when it slides from point A to point B regardless of what angle the yaw has rotated to.

(Sorry for the small pictures, I couldn’t find an image uploading site that made multiple pictures easy to upload)

http://imgur.com/QOuJm06

http://imgur.com/QOuJm06

http://imgur.com/QOuJm06

[editline]12th August 2017[/editline]

http://imgur.com/QOuJm06

[editline]12th August 2017[/editline]

http://imgur.com/a/88HsY

[editline]12th August 2017[/editline]

The IMG tag is not wanting to work for me today.

*snip

[editline]12th August 2017[/editline]

Are you trying to make a clockhand?

Nah it’s not a clock. This is going to be the first script I’ve sold for garrysmod in years. It’s nearly finished but I’ve hidden a lot of elements as to what it is because I don’t want to reveal what it is yet.

As far as parenting goes, I’ll give it a try once more, but in my tests it was giving very strange errors. Like, when I just now tried it, it made the 3x3 disappear when it finished traveling from one end to the other.

Maybe if I parent it right before movement and unparent it when it finishes that can work, but it’ll be pretty hacky.

Still, I appreciate the suggestion. I’ll see if I can make it work.

[editline]12th August 2017[/editline]

YES! It works. Thank you so much perky. You saved me hours of time.

[editline]12th August 2017[/editline]

So I did have to parent it during the slide and immediately unparent it when the time is reached to reach the opposite point.

The only error now is that the 3x3 disappears for one frame and then reappears when it reaches the opposite end and I unparent it.

Does anyone know what causes this error and how I can fix it?

[editline]12th August 2017[/editline]

Turns out if you parent an object shared and unparent it serverside it removes that error in case anyone wants to know.

It’s working fully now. Cheers.