# Tag: physics

Entries for tag "physics", ordered from most recent. Entry count: 3.

Warning! Some information on this page is older than 6 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

Pages: 1

# Ball Physics

Sat
17
Mar 2012

Checking collision between different kinds of 2D or 3D shapes is a subject I deal with for some time. It is useful in game development to determine if some object is visible or affected by light and this if it should be rendered. I have lots of function to check such collisions in the math module of CommonLib library.

But that is only the beginning if you want to make physics. Adding physical behavior to your game requires additional calculations to correct positions and apply forces to colliding bodies. I prepared a small code snippet that implements physical 2D collision between moving circle and another moving circle (calculated by CircleToCircleCollision function), static line (CircleToLineCollision function) and static axis-aligned rectangle (CircleToRectangleCollision function).

The code uses some good practices (like fixed time step) as well as bad practices (like Euler integration). It should be enough for a simple physics in game like a platformer.

# The Concept of Immutability and Descriptor

Fri
27
Nov 2009

An object of some class represents a piece of data, chunk of memory or other resource along with methods to operate on it. It should also automatically free these resources in destructor. But how should modifying these data look like? There are two possible approaches. As an example, let's consider a fictional class to encapsulate Direct3D 9 Vertex Declaration (I'll show my real one in some future blog entry). A Vertex Declaration is an array of D3DVERTEXELEMENT9 structures, which can be used to create IDirect3DVertexDeclaration9 object. First solution is to define class interface in a way that data inside can be modified at any time.

```class MyMutableVertexDecl
{
public:
// Creates an empty declaration.
MyMutableVertexDecl();
// Frees all allocated resources.
~MyMutableVertexDecl();

// Copies data from another object
void CopyFrom(const MyMutableVertexDecl &src);
// Deletes all internal data so object becomes empty again.
void Clear();
bool IsEmpty() const;

// I/O (Serialization)
void SaveToStream(IStream &s) const;

size_t GetElemCount() const;
const D3DVERTEXELEMENT9 & GetElem(size_t index) const;

// Modification of underlying array
void SetElem(const D3DVERTEXELEMENT9 &elem, size_t index);
void InsertElem(const D3DVERTEXELEMENT9 &elem, size_t index);
void RemoveElem(size_t index);

IDirect3DVertexDeclaration9 * GetD3dDecl() const;

private:
std::vector<D3DVERTEXELEMENT9> m_Elems;
IDirect3DVertexDeclaration9 *m_D3dDecl;
...
};```

This approach seems very nice as you can create your object any time you wish and fill it with data later, as well as change this data whenever you need to. But at the same time, a question emerges: when to (re)create "destination" IDirect3DVertexDeclaration9 from the "source" D3DVERTEXELEMENT9 array? Each time the array is modified? Or maybe each time the IDirect3DVertexDeclaration9 is retrieved? Optimal solution for the interface above would be to do lazy evaluation, that is to recreate IDirect3DVertexDeclaration9 whenever it is retrieved for the first time since last time the D3DVERTEXELEMENT9 array have been modified. But...

Comments | #software engineering #directx #.net #physics #c++

# Learning PhysX

Fri
18
Sep 2009

Yesterday I've started learning NVIDIA PhysX. It's the first time I use a physics engine at home, but it seems quite easy for me. I like the PhysX library as it has pretty, object-oriented API.

Is using a physics engine really an advanced topic? Now I don't think so. Sure there are some scary looking terms like "inertia tensor" or "angular damping", but all you need for the start is to know a bit about vectors, matrices, quaternions and some school-level physics like the concept of mass, velocity and force.

PhysX is very powerful. It can simulate soft bodies, cloth, force fields, fluids and much more. But the foundation is just rigid body physics plus character controller. In fact it is so simple and powerful at the same time that I now think every beginner game programmer should learn this. Physics in games is not only to simulate boxes and ragdolls of killed enemies. Using such physics engine is many times easier than implementing algorithms for normal collision detection, sliding along walls, walking on the height field, checking if an object entered a trigger or casting rays. PhysX offers it all!

So how to start learning PhysX if you are interested in this subject? All you need to do is to go to the NVIDIA PhysX website, download and install the PhysX SDK. It includes all necessary files like headers, libraries, DLL-s, documentation and tutorials. I learn from the included tutorials in DOC format which can be found in the TrainingPrograms\Docs\ subdirectory.

Here is a small screenshots from my today experiments: