Is btSphereShape cheaper for calculations than btBoxShape? - bulletphysics

Always thought sphere is the cheapest collision primitive but it looks like Bullet creates a convex body for the sphere, don't it? So is the btSphereShape actually cheaper than the btBoxShape for collision computations or much more expensive?

This is not a complete answer. (A complete one should do a profile test.)
From the official wiki, they are not so different in performance
(both shape are grouped in to the same tier - Primitives) :-
Using convex collision shapes such as convex hulls and primitives such
as a sphere, box and cylinder makes the collision algorithms much more
simple and efficient.
In the same page, it implies that Sphere is the fastest shape :-
btSphereShape : A very fast and simple shape.
btBoxShape :
A cuboid shape, the length in each dimension can be chosen arbitrarily.
Response to each mini questions
OP: btSphereShape actually cheaper .... , (because it is inherited) from btConvexInternalShape.
Yes, but the class hierchy is not the reason.
Both sphere and box are inherited from the btConvexInternalShape.
Sphere are just easy to calculate.
OP: ... Bullet creates a convex body for the sphere ... (so) the btSphereShape actually cheaper
Primitive shape uses special highly-customized algorithm, so it is cheaper than general convex. For an evidence, see btSphereSphereCollisionAlgorithm (cheap) vs btConvexConvexAlgorithm (expensive) .
General guide
In practice, you should select the one that match your need. (except your application is really physics-intensive)
As I browse the forum for a long time, there are no one mention/complain about difference in performance of Box vs Sphere.
I have also used both of them for a few years and see no difference in practice.
You may be also interested in collision-shape algorithm (a popular collision detection website).
I believe Bullet use one of such algorithms.

Related

I want to collition with an attack of sword or club of enemy

I am making a 2D platform game using cocos2dx and box2d.
And I am just now making of a player collitions with an attack of enemy.
An attack of enemy is such as a sword or a club, so I want to collition when a player is contacted with an edge of sword(or club) of enemy.
So far I made of a player collitions with a bullet using box2d, but I don't know that how to make the bounding box to an edge of sword(or club) of enemy like the following picture.
How are you making like this collision?
I believe that what you want isn't a bounding box but a collection of bodies, fixtures, and shapes to make a realistic enough simulation of the enemy and player interacting with each other.
To do that, you'll need to partition the pink lined shape (the enemy and its weapons) into a set of convex polygons — just like you'd need to do for the player.
Note that mathematically speaking, any concave shape can be expressed as a combination of convex polygons. From Wikipedia's entry for Concave polygon:
It is always possible to partition a concave polygon into a set of convex polygons.
If there's few enough entities you need to deal with this for, I'd just manually do the partitioning.
For one thing, it looks like you'd need to partition things manually anyway just in order to partition the ideally immutable parts from each other. Parts like the mace (or club), the forearm, the upper arm, etc where structures like metal, wood, or bone make a part rigid enough for the detail of the simulation you want to achieve.
Then you'll need to partition these rigid parts again to match the level of detail of the simulation that you want. So the mace's head, might be represented using something close to a pentagon shaped polygon. The mace's shaft, by just a rectangular polygon. Attach those two convex shapes to a single body for the mace. And so on.
You can use algorithms (or use implementations of those algorithms) to compute the sets of convex polygons that make up concave parts if you'd prefer. Unless you're going to do this for arbitrary shapes however, I'm not sure it's worth it.

AS3: How to intersect vectors at runtime?

Let's say I use the Graphics class at runtime to draw some vector shapes dynamically. For example a square and a circle.
Is there a way to create a new shape at runtime where those 2 vectors shapes overlap?
Those kind of operations are very common in all vector design programs such as Illustrator, Corel, etc... but I haven't found anything in Adobe's documentation, nor anywhere else, to do it by code.
Although drawing operations on the Graphics class are described in terms of lines, points etc this is - as far as you're concerned - just telling it what to draw onto a bitmap. There's no way to remove a shape once drawn, short of clear(), which just wipes the whole thing clean.
I don't fully understand why, as the vector data must be retained - there's no loss of quality on scaling after drawing, for example.
If you don't want to get into some hardcore maths (for anything beyond straight lines, you'll need to) there's an answer here which might help if you've ever used PixelBender:
How to calculate intersection between shapes in flash / action script ? (access to shape's segments and nodes?)
Failing that, if it's just cosmetic you could play around with masking shapes (will probably end up quite hacky though) - however, if you actually want to use the intersection to draw or describe a shape you will need to dig out your maths book or look for a good graphics library.
Hope this helps

Store a "routine" which, given some input, generates a 3d model

Well, it's the time of the year were I get busy on my next-generation, cutting edge, R&D project (just for the fun of it...and maybe some profit eventually).
This time, I've had a great idea for a service, which unfortunately I can't detail much.
However, a major part of this project is the ability to generate a 3d model out of certain input criteria. The generated model must be different on each generation.
As such, this is much different than the static models used in games - I think I will have to store actual code more than just model coords.
To give an example of some output:
var apple = new AppleGenerator();
apple->set_size_between(30, 50); // these two numbers are just samples...
apple->set_seeds_between(3, 8); // apple must have at least 3 seeds*
var apple_model = apple->generate();
// * I realize seeds may not be exactly part of the model, but I can't of anything else
So I need to tackle some points here:
How do I store these models as data?
Do you know of any tools that may help?
I need to incorporate a randomness factor (for example, the apples would have slightly different shapes each time)
I suppose math will play a good part here, but since these are complex shapes, it's going to be infeasible to cook up the necessary formulae for each model, right?
Also, textures must be relevant to each part of the model, as well as making the model look random (eg; I could be detailing a 40 to 60 percent red, and the rest green, for the generated apple).
This is in fact not a simple task. The solution varies a LOT depending on the complexity and variety of the objects you are trying to create.
Let's consider a few cases though:
Object is more or less known:
The most simple case is, to have a 3d model in the conventional way, and then randomize it a bit. Take the apple for example. The randomization can vary from the size of the apple to its texture colors to fruit damage.
All your objects can be described using NURBS surfaces:
In this case, you need to store enough data for the surface to be able to be generated, where of course this data can be randomized a bit.
Your objects have rotational symmetry:
In this case, generating a single curve and rotating it around the an axis can give you a shape. An apple is an example. You would need to store only the curve data and randomizing the shape could either be done on the curve (keeping symmetry) or on the final mesh.
On textures
This is way more complicated than the mesh generation. This is mainly because textures carry much more information than meshes (they are more detailed). You can have many texture generation strategies. In the case of your apple, you could select a few vertices, give them colors (one red, one green, another red etc) and interpolate the other vertex colors. This creates a smooth transition of colors which may look nice on an apple. If you are generating a knife however that just looks terrible.
In most cases, you need to be aware of which part of your mesh represents what, and generate the texture part by part. In the knife example above, you can generate the mesh in two steps; blade and handle each part's texture generated separately.
Conclusion
You can have a mixture of these of course. A meshGenerator class can take the data and based on whichever type they are, generates a mesh accordingly. Perhaps the first solution for object creation is the most suitable as any complicated object can be more easily defined by its triangles rather than NURBS.
Take a look at some of the basic architectural principles used to code Spore, the video game about evolving living creatures: http://chrishecker.com/My_liner_notes_for_spore
Here's an example of how to XML-serialize a mesh, along with some random morph behavior: http://www.ogre3d.org/tikiwiki/Morph+animation#The_XML_format_of_meshes_with_morph_animation
To make your apples all a bit different, you can apply a random transformation (or deformation). See for example: http://wiki.blender.org/index.php/Doc:2.4/Manual/Modifiers/Deform/MeshDeform
You want to use an established file format to avoid strange problems. It's more geometry than pure math. Your generate function would plot the polygons, and then your save method would interact with the formats.
https://stackoverflow.com/questions/441388/most-common-3d-model-format

Electrically charging edges in a force-based graph drawing algorithm?

I'm attempting to write a short mini-program in Python that plays around with force-based algorithms for graph drawing.
I'm trying to minimize the number of times lines intersect. Wikipedia suggests giving the lines an electrical charge so that they repel each other. I asked my physics teacher how I might simulate this, and she mentioned using calculus with Coulomb's Law, but I'm uncertain how to start.
Could somebody give me a hint on how I could do this? (Or alternatively, another way to tweak a force-based graph drawing algorithm to minimize the number of times the lines cross?) I'm just looking for a hint; no source code please.
In case anybody's interested, my source code and a youtube vid I made about it.
You need to explicitly include a term in your cost function that minimizes the number of edge crossings. For example, for every pair of edges that cross, you incur a fixed penalty or, if the edges are weighted, you incur a penalty that is the product of the two weights.

Breaking a concave polygon into convex ones

I'm using a game physics library (Box2D) which only supports convex polygon shapes. However, I'd like the level builder to be able to just specify concave polygons without having to worry about that.
So, how can I automatically break apart a concave polygon into convex ones (or even all triangles).
Speed would be cool, but ease of implementation is more important. The breaking apart will only be done on game initialization.
(My language is Flash/ActionScript 3, but that shouldn't matter)
Bernard Chazelle and David P. Dobkin presented an algorithm for that in 1985: Optimal Convex Decompositions.
Other approaches can be found on Wikipedia.
you probabaly need triangulation
This page explains how to convert polygons into non-complex shapes using ActionScript 3. The code is large so I wont copy paste here.
http://www.emanueleferonato.com/2011/09/12/create-non-convex-complex-shapes-with-box2d/