Giter Club home page Giter Club logo

Comments (10)

RandyGaul avatar RandyGaul commented on May 3, 2024 1

Thanks taking a look now !

from cute_headers.

RandyGaul avatar RandyGaul commented on May 3, 2024 1

Sure I'll add in a big fat comment right above the definition of c2Ray. Good idea!

from cute_headers.

RandyGaul avatar RandyGaul commented on May 3, 2024

Here's the rendering of your code:

image

All is working as intended! Except I bet you meant to normalize your ray direction :)

Try:

ray.d = c2Norm(c2V(100, 100));

Here's the code I used to do the rendering in the tinyc2 example demo:

void sro5hRayBug( )
{
	c2Ray ray;
	ray.p = c2V( 100.0f, 100.0f );
	ray.d = c2V( 100.0f, 100.0f );
	ray.t = 1.0f;

	c2Circle circle;
	circle.r = 30.0f;
	circle.p = c2V( 200.0f, 200.0f );

	DrawCircle( circle.p, circle.r );

	c2Raycast cast;
	if ( c2RaytoCircle( ray, circle, &cast ) )
	{
		c2v impact = c2Impact( ray, ray.t );
		c2v end = c2Add( impact, c2Mulvs( cast.n, 1.0f ) );
		tgLineColor( ctx, 1.0f, 0.2f, 0.4f );
		tgLine( ctx, impact.x, impact.y, 0, end.x, end.y, 0 );
	}

	c2v end = c2Add( ray.p, c2Mulvs( ray.d, ray.t ) );
	tgLineColor( ctx, 1.0f, 1.0f, 1.0f );
	tgLine( ctx, ray.p.x, ray.p.y, 0, end.x, end.y, 0 );
}

from cute_headers.

sro5h avatar sro5h commented on May 3, 2024

In your function sro5hRayBug you don't seem to normalize ray.d, is that a typo?

from cute_headers.

RandyGaul avatar RandyGaul commented on May 3, 2024

Nope not a typo. The ray does not have to be normalized, but it seemed like you wanted it to be judging by your post. Here's a pretty good link on vectors and normalization, take a peek if any of the math here is confusing. Rays are just vectors for the most part :)

https://www.khanacademy.org/computing/computer-programming/programming-natural-simulations/programming-vectors/a/vector-magnitude-normalization

from cute_headers.

sro5h avatar sro5h commented on May 3, 2024

Well I feel like I understand the math (normalization creates a Vector of length 1 with the same direction) :)
But I really don't understand why my code gives me the wrong impact point.

from cute_headers.

sro5h avatar sro5h commented on May 3, 2024

Why are you using ray.t in c2Impact, wouldn't that just give you the endpoint of the ray rather than the actual impact?

from cute_headers.

sro5h avatar sro5h commented on May 3, 2024

What really confuses me is that if I take the code from this issues and replace the circle with an AABB the raycast gives me the correct impact. So there has to be some difference between c2RaytoCircle and c2RaytoAABB if one time cast.t is correct even if ray.d is not normalized and the other time it isn't

from cute_headers.

RandyGaul avatar RandyGaul commented on May 3, 2024

The impact point is off because the function c2RaytoCircle is yet again written with speed in mind. It's not really your fault, it's just confusing. Most other libraries will take extra care and effort to prevent this kind of thing from happening, but tinyc2 is much more "bare metal".

ray

If we look here we can see a huge floating point value. It doesn't even have decimal points. That's because the function is taking big vectors and dotting them together, making a giant value for the disc variable. This giant value has such poor precision that the t value, calculated later, is visibly inaccurate. This is because large floating point numbers have very low precision.

To prevent this kind of thing from happening normalizing the Ray's direction vector results in a much smaller disc value, of only 900.003906. That is plenty small enough to give an accurate result.

c2RaytoAABB worked out OK because the algorithm inside is not very sensitive to the large ray.d inputs. The c2RaytoCircle function uses an algorithm that is very sensitive to the length of the ray's direction vector.

The solution is to normalize the ray's d vector and scale t by the length. In this way the magnitude of the old longer d vector is transferred to the t variable. In general the raycast functions in tinyc2 are not very sensitive to fairly large t values.

Something like this:

c2Ray ray;
ray.p = c2V( 100.0f, 100.0f );
ray.d = c2Norm( c2V( 100.0f, 100.0f ) );
ray.t = c2Length( c2V( 100.0f, 100.0f ) );

from cute_headers.

sro5h avatar sro5h commented on May 3, 2024

Ok now I understand it, thanks for the help.

Maybe a notice along the lines of "Keep in mind that direction vectors with a huge length can introduce major inaccuracy in the result. As a fix it is best to normalize the direction of the ray" in a comment above the function would be helpful for others?

from cute_headers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.