Detecting surface intersection in lispbuilder-sdl - sdl

I am working on making a game in Common Lisp, using Lispbuilder-SDL. I am currently writing a function to check for collision between two surfaces. I need to find out if a surface A intersects with another surface B. I haven't yet seen anything that fits the bill on https://lispbuilder.googlecode.com/svn/trunk/documentation/lispbuilder-sdl.html - is anyone aware of a simple, built-in way of doing this?

The math for the line that forms when two planes/surfaces intersect is not difficult. See Intersecting Planes, Plane Geometry or generally Googling. If there is no intersection, then the planes/surfaces are parallel - there are simple tests for that.
A plane is defined by a point and a normal. If the dot-product of two plane normals is one, then the planes are parallel. In your game, if the dot-product is close enough to one, then the intersection is outside of the play area.

Related

How to get curve from intersection of point cloud and arbitrary plane?

I have various point clouds defining RT-STRUCTs called ROI from DICOM files. DICOM files are formed by tomographic scanners. Each ROI is formed by point cloud and it represents some 3D object.
The goal is to get 2D curve which is formed by plane, cutting ROI's cloud point. The problem is that I can't just use points which were intersected by plane. What I probably need is to intersect 3D concave hull with some plane and get resulting intersection contour.
Is there any libraries which have already implemented these operations? I've found PCL library and probably it should be able to solve my problem, but I can't figure out how to achieve it with PCL. In addition I can use Matlab as well - we use it through its runtime from C++.
Has anyone stumbled with this problem already?
P.S. As I've mentioned above, I need to use a solution from my C++ code - so it should be some library or matlab solution which I'll use through Matlab Runtime.
P.P.S. Accuracy in such kind of calculations is really important - it will be used in a medical software intended for work with brain tumors, so you can imagine consequences of an error (:
You first need to form a surface from the point set.
If it's possible to pick a 2d direction for the points (ie they form a convexhull in one view) you can use a simple 2D Delaunay triangluation in those 2 coordinates.
otherwise you need a full 3D surfacing function (marching cubes or Poisson)
Then once you have the triangles it's simple to calculate the contour line that a plane cuts them.
See links in Mesh generation from points with x, y and z coordinates
Perhaps you could just discard the points that are far from the plane and project the remaining ones onto the plane. You'll still need to reconstruct the curve in the plane but there are several good methods for that. See for instance http://www.cse.ohio-state.edu/~tamaldey/curverecon.htm and http://valis.cs.uiuc.edu/~sariel/research/CG/applets/Crust/Crust.html.

Collision Detection Techniques to Generate Sloping Planes between two Mesh

I've asked this question both at game SE and math SE, but the response were not so encouraging. So I reasked again, with a bit more of a twist.
I have a terrain, which is defined by mesh. And there are a lot of other polygonal faces scattered throughout the terrain, they can be located above, or below or cutting through the terrain. You can think of those faces as platforms.
A screenshot below should clarify what I mean. Despite looking smooth, all the mesh are actually consist of small elements (number> 10k) combined together, giving the false appearance of smoothness. The obvious disconnected area are platforms.
My question is, how can I generate the planes that connect between the platforms and other platforms/ terrain? Here are the rules to generate the series of sloped planes:
They could go up or down, depending on which direction will make them hit the terrain/neigbouring platform first.
The plane generation rule is that, the plane will start at the edge of a platform, and moving 45 degree upward/downward with respect to z axis for a certain length, then it will move 0 degree with respect to z axis for another certain length, and repeat. So it will be a series of piecemeal planes until at some points of the planes, obstacles are hit.
The algorithm should be focused on plane generation and plane generation alone; I don't want it to be tied to any renderer ( e.g, opengl and whatnot), I can render it myself.
In short, I want to generate a set of "planes" that is actually something like a flight of stairs or a piece of corrugated paper, leading up or down from one point in space until it makes contact with a given mesh
Sounds like a straight forward collision detection problem that are frequent in physics simulation, right? Is there any game/physics libraries that I can use to attack this problem?
Note that since I am not doing any animation, so frame-by-frame update and all those stuffs are not relevant to me; this is why I am hesitant to use existing game physics library like bullet. What is relevant to me is how to use existing libraries to generate those connecting planes according to the above rules.

library for beam tracing (beam intersection) on a 3D Polygon model

I want to simulate a laser scanner which emits laser beam onto a 3D model to measure distance or other features from the model. The 3D model consists of vertices in xyz coordinate and faces; each vertex has also some user defined features.
The method should be simple. I define a view point and view vector (i.e. laser beam); what I need to do is checking the first vertex or the first face which is intersected with the view vector, then I can measure the distance and evaluate feature from the nearest vertices.
Is there any available library or tools to do that?
What you are talking about is, in a very literal sense, ray tracing. The maths and code behind doing this is not particularly complicated, especially if you don't have to consider reflections. There's a tutorial for doing exactly this in C++ here; triangle intersection is almost as simple as sphere intersection, and you can completely ignore the surface properties. If you don't want to write your own code (but seriously, it's maybe a hundred lines to do what you're looking for), there's a hint as to how to get Povray to do what you're after here.
EDIT: More maths, including triangle intersection, is here.

Finding object under mouse

I'm developing a game that basically has its entire terrain made out of AABB boxes. I know the verticies, minimum, and maximum of each box. I also set up my camera like this:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(Camera.rotx,1,0,0);
glRotatef(Camera.roty,0,1,0);
glRotatef(Camera.rotz,0,0,1);
glTranslatef(-Camera.x,-Camera.y,-Camera.z);
What I'm trying to do is basically find the cube the mouse is on. I thought about giving the mouse position a forward directional vector and simply iterating through until the 'mouse bullet' hits something. However this envolves interating through all objects several times. Is there a way I could do it by only iterating through all the objects once?
Thanks
This is usually referred to as 'picking' This here looks like a good gl based link
If that is tldr, then a basic algorithm you could use
sort objects by z (or keep them sorted by z, or depth buffer tricks etc)
iterate and do a bounds test, stopping when you hit the first one.
This is called Ray Tracing (oops, my mistake, it's actually Ray Casting). Every Physics engine has this functionality. You can look at one of the simplest - ODE, or it's derivative - Bullet. They are open-source so you can take out what you don't need. They both have a handy math library that handles all oftenly needed matrix and vertex operations.
They all have demos on how to do exactly this task.
I suggest you consider looking at this issue from a bigger perspective.
The boxes are just points at a lower resolution. The trick is to reduce the resolution of the mouse to figure out which box it is on.
You may have to perform a 2d to 3d conversion (or vice versa). In most games, the mouse lives in a 2d coordinate world. The stuff "under" the mouse is a 2d projection of a 3d universe.
You want to use a 3D picking algorithm. The idea is that you draw a ray from the user's position in the virtual world in the direction of the click. This blog post explains very clearly how to implement such an algorithm. Essentially your screen coordinates need to be transformed from the screen space to the virtual world space. There's a website that has a very good description about the various transformations involved and I can't post the link due to my rank. Search for book of hook's mouse picking algorithm [I do not own the site and I haven't authored the document].
Once you get a ray in the desired direction, you need to perform tests for intersection with the geometries in the real world. Since you have AABB boxes entirely, you can use simple vector equations to check which geometry intersects the ray. I would say that approximating your boxes as a sphere would make life very easy since there is a very simple sphere-ray intersection test. So, your ray would be described by what you obtain from the first step (the ray drawn in the first step) and then you would need to use an intersection test. If you're ok with using spheres, the center of the sphere would be the point you draw your box and the diameter would be the width of your box.
Good Luck!

Techniques for generating a 2D game world

I want to make a 2D game in C++ using the Irrlicht engine. In this game, you will control a tiny ship in a cave of some sort. This cave will be created automatically (the game will have random levels) and will look like this:
Suppose I already have the the points of the polygon of the inside of the cave (the white part). How should I render this shape on the screen and use it for collision detection? From what I've read around different sites, I should use a triangulation algorithm to make meshes of the walls of the cave (the black part) using the polygon of the inside of the cave (the white part). Then, I can also use these meshes for collision detection. Is this really the best way to do it? Do you know if Irrlicht has some built-in functions that can help me achieve this?
Any advice will be apreciated.
Describing how to get an arbitrary polygonal shape to render using a given 3D engine is quite a lengthy process. Suffice to say that pretty much all 3D rendering is done in terms of triangles, and if you didn't use a tool to generate a model that is already composed of triangles, you'll need to generate triangles from whatever data you have there. Triangulating either the black space or the white space is probably the best way to do it, yes. Then you can build up a mesh or vertex list from that, and render those triangles that way. The triangles in the list then also double up for collision detection purposes.
I doubt Irrlicht has anything for triangulation as it's quite specific to your game design and not a general approach most people would take. (Typically they would have a tool which permits generation of the game geometry and the navigation geometry side by side.) It looks like it might be quite tricky given the shapes you have there.
One option is to use the map (image mask) directly to test for collision.
For example,
if map_points[sprite.x sprite.y] is black then
collision detected
assuming that your objects are images and they aren't real polygons.
In case you use real polygons you can have a "points sample" for every object shape,
and check the sample for collisions.
To check whether a point is inside or outside your polygon, you can simply count crossings. You know (0,0) is outside your polygon. Now draw a line from there to your test point (X,Y). If this line crosses an odd number of polygon edges (e.g. 1), it's inside the polygon . If the line crosses an even number of edges (e.g. 0 or 2), the point (X,Y) is outside the polygon. It's useful to run this algorithm on paper once to convince yourself.