ofPolyline, how to discover angle at closest point

Hello, I’m making a particle system from scratch and wanted to know how to discover angle at closest point.
The only way I figured out with actual ofPolyline commands was using subdivision and getting the index of the closest vertex.
Is there another more precise way of doing that?
Thank you

Can you clarify what you mean with an diagram?

Sure @bakercp
I want to know the reflection angle at a determinate ofPolyline position so my particle can collide and bounce.
I think I have to find the normal at some location of the ofPolyline at a determinate point.
I can get an ofPoint of closest point but can’t get an index float of this point to be able to get the angle.
Any ideas are welcome! Thanks

Collisions are difficult. Collisions on “curved” (even if they are piecewise linear) are even a little trickier. A “naive” approach might be to cast a “ray” (i.e. draw a line) along the trajectory of path and figure out what ofPolyline segment it intersects first (this is tricky because you need to prioritize your line segment searches based on which line segments are closest (there are algorithms for doing this e.g. https://en.wikipedia.org/wiki/Bentley–Ottmann_algorithm, ofxSweep / ofxSweepLine).

Anyway, once you calculate the intersection of your particles’ trajectory and the ofPolyline segment of interest (you test for line intersections (I was just doing this in another example a few hours ago here https://github.com/SAIC-ATS/ARTTECH-3135/blob/develop/Assignment_0/WallDrawing327_WithMath/src/ofApp.cpp#L82-L128).

From the particle trajectory ray and the ofPolyline segment, you can calculate the angle of incidence and reflection, etc.

A couple tricky things to watch out for based on my experience …

Anyway … hopefully a few annotations help … :slight_smile:

1 Like

ofPolyline also has a getTangentAtIndex() function, which you can rotate 90 degrees to get a normal. I sometimes will calculate my pct between two indices and mix the two rotated tangent vectors based on the pct.

also @bakercp one trick to speed things up that I found kind of cool (which depends on the number of rays) is to rotate the polyline and ray so that the ray is axis aligned, then it’s very fast to search for intersections (ie, do this line segment have a.y < 0 and b.y > 0)…

3 Likes

Thank you @bakercp and @zach. I’ll try the ideas right now

1 Like

Already working here, I’ve subdivided the path in 200 parts and the piece of code is here in the case it helps somebody else.
tangent will depend on path direction of svg file (+90 / -90)
Thanks

        unsigned int node;
        ofPoint ponto = poly.getClosestPoint(mouse, &node);
        ofDrawCircle(ponto.x, ponto.y, 4);
        ofDrawLine(ponto, mouse);

        ofSetColor(0,0,255);
        ofPoint closePoint = poly.getVertices()[node];
        ofDrawCircle(closePoint, 4);

        ofVec2f tan = poly.getTangentAtIndex(node);
        ofVec2f perp = tan.getRotated(90);

        ofDrawLine(closePoint, closePoint + perp*50.0);
1 Like

Coooool trick :slight_smile: