Advertisement Jump to content
Colby Ray

Help with 3d collision question

Recommended Posts

Hi, I'm new to 3d work. My platform is android with opengl es 3. I use blender to model the 3d. I made a lake and the camera moves around the scene. But my question is how to confine it to the lake area. To prevent the boat from leaving the lake. In blender the lake is just a 2d plane really, but of irregular shape. Would I need to know points around the lakes perimeter? How would I get that? How would you do it?

Share this post


Link to post
Share on other sites
Advertisement

I’ve done something similar recently creating a race track which was also just a giant snaking plane. I’d suggest you select the entire outer edge all the way around your plane and extrude it up so you have a wall around the whole thing.

I would then use this wall as an invisible collider stopping the boat from leaving your lake.

 

Share this post


Link to post
Share on other sites

How big is the lake, do you keep the whole thing loaded at once? Is the edge represented as a mesh or a texture? 

Do you need to accurately follow triangles in the mesh, or could you use bilinear control points to define the edge?

Do you want to reflect, bounce or collide n slide, or something more complicated like get some air or crash?

How much do you like maths?

Share this post


Link to post
Share on other sites

I can think of lots of ways of doing this, depending on your game details as TeaTreeTim says, however, quite a cool cheap way of doing it and colliding the boat against the edges:

  • Take the lake geometry (a bunch of triangles with mostly shared vertices) and build this into a navmesh. Then use a standard navmesh solution to detect moves to neighbouring polys, and prevent moves or slide against boundaries of the lake.
  • You don't mention an engine so I'm assuming you are writing this all yourself. You can get the poly geometry from blender (e.g. export as an obj and parse the file yourself) but presumably you are loading in the lake geometry already to render it, so you can derive the navmesh from this. If you are using an engine it will probably have a mechanism of reading the geometry.

There are also point in concave polygon tests, however you might want something cheaper depending on the number of edges to the lake. You could e.g. grid up the lake and list the edges to check on edge grid squares, or whether a grid square is totally inside / outside the lake.

Also agree that having something more like bezier curves round the edge might be cool, and you may also want to take account of lake depth (boats might not be able to go right up to lake boundaries).

Share this post


Link to post
Share on other sites
Posted (edited)

Ok, wow and thank you. To clarify, I am writing this myself...no engine. If I did extrude the edges up but kept them invisible, I still don't know how I'd go about registering a collision. I've never heard of navmeshes or point in concave polygon tests, but I'd follow a good tutorial. If I could. 

I keep the whole lake loaded at once, it's not very much data. The edge is part of the mesh, not sure what you're alluding to.

What i want is simply for the boat to not be able to move past the lake. Just to stop. I don't need ultra realism, as long as it can move across the whole lake and never beyond it.  I may be able to do that grid strategy, I was hoping I could find the easiest fastest solution. Thanks for suggesting what to research. This is a very difficult thing to attempt, I am taking it slow.

Edited by Colby Ray

Share this post


Link to post
Share on other sites

Sorry I didn't notice this was in beginner section. However it is a good learning task, so don't be afraid to attempt. :) 

If the complexity (number of triangles) in the lake is low, then just using a simple point in polygon test will find whether a boat is attempting to move outside the lake. An example source code in c++ is here:

http://geomalgorithms.com/a03-_inclusion.html

However, this will mean the boat stops dead when it reaches an edge, instead of sliding, which is not too realistic. Even so it may be a good first test that you are able to load the lake data correctly.

Once this is working, if you understand some programming like arrays etc I would encourage you to try creating a navigational mesh (navmesh), as these are conceptually easy to understand and very powerful in games, and will allow you to slide boats at edges, and later perform more advanced stuff like pathfinding (maybe in a later game).

The first step in all cases in to load your polygons, I am not sure how you are doing this for rendering, but simplifying it to the case for triangles, you would end up with:

  • A list of vertex positions (x, y, z). In the case of a lake you can ignore the height coordinate and do things in 2d. This will usually be either y or z depending on your convention.
  • A list of triangles, with each triangle defined by 3 vertex indices (which can be used to look up the position in the first list).

Triangles will often share vertices, i.e. the index of a vertex will appear in more than 1 triangle. You can use this to identify neighbouring triangles, and to build the navmesh, as once a boat is within a triangle, it can only progress to neighbouring triangles.

When running the game, you keep track of which triangle the boat is within. If it crosses once of the edges it is either moving into a neighbouring triangle (in which case the move is okay), or trying to move into space outside the lake, in which case you can slide it against the edge of the triangle.

Once you have a boat moving on the navmesh, the next step is to shrink the navmesh a little to take account for the radius of the boat (so it doesn't poke onto land). You can either do this in blender (easier) or computationally in your game code.

This may sound complex, we don't know what level you are at, it maybe easier to start with a simpler game design, e.g. a grid based game. Another alternative is to use a third party physics engine to help, for instance Box2D. If you were using a physics engine you might send it the edges of the lake to be static colliders, then place a physics object for the boat within the lake, and push it around with forces.

Share this post


Link to post
Share on other sites

I'm using java not c++ so I don't think I can use any libraries? I'm keeping the mesh data stored client side, but I'm using drawarrays not drawelements (I always had trouble with texturing with draw elements). I guess I could grab the face index list when I'm parsing the obj, though. But if the point in polygon test doesnt need them I'll try that first. I'm curious how it works. Of course a navmesh sounds really cool now. I'll attempt it in the next couple days and keep checking and rereading. So thanks again!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!