Dimitrios Christopoulos - Collision detection tutorial

Здесь есть возможность читать онлайн «Dimitrios Christopoulos - Collision detection tutorial» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

Collision detection tutorial: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «Collision detection tutorial»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

Collision detection tutorial — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «Collision detection tutorial», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

//render/blend explosions

glEnable(GL_BLEND); //enable blending

glDepthMask(GL_FALSE); //disable depth buffer writtes

glBindTexture(GL_TEXTURE_2D, texture[1]); //upload texture

for(i = 0; i < 20; i++) //update and render explosions

{

if (ExplosionArray[i]._Alpha >= 0) {

glPushMatrix();

ExplosionArray[i]._Alpha -= 0.01f; //update alpha

ExplosionArray[i]._Scale += 0.03f; //update scale

//assign vertices colour yellow with alpha

// colour tracks ambient and diffuse

glColor4f(1, 1, 0, ExplosionArray[i]._Alpha);

//scale

glScalef(ExplosionArray[i]._Scale, ExplosionArray[i]._Scale, ExplosionArray[i]._Scale);

// translate into position taking into acout the offset caused by the scale

glTranslatef((float)ExplosionArray[i]._Position.X() / ExplosionArray[i]._Scale, (float)ExplosionArray[i]._Position.Y() / ExplosionArray[i]._Scale, (float)ExplosionArray[i]._Position.Z() / ExplosionArray[i]._Scale);

glCallList(dlist); // call display list glPopMatrix();

}

}

Sound

For the sound the windows multimedia function PlaySound is used. This is a quick and dirty way to play wav files quickly and without trouble.

Explaining the Code

Congratulations…

If you are still with me you have survived successfully the theory section ;)

Before having fun playing around with the demo, some further explanations about the source code are necessary.

The main flow and steps of the simulation are as follows (in pseudo code).

While (Timestep != 0) {

For each ball {

compute nearest collision with planes;

compute nearest collision with cylinders;

Save and replace if it the nearest intersection in time computed until now;

}

Check for collision among moving balls;

Save and replace if it the nearest intersection in time computed until now;

If (Collision occurred) {

Move All Balls for time equal to collision time;

(We already have computed the point, normal and collision time.)

Compute Response;

Timestep -= CollisonTime;

} else Move All Balls for time equal to Timestep

}

The actual code which implements the above pseudo code is much harder to read but essentially is an exact implementation of the pseudo code above.

// While time step not over

while (RestTime > ZERO) {

lamda = 10000; //initialize to very large value

// For all the balls find closest intersection between balls and planes/cylinders

for (int i = 0; i < NrOfBalls; i++) {

// compute new position and distance

OldPos[i] = ArrayPos[i];

TVector::unit(ArrayVel[i],uveloc);

ArrayPos[i] = ArrayPos[i] + ArrayVel[i] * RestTime;

rt2 = OldPos[i].dist(ArrayPos[i]);

// Test if collision occured between ball and all 5 planes

if (TestIntersionPlane(pl1, OldPos[i], uveloc, rt, norm)) {

// Find intersection time

rt4 = rt * RestTime / rt2;

// if smaller than the one already stored replace and in timestep

if (rt4 <= lamda) {

// if intersection time in current time step

if (rt4 <= RestTime + ZERO) if (! ((rt <= ZERO) && (uveloc.dot(norm) > ZERO)) ) {

normal = norm;

point=OldPos[i] + uveloc * rt;

lamda = rt4;

BallNr = i;

}

}

}

if (TestIntersionPlane(pl2, OldPos[i], uveloc, rt, norm)) {

//…The same as above omitted for space reasons

}

if (TestIntersionPlane(pl3, OldPos[i], uveloc, rt, norm)) {

//…The same as above omitted for space reasons

}

if (TestIntersionPlane(pl4, OldPos[i], uveloc, rt, norm)) {

//…The same as above omitted for space reasons

}

if (TestIntersionPlane(pl5, OldPos[i], uveloc, rt, norm)) {

//…The same as above omitted for space reasons

}

// Now test intersection with the 3 cylinders

if (TestIntersionCylinder(cyl1, OldPos[i], uveloc, rt, norm, Nc)) {

rt4 = rt * RestTime / rt2;

if (rt4 <= lamda) {

if (rt4 <= RestTime + ZERO) if (! ((rt <= ZERO) && (uveloc.dot(norm) > ZERO)) ) {

normal = norm;

point = Nc;

lamda = rt4;

BallNr = i;

}

}

}

if (TestIntersionCylinder(cyl2, OldPos[i], uveloc, rt, norm, Nc)) {

//…The same as above omitted for space reasons

}

if (TestIntersionCylinder(cyl3, OldPos[i], uveloc, rt, norm, Nc)) {

//…The same as above ommited for space reasons

}

}

// After all balls were tested with planes/cylinders test for collision

// between them and replace if collision time smaller

if (FindBallCol(Pos2, BallTime, RestTime, BallColNr1, BallColNr2)) {

if (sounds) PlaySound("Explode.wav", NULL, SND_FILENAME|SND_ASYNC);

if ( (lamda == 10000) || (lamda > BallTime) ) {

RestTime = RestTime - BallTime;

TVector pb1, pb2, xaxis, U1x, U1y, U2x, U2y, V1x, V1y, V2x, V2y;

double a,b;

...

code omitted for space reasons

the code is described in the Physically Based Modeling section under sphere to sphere collision

...

// Update explosion array and insert explosion

for (j = 0; j < 20; j++) {

if (ExplosionArray[j]._Alpha <= 0) {

ExplosionArray[j]._Alpha = 1;

ExplosionArray[j]._Position = ArrayPos[BallColNr1];

ExplosionArray[j]._Scale = 1;

break;

}

}

continue;

}

}

// End of tests

// If collision occured move simulation for the correct timestep

// and compute response for the colliding ball

if (lamda !=10000) {

RestTime -= lamda;

for (j = 0; j < NrOfBalls; j++) ArrayPos[j] = OldPos[j] + ArrayVel[j] * lamda;

rt2 = ArrayVel[BallNr].mag();

ArrayVel[BallNr].unit();

ArrayVel[BallNr] = TVector::unit( (normal * (2 * normal.dot(-ArrayVel[BallNr]))) + ArrayVel[BallNr] );

ArrayVel[BallNr] = ArrayVel[BallNr] * rt2;

// Update explosion array and insert explosion

for (j = 0; j < 20; j++) {

if (ExplosionArray[j]._Alpha <= 0) {

ExplosionArray[j]._Alpha = 1;

ExplosionArray[j]._Position = point;

ExplosionArray[j]._Scale = 1;

break;

}

}

} else RestTime=0;

} //end of while loop

The main global variables of importance are:

Represent the direction and position of the camera. The camera is moved using the LookAt function. As you will probably notice, if not in hook mode (which I will explain later), the whole scene rotates around, the degree of ratation is handles with camera_rotation. TVector dir; TVector pos(0,-50,1000); float camera_rotation=0;
Represent the acceleration applied to the moving balls. Acts as gravity in the application. TVector accel(0,-0.05,0);
Arrays which hold the New and old ball positions and the velocity vector of each ball. The number of balls is hard coded to 10. TVector ArrayVel[10]; TVector ArrayPos[10]; TVector OldPos[10]; int NrOfBalls=3;
The time step we use. double Time=0.6;
If 1 the camera view changes and a (the ball with index 0 in the array) ball is followed. For making the camera following the ball we used its position and velocity vector to position the camera exactly behind the ball and make it look along the velocity vector of the ball. int hook_toball1=0;
Self explanatory structures for holding data about explosions, planes and cylinders. struct Plane struct Cylinder struct Explosion
The explosions are stored in a array, of fixed length. Explosion ExplosionArray[20];

The main functions of interest are:

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «Collision detection tutorial»

Представляем Вашему вниманию похожие книги на «Collision detection tutorial» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Oskar Andreasson - Iptables Tutorial 1.1.19
Oskar Andreasson
Barrington Bayley - Collision with Chronos
Barrington Bayley
Zoë Archer - Collision Course
Zoë Archer
Jeff Abbott - Collision
Jeff Abbott
libcat.ru: книга без обложки
Неизвестный Автор
libcat.ru: книга без обложки
Неизвестный Автор
Борис Акунин - Квест. Tutorial
Борис Акунин
Сергей (Sergey) Зайцев - Brief public speaking tutorial
Сергей (Sergey) Зайцев
Отзывы о книге «Collision detection tutorial»

Обсуждение, отзывы о книге «Collision detection tutorial» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x