Posts Tagged ‘sample’

Posted by kent at 14 March 2011

Category: c++

Tags: , , ,

I always forget how to use boost::format when I need it. So, here are a few samples from when I did remember.

Only need is to include the proper boost header. No compile of any of the boost libraries is necessary.

The source is also available in subversion (webclient) (subversion checkout).


// build with make or:
//
// g++ -I../../boost_1_46_0 fmt.cpp
//

#include <iostream>
#include <sstream>
#include <boost/format.hpp>

using namespace std;
using namespace boost;

int main()
{
    stringstream ss;

    int year    = 2011;
    int month   = 3;
    int day     = 11;
    int hour    = 12;
    int minute  = 1;
    double seconds= 23.35;
    double epoch  = 1231234567890.35;

    // Fill with whitespace, right align
    ss << format("#1.0 %4i %4i %4i\n") % year % month % day;
    // Output: #1.0 2011    3   11

    // Fill with whitespace, left align
    ss << format("#1.1 %-4i %-4i %-4i\n") % year % month % day;
    // Output: #1.1 2011 3    11

    // Fill with zero (pad)
    ss << format("#2.0 %04i %04i %04i\n") % year % month % day;
    // Output: #2.0 2011 0003 0011

    // Floating point precision
    ss << format("#3.0 %11.2f\n") % seconds;
    // Output: #3.0       23.35

    // Floating point precision with zero-fill (zero pad)
    ss << format("#4.0 %011.2f\n") % seconds;
    // Output: #4.0 00000023.35

    // Left aligned string literal with 30+20 columns
    ss << format("#5.0 %-30s%-20s\n") % "LEFT ALIGNED 1" % "LEFT ALIGNED 2";
    // Output: #5.0 LEFT ALIGNED 1                LEFT ALIGNED 2

    // Right aligned string literal
    ss << format("#6.0 %30s%20s\n") % "RIGHT ALIGNED 1" % "RIGHT ALIGNED 2";
    // Output: #6.0                RIGHT ALIGNED 1     RIGHT ALIGNED 2

    cout << ss.str();

    return 0;
}

The output should be:

#1.0 2011    3   11
#1.1 2011 3    11
#2.0 2011 0003 0011
#3.0       23.35
#4.0 00000023.35
#5.0 LEFT ALIGNED 1                LEFT ALIGNED 2
#6.0                RIGHT ALIGNED 1     RIGHT ALIGNED 2
VN:F [1.9.13_1145]
Rating: 4.0/5 (1 vote cast)
Share

Posted by kent at 13 January 2007

Category:

Tags: , , , , ,

Collision Detection

To determine if two spheres are colliding, we take the sum of the radiuses and compare it with the length from the centers of the spheres. If the lenght is smaller than the sum of the radiuses, we have a collision.

Difference vector (the length is the distance between those two spheres):
vec{d} = s1.pos - s2.pos

Then the length is computed:
distance = vec{d}.length = sqrt{vec{d}.x^2 + vec{d}.y^2 + vec{d}.z^2}

Sum of the radiuses:
sumradius = s1.radius + s2.radius

If distance<sumradius the we have a collision to take care of.

Collision Response

This is the little more trickier part, but with some basics explained, it should be pretty straight forward.

First, find the vector which will serve as a basis vector (x-axis), in an arbiary direction. It have to be normalized to get realistic results.
vec{x} = s1.pos - s2.pos
vec{x}.normalize()

Then we calculate the x-direction velocity vector and the perpendicular y-vector.
vec{v1} = s1.vel
x1 = x.dot(vec{v1})
vec{v1x} = vec{x} * x1
vec{v1y} = vec{v1} - vec{v1x}
m1 = s1.mass

Same procedure for the other sphere.
vec{x} = vec{x}*-1
vec{v2} = s2.vel
x2 = vec{x}.dot(vec{v2})
vec{v2x} = vec{x} * x2
vec{v2y} = vec{v2} - vec{v2x}
m2 = s2.mass

Then we mix and play around with some of Newtons laws to obtain a formula for the speed (in vector format) after the collision.
s1.vel = vec{v1x}{(m1-m2)/(m1+m2)} + vec{v2x}{(2*m2)/(m1+m2)} + vec{v1y}
s2.vel = vec{v1x}{(2*m1)/(m1+m2)} + vec{v2x}{(m2-m1)/(m1+m2)} + vec{v2y}

And it actually works! :)

Sample application:

Source code in subversion repository.

VN:F [1.9.13_1145]
Rating: 5.0/5 (3 votes cast)
Share