Detecting clicks in a non-rectangular shape - xna-4.0

I am pretty new to XNA 4.0 and have a problem I can't seem to figure out. So far I have been detecting mouse clicks by creating rectangles and checking the rectangle.contains method, but what if I don't want to check inside a rectangle?
In one part of my map I have a triangle button that I want to let the user click, but I want to only count it as a click if it is actually inside the triangle. If I create a rectangle around the triangle the user will be able to click outside the texture area, if you know what I mean.
How do I go about doing this?

You will have to use a Point in Polygon Algorithm for more complex shapes:
C# Point in polygon
Determining Whether A Point Is Inside A Complex Polygon
The check for convex polygons (your triangle) is simpler than for concave ones (see first linked article).
If you have to do lots of checks and are hitting performance limits, consider using some kind of hierarchy, a Quadtree or an LOD system. For example, you can calculate an additional bounding rectangle for very complex polygons and only do the expensive in-check with the polygon if the point lies inside the rectangle.

Related

Find all 3D points inside a cube(3D Box)

I'm trying to find out the 3D points of a point cloud which are inside a 3D Box. I'm able to get the points inside 3D box by the below logic, only when the 3d box is align with the axis, but on rotating the 3d box, the logic fails.
(This only applies if the box's edges are aligned with the coordinate axes.)
If the vertices are (x1,y1,z1),(x2,y1,z1),(x1,y2,z1),⋯,(x2,y2,z2), then any point (x,y,z) is inside the box if and only if
x1<x<x2
and
y1<y<y2
and
z1<z<z2
In other words, the point's coordinates are between the vertices' coordinates.
Now I got algo from https://math.stackexchange.com/questions/2909812/points-inside-a-3d-rectangle?noredirect=1&lq=1 but not able to figure out how to take the "position vectors".
Any suggestions!!
Another issue: Is there any fast way to find out all the points inside a 3D box, rather than iterating over the whole point cloud.
Any suggestion is appreciated!!!
Since you listed point-cloud-library in the question's tags, pcl::CropBox is probably what you are looking for. You basically provide the two corner points, a translation and a rotation, after that, you can extract all points inside the box.
I'll use the notation $\vec{OA}=\vec A-\vec O$ for a difference of position vectors. $O$ is one vertex, and $A,B,C$ are its three adjacent vertices.
O,A,B,C in there are vertices of cuboid. Being a parallelepiped, cube can defined by three adjacent faces, that's four points in total.

3D algorithm for clamping a model to view frustum

What would be an efficient approach for constraining an object so that it's always at least partially intersecting the view frustum?
The use case is that when viewing a model I want to clamp camera panning, as well as model translation, so that the view frustum is never looking at empty space.
One approach I tried was to wrap the model objects in bounding volumes, then enforce the constraint when those fall outside the frustum. I've tried bounding boxes so far, but am considering using a minimal convex hull.
The problem is that that when you zoom in close enough, it's still possible to be looking at empty space within the boundary, as shown in the attached diagram.
This is for a WebGL application, so needs to be fairly efficient in JavaScript, and also for thousand of vertices.
Ideally you would have a aabb tree of your mesh, and then you can recursively project on camea/screen until you get an intersection ?
http://www.codersnotes.com/algorithms/projected-area-of-an-aabb
edit: it's just frustum culling algo against aabtree does anyway, so looking for optimized solution, is looking for optimized frustum culling things
https://fgiesen.wordpress.com/2010/10/17/view-frustum-culling/
http://www2.in.tu-clausthal.de/~zach/teaching/cg_literatur/vfc_bbox.pdf
As general approximation is possible I would try the point cloud. First create a list of points - either by every Nth mesh vertex or every Nth face centre. With N being for example 10. Once you have this point array all you do is check if any of points is in frustum while updating its orientation. If not then this means that user moved or rotated the camera too much and you need to restore last acceptable orientation.
I know that this may seem quite ridiculous but I think that it is fairly easy to implement and frustum checking of vertex is just couple of multiplications per plane. It will be not perfect though.
You can also make frustum a little smaller to ensure that there is some border around the object.

Checking user touched in a closed area

I have a 3D world with curves. Each curve is simply a list of 3D points.
When user clicks on the scree, i would like to test if the point is in a closed area.
For example, the user draws four curves that form a rectangle. If he clicks inside this rectangle i would like to "know" that it is a closed area.
I could not find any known algorithms - how i can achieve this behaviour?
Assuming your "closed areas" are all planar: For each area, find the point of intersection between the pick-ray and the area's plane. Then use an odd-even crossing test (following a ray in that plane) to determine whether the point is inside that area.
If your areas are not planar, you can instead use a winding test, where you sum the signed angles about the pick-ray of each consecutive pair of points in the area, and see whether that sum is nonzero (accounting for numerical drift).

Circle Corner Collision Behavior

I am working on a project where I have a rolling marble in 2D. I can detect when the marble hits the corner of a box but I can't figure out what behavior it should have when it hits the corner. I need to figure out how to reposition the ball so it doesn't overlap the box. With a box to box collision it is as easy as setting the boxes right next to each other but for a circle and box I don't know where to move the marble to so it looks realistic.
I am not asking how it bounces off. I do have a good understanding of trig. As you move the marble towards the box it will, at some point, come to overlap with the box. I need to know how to make it so they don't overlap anymore but so it still looks realistic. It does not need to bounce off (like assuming infinite mass on part of marble).
Take a good look at this article on Wikipedia: Elastic Collision.
You will need to create surface normals for your 2D objects at any given surface point, which will be problematic if your boxes are perfectly square, as sharp edges don't have normals. You will have to give the corner cases (no pun intended) special treatment and give them surface normals that are the average of the normals of the two lines that join at the corner.
Basically, use vectors.

Algorithm to produce rounded edges and corners in a 3D mesh

Starting with a 3D mesh, how would you give a rounded appearance to the edges and corners between the polygons of that mesh?
Without wishing to discourage other approaches, here's how I'm currently approaching the problem:
Given the mesh for a regular polyhedron, I can give the mesh's edges a rounded appearance by scaling each polygon along its plane and connecting the edges using cylinder segments such that each cylinder is tangent to each polygon where it meets that polygon.
Here's an example involving a cube:
Here's the cube after scaling its polygons:
Here's the cube after connecting the polygons' edges using cylinders:
What I'm having trouble with is figuring out how to deal with the corners between polygons, especially in cases where more than three edges meet at each corner. I'd also like an algorithm that works for all closed polyhedra instead of just those that are regular.
I post this as an answer because I can't put images into comments.
Sattle point
Here's an image of two brothers camping:
They placed their simple tents right beside each other in the middle of a steep walley (that's one bad place for tents, but thats not the point), so one end of each tent points upwards. At the point where the four squares meet you have a sattle point. The two edges on top of each tent can be rounded normally as well as the two downward edges. But at the sattle point you have different curvature in both directions and therefore its not possible to use a sphere. This rules out Svante's solution.
Selfintersection
The following image shows some 3D polygons if viewed from the side. Its some sharp thing with a hole drilled into it from the other side. The left image shows it before, the right after rounding.
.
The mass thats get removed from the sharp edge containts the end of the drill hole.
There is someething else to see here. The drill holes sides might be very large polygons (lets say it's not a hole but a slit). Still you only get small radii at the top. you can't just scale your polygons, you have to take into account the neighboring polygon.
Convexity
You say you're only removing mass, this is only true if your geometry is convex. Look at the image you posted. But now assume that the viewer is inside the volume. The radii turn away from you and therefore add mass.
NURBS
I'm not a nurbs specialist my self. But the constraints would look something like this:
The corners of the nurbs patch must be at the same position as the corners of the scaled-down polygons. The normal vectors of the nurb surface at the corners must be equal to the normal of the polygon. This should be sufficent to gurarantee that the nurb edge will be a straight line following the polygon edge. The normals also ensure that no visible edges will result at the border between polygon and nurbs patch.
I'd just do the math myself. nurbs are just polygons. You'll have some unknown coefficients and your constraints. This gives you a system of equations (often linear) that you can solve.
Is there any upper bound on the number of faces, that meet at that corner?
You might you might employ concepts from CAGD, especially Non-Uniform Rational B-Splines (NURBS) might be of interest for you.
Your current approach - glueing some fixed geometrical primitives might be too inflexible to solve the problem. NURBS require some mathematical work to get used to, but might be more suitable for your needs.
Extrapolating your cylinder-edge approach, the corners should be spheres, resp. sphere segments, that have the same radius as the cylinders meeting there and the centre at the intersection of the cylinders' axes.
Here we have a single C++ header for generating triangulated rounded 3D boxes. The code is in C++ but also easy to transplant to other coding languages. Also it's easy to be modified for other primitives like quads.
https://github.com/nepluno/RoundCornerBox
As #Raymond suggests, I also think that the nepluno repo provides a very good implementation to solve this issue; efficient and simple.
To complete his answer, I just wrote a solution to this issue in JS, based on the BabylonJS 3D engine. This solution can be found here, and can be quite easily replaced by another 3D engine:
https://playground.babylonjs.com/#AY7B23

Resources