Jump to content

How do I fix this?

as96
Go to solution Solved by as96,

nevermind I fixed it, I thought of all complex and useless stuff when the fix was:

		float pX = std::nearbyintf(mPosition.x);		float pY = std::nearbyintf(mPosition.y);

I'm trying to write an extremely simple "AI", but I'm having this problem:

 

Route step code:

	sf::Vector2f AI_Route::Step()	{		float pX = Math::Clamp<float>(std::ceil(mPosition.x), 0, mPoints[mAtPoint].x);		float pY = Math::Clamp<float>(std::ceil(mPosition.y), 0, mPoints[mAtPoint].y);		if (mAtPoint < mNoPoints)		{			if (pX == mPoints[mAtPoint].x && pY == mPoints[mAtPoint].y)			{				mAtPoint++;			}			if (mAtPoint < mNoPoints)			{				mPosition = Math::Lerp<sf::Vector2f>(mPosition, mPoints.at(mAtPoint), mPercentageSpeed);			}		}		return mPosition;	}

Clamp and Lerp code:

		template <typename T> T Lerp(const T& start, const T& end, float percentage)		{			return (start + percentage*(end - start));		}		template <typename T> T Clamp(const T& value, const T& min, const T& max)		{			return value < min ? min : (value > max ? max : value);		}

The problem is related to some kind of rounding error that I can't fix for some reason, I tried flooring, ceiling, and clamping the number but nothing, and now it's even skipping points if I clamp the values as you can see in the video.

Also, now it goes out of range (but only if I clamp the values)

I spent 15 minutes trying to fix it but nothing...

Link to comment
Share on other sites

Link to post
Share on other sites

Edited the post, so you can focus on the important part of the code (the step function)

Link to comment
Share on other sites

Link to post
Share on other sites

I don't get why you are clamping it to begin with. You're clamping your position between [0, mPoints[mAtPoint].x]. The points you skip have mPosition.x >= mPoints[mAtPoint].x, which results in pX == mPoints[mAtPoint].x. If you want to clamp, clamp the position based on the dimensions of the canvas. That way you get (if you truly want to get) a position within the boundary of the screen. Remove the clamp otherwise.

 

Are your mPoints integers? Try maybe rounding the mPosition and bringing it into a integer when you compare. Also i think your mPosition.y should be floating too, but you have it as 0 instead of 0.f

Link to comment
Share on other sites

Link to post
Share on other sites

I don't get why you are clamping it to begin with. You're clamping your position between [0, mPoints[mAtPoint].x]. The points you skip have mPosition.x >= mPoints[mAtPoint].x, which results in pX == mPoints[mAtPoint].x. If you want to clamp, clamp the position based on the dimensions of the canvas. That way you get (if you truly want to get) a position within the boundary of the screen. Remove the clamp otherwise.

 

Are your mPoints integers? Try maybe rounding the mPosition and bringing it into a integer when you compare. Also i think your mPosition.y should be floating too, but you have it as 0 instead of 0.f

I'm clamping it because it went to 201 instead of 200, so the points didn't match, and I had to ceil it because if I didn't it goes at a slightly lower value like 199.999.

The mPoints are floats

Link to comment
Share on other sites

Link to post
Share on other sites

nevermind I fixed it, I thought of all complex and useless stuff when the fix was:

		float pX = std::nearbyintf(mPosition.x);		float pY = std::nearbyintf(mPosition.y);
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×