Interesting c++ differences

Interestingly I wrote my code different to the video (actually wrote it before the video) and my code is seemingly more stable. In the code of the video you need to add the lines about GetSafeNormal(). In my code I do not need it. It works without much issue (other than running at 10000 worth of speed, but 5000, and 1000 work just fine). Even if it requires more lines of code I feel like finding the magnitude of the vectors may have been a contributing factor?
EDIT: On a side note perhaps me placing the actual movement of the platform as the last line of code makes more of a difference?

// Called every frame
void AMovingPlatform::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	//Move platform back and forth
		// do as above then check how many units of movement we have made
		// if gone to far then reverse movement 
	float result;
	if (MyVector.Size() > LocationStart.Size()) {
		result = MyVector.Size() - LocationStart.Size();
	}
	else {
		result = LocationStart.Size() - MyVector.Size();
	}
	
	if (result >= UnitsOfMovement) {
		PlatformVectorDir *= -1;
		LocationStart = MyVector;
	}

	MyVector += PlatformVectorDir * DeltaTime;
	SetActorLocation(MyVector);
}

I had a look at your code and wanted to share what I found. Starting with the variables set to the following PlatformVectorDir = (1000, 0, 0), UnitsOfMovement = 10, LocationStart = (0, 0, 0), MyVector = (0, 0, 0), I outputted some of the variables at different stages and this is the result:

Current location is X=0.000 Y=0.000 Z=0.000
Platform's distance from start location is 0.000000 out of 10
Moving 400.000006 units

Current location is X=400.000 Y=0.000 Z=0.000
Platform's distance from start location is 400.000000 out of 10
Changing direction at location X=400.000 Y=0.000 Z=0.000
Moving 156.397194 units

Current location is X=243.603 Y=0.000 Z=0.000
Platform's distance from start location is 156.397186 out of 10
Changing direction at location X=243.603 Y=0.000 Z=0.000
Moving 8.333400 units

Current location is X=251.936 Y=0.000 Z=0.000
Platform's distance from start location is 8.333400 out of 10
Moving 13.110597 units

Current location is X=265.047 Y=0.000 Z=0.000
Platform's distance from start location is 21.443996 out of 10
Changing direction at location X=265.047 Y=0.000 Z=0.000
Moving 13.152201 units

I then did the same with the code used in the course and ended up with this:

Current location is X=0.000 Y=0.000 Z=0.000
Moving 8.333400 units
Platform's distance from start location is 8.333400 out of 10

Current location is X=8.333 Y=0.000 Z=0.000
Moving 400.000006 units
Platform's distance from start location is 408.333405 out of 10
Changing direction at location X=10.000 Y=0.000 Z=0.000

Current location is X=10.000 Y=0.000 Z=0.000
Moving 155.332208 units
Platform's distance from start location is 155.332214 out of 10
Changing direction at location X=0.000 Y=0.000 Z=0.000

Current location is X=0.000 Y=0.000 Z=0.000
Moving 16.712900 units
Platform's distance from start location is 16.712900 out of 10
Changing direction at location X=10.000 Y=0.000 Z=0.000

Current location is X=10.000 Y=0.000 Z=0.000
Moving 15.575100 units

Now what is happening here? Well, the difference is that the code from the course checks if the platform has gone out of bounds, and forces it back to the max position if it has. That’s it, really. In your code, if the platform goes out of bounds, it stays out of bounds and just happily continues along. This is where you would need to use the unit vector to find the max position and place the platform there if it has gone out of bounds.

Now, having done this much, I also want to point out some other things with your code. It’s really great that you try finding your own approach, and it will definitely help you improving much faster. I hope I can give you some pointers to speed up your improvement even further.

MyVector and LocationStart are in fact positions, not vectors (as you probably realize, but it’s worth really taking this into consideration). You are essentially turning them into vectors from (0, 0, 0) when getting the magnitude. You get the magnitude of the vector from (0, 0, 0) to MyVector, then the vector from (0, 0, 0) to LocationStart, and then you calculate the difference in their length. To do this, you need to check which vector has the greatest magnitude so that you can subtract the other vector’s magnitude from that since subtraction isn’t commutative.

What if I told you there is an easier way? Rather than comparing the two vectors, you can create a vector between the two positions MyVector and LocationStart and find the magnitude of that vector. This is where it helps to remember that MyVector and LocationStart are in fact positions, and you can get a vector between two positions by subtracting one position from the other. And what is even nicer is that you can do it in any order. MyVector - LocationStart = -(LocationStart - MyVector). It’s the same vector, just pointing in the different direction. And it doesn’t even matter which direction it is pointing when you’re getting the magnitude because that will always be positive. I made a simple drawing to illustrate this.

This reply has gotten really long, so I’ll wrap it up here. I’ll end with that setting the location of the platform on the last line is definitely the way to go. The main issue I have with the code from the course is that the platform is potentially moved twice. Once to the new calculated position, and then to the max position if it had gone out of bounds. Checking if it has gone out of bounds before ever placing it will allow you to just set the new position to the max position. No need to do it twice.

4 Likes

Interesting! I forgot about finding a vector between two positions, it was over 4 years ago that I actually did this kind of math. Thanks for the long comment! It really helps!

No problem. Glad I could be of help. I’m taking this course myself to refresh my C++ knowledge, in addition to learning Unreal Engine. I could tell you had some knowledge of vector math, but I also wanted to write as detailed as I could for other people who might read this. I hope to see more of your code along the way. Keep being awesome :slight_smile:

3 Likes

Privacy & Terms