3D collision detection with MatGeoLib? - c++

I am writing a plug-in in C++ for some dedicated server software which hosts servers for a game.
I looked around for 3D Collision libraries but it's really hard to find any with examples, but I decided to use MathGeoLib because it looks promising - but it lacks examples and I am unable to find any.
So my question is: How would I define a sphere and a cube, a line (Point+direction) and then get the Position XYZ of the first collision which the line encounters?
The documentation only shows the classes and which methods they have. But nothing show how to start using MathGeoLib. Are there any tutorials which are not findable on google?
Background information on my project:
I am making a collision detector for San Andreas Multiplayer, the server has no information whatsover on the game world so I decided to extract the collision files and object placement files and convert them to a usable format for my plug-in.
The objects have a position XYZ and quaternion rotation XYZW and the collision files, well, have lots of stuff in them [a project member is writing a parser for those .col files for the project].
The project goals are to provide a mechanism to determine the Z position based on the XY position and to provide a ray-tracer which tells you where a line (StartPos,EndPos) intersects (hit XYZ) in the game world.
This is why I need to know how to accomplish this with MathGeoLib. I'm going to load all the object collisions into one world and then execute the ray tracer functions. (Amount of objects is around 30,000 in a 6000x6000x1000 area)

Check out
http://bulletphysics.org/wordpress/
Bullet is an open source physics engine and comes with lots of example code. In particular you want to use the rayTest() method of a dynamics world to cast a ray and return the nearest collision point. To summarize you'll need to; create a dynamics world, load your sphere and cube data into it, and then call rayTest(startPoint, endPoint, resultCallback);
Assuming you haven't used bullet before start with the "hello world" example code to see how you can easily create a dynamics world and add rigid bodies to it.

Related

Basic 3D OpenGL collision detection C++

I'm currently in the process of developing a very basic 3D OpenGL game in C++ as part of a small college project. We don't get a lot of insight from the teachers however, and only very limited documentation, as well as a small timeframe, so I'm kind of a little lost here at the moment.
My game is a tank battle on an orthogonal plane that pretty much looks exactly like the image I sketched below. Each tank (A and B) can be controlled by a different player, and each one can shoot projectiles, which are supposed to influence the other tank's score upon collision.
My question is, what would be the simplest way of effectively implementing collisions for the tanks? (Tank vs tank, tank vs map boundaries and tank vs any kind of parallelepipedic object like the one in the center of the picture - and the same thing but applied to the projectiles shot from the tank turrets).
Ideally, without the need of using an external physics engine, but also accepted if the implementation can be done easily. At the moment, I'm solely using the GLUT library.
Download and integrate Box2D (http://box2d.org) into your project.
Unless your project is to implement a physics engine, then don't bother doing it yourself. Your time will be much better spent learning how to integratate libraries and how proper physics engines work.
You can then easily use a box collider for your tanks, circle for projectiles and 4 lines for your perimeter. You can create callbacks to notify you when a projectile has collided with another tank.
You will have to use forces and torques to move and rotate your tanks, rather than just updating their positions. But you would probably have to do that anyway if you were going to implement the physics yourself.

Cocos2d/Box2d. How to generate a physics body from a tilemap .tmx?

I am new to game development.
What I'm trying to do is load the .tmx tilemap I've created from Tiled to my game as a physics body(ies), I'm using C++ and Boxd2. My tilemaps show only the tiles I want to be collideable, like the floor and some platforms.
It's important to note that i'm programming for windows and all documentation I found were about IOS or for an older version, some functions like b2BodyDef bodyDef doesn't seem to work.
I am currently doing what you are describing in my latest game, the only difference being that I use cocostudio to build everything in my game, including my maps. The reason I did this was so that I can have control over everything I needed, I use pre-defined naming conventions to indicate physical edges which I then read up and determine its dimensions and construct my edge bodies.
High-Level overview
Before continuing here are the assumptions I am making about your .tmx tilemap. You have unique identifiers for the objects you would like to create as a physical object
(something in it's name that you can use to pull it out and inspect it).
Read in the .tmx file and give it to your fileParser.
Get all the "Nodes", "Sprites" that have your physical body identifier in it.
Since you are using a tile map I'm assuming that all your collision areas wil be square.
Give the list of nodes to your PhysicsBodyFactory and use the following code to construct the bodies you require.
pseudo-ish code below
auto tileNode = listOfNodes.at(nodeIndex);
b2BodyDef tileBodyDef;
tileBodyDef.type = b2_staticBody;
tileBodyDef.position.Set(tileNode->getPositionX() / Pixel_To_Meter_Ratio,
tileNode->getPositionY()/ Pixel_To_Meter_Ratio);
auto b2PolygonShape tileShape;
tileShape.SetAsBox(tileNode->getBoundingBox().size.width / Pixel_To_Meter_Ratio,
tileNode->getBoundingBox().size.height / Pixel_To_Meter_Ratio);
auto tileBody = physicsWorld->CreateBody(tileBodyDef);
// This can be used later on when you want to have different reactions for different collisions.
tileBody->SetUserData("CollidableTile");
This will create and place these object in your physics world, allowing other bodies in there to collide with it. If your player has its own body in the world, the basic collision will be handled for you.
I am writing a blog post on using cocostudio as more than a UI tool, which will cover this exactly. I hope this helps, please let me know if this solution is what you are looking for.

SDL Tile and Sprite Rendering Terrain

Hello recently i started to mess around with SDL. Since i was interested in some 2D/2.5D games.So i started messing around with SDL in C++, I was looking to recreate something similar to Original Zelda.
So as far as i understand those game work with some kind of isometric prespective, or standard Orthogonal view but one thing i do not understand is how can you generate 3D-like Collisions between those objects on the map (tiles, sprites etc which are in 2D). Have a look at the video link below. Is this created purely in SDL, is it PerPixel collision or rectangular ? Or it might involve OpenGL as well ?
Link: https://www.youtube.com/watch?v=wFvAByqAuk0
The original was probably a simple Rectangular collision.
I believe that your "3D collision" is the partial collision present in some objects. For example, Link can go through the leaves, but not through the trunk.
You can do it easily in 2 ways:
Layers of rendering and collision. The trunk is located in one layer and is covered by some collision boxes. Link is present in a intermediary layer. And the leaves are in another layer, on top of Link. Then you can check collision between Link's Layer and the layer with the trunk and other objects, for example.
Additionally you can create a property for your tiles in which you can store the type of collision you hope to obtain. For example, 'box' collision will tell your engine that the object is collidable on every side. Or 'bottom' collision will tell your engine that Link will collide with this object only if he is walking down into the object (this is the effect of you will see on some 2D sidescrollers: jump through a tile but then fall into it solid.
Per pixel collision in those simple cases is not worth it. I find it much better to personalize the collision ourselves, using creativity, masks and layers.
BTW: This topic would fit better on https://gamedev.stackexchange.com/

Box2d - how to grab and throw objects

Say there are 3 boxes on the screen, how can I go about touching one of them to pick it up and "throw" it at the others? I have the rest of the world implemented but can't find much information on how to grab/drag/toss physics objects. Any sample code or documentation out there that would help with this?
It depends what you are attempting to do. It is a physics simulation and as such a typical way of interacting with the system is by applying forces to objects opposed to direct manipulation of the x,y coordinates. But you can in fact do either. I believe the most common approach is to use a mouse joint. A google search on b2MouseJoint will show the documentation and several examples including this one.
http://muhammedalee.wordpress.com/tag/b2mousejoint/

2D collision detection and stuff with OpenGL

I am working on a simple 2D openGL project. It contains a main actor you can control with the keyboard arrows. I got that to work okay. What I am wanting is something that can help explain how to make another actor object follow the main actor. Maybe a tutorial on openGL. The three main things I need to learn are the actor following, collision detection, and some kind of way to create gravity. Any good books or tutorials to help get me in the right direction would be great.
You could use a physics library like Chipmunk Physics, which lets you attach springs and things between the two objects and detect when they hit each other and other things.
A pre-rolled library would be good, but the concepts you describe are ones you need to know if you are going to do any sort of game programming anyways:
A simple way to make one actor follow behind another is to have the lead actor store its position every time it moves. Feed these positions to a trailing actor with a delay of a few values - the longer the delay, the further behind they travel. Simple, but doesn't handle dynamic collision (other actors moving the block collision.)
Collision detection in 2D can simply be axis aligned (AA) bounding boxes. Search for this and you'll see the 4 ifs or so that are needed.
Gravity is just adding a fixed velocity (usually down) to every object every game loop. This is constant acceleration which is exactly how gravity works.