Comments (10)
Thanks taking a look now !
from cute_headers.
Sure I'll add in a big fat comment right above the definition of c2Ray. Good idea!
from cute_headers.
Here's the rendering of your code:
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.
In your function sro5hRayBug
you don't seem to normalize ray.d
, is that a typo?
from cute_headers.
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 :)
from cute_headers.
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.
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.
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.
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".
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.
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)
- cute_aseprite: 1.3 Tilemap Support HOT 2
- [cute_sound.h v2.x] Support multiple contexts. HOT 2
- http://randygaul.net is taken over by squatters HOT 1
- cute_net.h times out over the internet HOT 5
- Dynamically set panning for an individual playing sound HOT 2
- cute_aseprite.h - v1.3 Tilesets Discussion HOT 4
- cute_aseprite.h - Aseprite v1.3 property map support
- Cute_tiled "staggered" causes warning HOT 1
- inclusion of infinite maps and chunks HOT 3
- c2TOI returning wrong contact point
- cute_tls - allow ignoring certificate errors HOT 1
- cute_tiled: Force loading RGBA for tintedcolor
- cute_tiled: object `type` field HOT 4
- cute_2d c2PolytoPolyManifold issue HOT 2
- [Cute_Net.h] Knowing if a packet has been sent. HOT 3
- [cute_tiled_h]: Field `name` points to wrong memory address. HOT 2
- cute_aseprite.h "warning C4456: declaration of 'frame' hides previous local declaration"
- Usage of c2x and rotation HOT 1
- cute_aseprite.h crash with both 9-slice and pivot enabled. HOT 1
- cute_sound 2.x has no equivalent of 1.x cs_stop_sound
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cute_headers.