welcome to my first real ballet physics
tutorial and in this video I'm going to
create a simple ballet physics program
program the base program basically which
I'm going to use later on so it will
have nothing too fancy for now so it
will have one plane on the bottom of the
map and there will be one or two objects
for now so probably a sphere sphere and
uh maybe a box I don't know so it will
be a really simple program for now and
then let actually let's try uh to create
it first of all before I go on I would
like to just show you what I have here
already this is my main openg G program
which I'm always starting off if I start
with a new openg G kind of 3D Library uh
I have a camera because it will be
relatively boring without camera by the
way you can follow along with for
example direct X as well but you have to
write this kind of stuff yourself or the
camera and uh and I commented out the
Skybox I don't we don't really need a
Skybox right now so so I Have Nothing
Fancy in here just clear the screen
update the camera position and uh that's
pretty much all and also I have a yeah I
have nothing more in this actually let
me delete this kill Sky Box as well okay
so this is my main openg G program which
I'm going to start with we don't really
actually need this Collision or
Collision plane and just left it here
accidentally we don't really need this
function either so what we really need
is basically the camera and of course
then to know the basically how the openg
G works so this is kind of PR request
the things all right uh so I think let's
start it and first of all I want to show
you how you can draw a a sphere very
simple in openg GL using Glu library
with Glu quadri although this is a bit
outdated but uh for demonstration
purposes this is the simplest way to
draw a sphere without having to go on
manually setting the vertices and the
faces so we first of all need to create
an object which is called Glu quadric
quadric
obj and this is actually a pointer and I
just call it quad okay and here
somewhere for example in the init
function let's let's initialize it to
initialize it just use the function GL
new quadric so quad equal to GL new
quadric that righted simple function
return with a pointer to a GL quadric
and so we should delete it at the end of
the program so right here I just use the
Glu delete quadric and quad so this is
kind of just openg GS stuff nothing
ballet physics right here okay and then
we can draw a circle for uh Sphere for
example with Glu sphere function and
this Glu sphere function waiting for the
quad as the first parameter the radius
has the second parameter so 1.0 for
example and then the horizontal and the
vertical basically resolution so make it
303 and now in theory we should be able
to compile compile this project then
normally as we would compile an openg G
project and run this and
uh and after set the color to some color
which we can see GL color 3F for example
let's make it red we can see our our
sphere so if I run the program right now
the sphere will be there the in the 0000
0 coordinate of course you can use
translate and rotation to move wherever
you want it to be moved okay you can set
the radius obviously the resolution of
the sphere basically the vertical and
the horizontal resolution the higher
number the more sphere likey it is or
you can create other things like
cylinders by using the function
Glu cylinder the first parameter again
is the quadric the second third and
fourth parameter are the extent of the
this cylinder so for example 1.0 1.0 and
5.0 and the next parameter is the
horizont uh is the
resolution uh the resolution so how many
uh each of the circles on the bottom and
the top how many vertexes there are and
finally in the top H so uh so in the
vertical horizontally how many
subdivisions there are
so so like that here is the
cylinder and
uh okay cylinder and the first parameter
is how well this circle is so how many
vertexes there are in this in this
circle and next is so the last parameter
is how many subdivisions there are
between the top and the bottom so for
example there could be one here then you
should write two or there could be
subdivisions like this and you should
write three or maybe three to something
okay so if I run this program right now
then it
should if I write a d right here then it
should compile and draw a cylinder for
us okay uh right there somewhere oh
there as you can see it is a cylinder 1
by 1X 5 cylinder of course you could
make this second one to zero and make a
cone instead of a
cylinder and now we have a a cone as you
can
see okay so these are few basic shapes
which you can draw with this Glu cadri
very simple without having to load an
obj file or using very complicated
mathematics to draw this I just wanted
to show you as a preview uh because we
will later on need to create these kind
of shapes but now let's go into the
actual ballet physics stuff before we
get into way too much open because
ballet physics super suppos to be a kind
of platform and Library independent so
as long as you can run C++ code and you
have any kind of 3D Library you should
be okay with bullet physics so uh let's
go into the buet physics first of all we
need to create a word actually I will
not create the word right here I will
just create a pointer right here in
globally so I can access it from the
init from the display and from other
functions so that's why I will going to
make a few variable Global yeah I know
global is not the best way to put
variables but for now I this is the
simplest way later on we will make a
class for it because we need a lot of
dynamic allocation and dynamic location
and the class Constructor and Destructor
will easily handle this but for this
simple first example let's just make it
Global so we will create a BT Dynamic
Dynamics word this is our kind of word
which we are going to create if you go
here to the not here this is not that uh
but here in the
to your ballet physics folder and to the
source code ballet Dynamics and Dynamics
here you will find all of the different
kind of bir which you can create and and
we are going to create a BT discrete
Dynamic word this discrete Dynamic word
is pretty much discrete because it has
discrete steps and dynamic word and it
is inherited from this BT Dynamic word
so thanks to polymorphism we can store
the this create Dynamic word in here is
the dynamic word so if I open up this
file you can see that this is indeed
inherited from the BT Dynamics world and
it has a lot of stuff in it we are only
interested in the Constructor for now as
you can see it's waiting for exactly
four par parameters and this seems very
scary at first because it has long names
but actually they are very easy to
create and basically we need just create
a variable and just set their default
both values so I will show you that in a
moment so in the openg g. CPP we create
a BT Dynamics word by the way all class
names in ballet physics starts with the
letter BT uh like ballet and then the
name of whatever class we have with all
capitalized first first letters and not
capitalize rest of the letters and
usually in bot physics every class has a
very long descriptive name like BT
discret Dynamic word it's a very long
and descriptive name this if you Pro if
you know uh a little bit of uh I don't
know physics or guess English then thisr
pretty much mean that it's uh it's not
continuous so for example every 160th
second it's make a collision check like
we have in our FPS game okay so the BT
Dynamic Dynamics word and I just call it
word the next is actually we need these
four kind of different variable one of
all of them so a dispatcher so BT
dispatcher and I just call it dispatcher
we don't really need to go into what
each of these does I will just uh give
you a basic idea so this BT dispatcher
you can set basically the Collision
algorithm between each of the stuff
again if you go here into this ballet
Library you will find the manual right
here somewhere right here the bullet
manual and here you can find
a a them
po table here it is which basically set
what Collision algorithm should be used
so for example between a box and a box
the box box Collision detection should
be used between the box and the sphere
the sphere box Collision detection
should be used so it is logically filled
in there is no reason to change any of
this so we will just use the default
later on I will initialize in the init
function as the BT default Collision
dispatcher okay the next thing is we
need the broad phase so the broad phase
is pretty much have to do the Collision
algorithm between the objects because we
can use the way we would use in uh in
normal so as we used in the SPs game so
every single object with every single
other object so if there are spheres
like this then we will just go through
every single object with double for Loop
and with n Square so I don't know how
many uh how many are here eight then
with 64 steps we have checked with every
s every single possible pair but there
are much more efficient ways like using
a quad three which is called BT dbvt
broad phas I guess I believe so so here
for example separate stuff up like that
and we don't have to check everything
just the objects which are in one uh
grid basically so for example if there
are two on here then we just have to
check this two we know that for example
between this object and this object
there could be no Collision because they
are way far away so we don't have to
care about that there is a convenient
class for this as well so so we uh so we
just initial initiate that class
instantiate that class and we don't have
to worry about that so BT broad phase
interface and I just call it broad
phase okay the next is the constraint
solver this is pretty much what
calculates everything so it's calculate
how much force it should apply after
collision and so on so we don't really
have to care about that as well either
because there are the default uh default
constraint solver and that will just
work as well so BT con constraint solver
and I just call it solver whatever okay
and one last thing is remaining if you
look at the header file the Collision
configuration it again has something to
do with the Collision dispatcher you can
fine-tune the algorithm which should be
used uh for Collision DET uh for
Collision uh uh detection and respon so
just create a BT
Collision uh
configuration
pointer configuration pointer and I just
call it
Collision config all right we have that
so we initial uh so we have the pointer
for all of these and actually let's fill
them in immediately in the init function
so let's first make the Collision
configuration so Collision config and
just set it to the default because as I
said the defaults are working very well
there is no absolutely no need to
actually set uh anything other than the
default except if you want to use
something very fancy okay so BT default
Collision config config figuration as I
said there are very there is very long
name actually uh we should use a new
okay so it has a very long name and I
will dynamically allocate that the names
are pretty descriptive so this is the
default and as I said there is no need
to use any other okay let's make the
second as the dispatcher so
dispatcher and this is equal to the new
BT
Collision dispatcher okay and just pass
the first so this Collision config into
here as a parameter and you are pretty
much done with this one as well so again
the default is very good there is
absolutely no need to change anything in
this
dispatcher like you you can you could
change the algorithm which could be used
here so I don't know use spere sphere
Collision detection instead of box box
to Collision detection or something the
next thing is to initialize the broad
phas so broad face and just again use
the default uh use the dbvt broad phase
so
dbbt broad phase and actually with not
capitalize P like that okay this dbvt
broad phase is pretty much that tree I
showed you right here in game so it's
pretty much divide the space into uh uh
different kind of uh so in different
sizes and then it really uh makes the
Collision detection much less expensive
there are two other kind of things which
you can which is inherited from this BT
broad phas interface there is a single
simple broad phas or similar to that I
have not used that this is the
everything with everything algorithm
like we used in the fpf game so double
for Loop and go through every single
possible pair and check for Collision
this is is not too effective I don't
recommend you to use it the other thing
is there is this XIs sweep or something
like that I have not used that either in
the Demos in the ballet physics library
right here the de uh in the demos it is
often used this is the AIS sweep uh the
disadvantage that you have to know the
size of your Maps so of your uh of your
word at previously I not going to use
that but you can use that if you want to
so just check the demo if you want to
know how to use it basically just again
in uh instate a new class uh with
uh with this btx sweep uh free class
right here so just uh instant a class
and it will work as you would uh wait in
theory it has a bit better performance
than the BVT broad phas but it is
working okay and one last thing is
remaining before we can create our word
is to make the solver so solver equal
again I'm going to use one of the
defaults which is BT
sequential sequential
impulse
constraint solver okay it has a very
long and scary name but don't worry we
just have to in in instantiate it and
it's it will work as we would expect
okay there there could be other thing if
you want to use the open C or something
to do um to do multithreading but for
now this will be all right okay finally
after this free initialization we can
make the word so these are pretty much
the default values and we will not
change any actually we will change some
of them later on where we'll use the
soft body but uh other than that it's
pretty much the same so don't worry
about this too much okay so let's create
the word new BT uh
discrete Dynamic word okay so this is it
and we have to just pass the parameter
in the right order so first the
dispatcher and then the broad
phase broad phase and after the broad
phase the constraint
solver so solver and finally we have to
pass the Collision configuration and
pretty much that's it we have the word
okay and one thing which you will always
do after you created the word is set the
gravity so set the gravity is pretty
simple just set gravity with just call
this function and here it's waiting for
the a vector it's it's like a vector 3D
object which we have created for the FPS
game except this is in ballet physics
and this is called BT Vector 3 this is
as I said it's almost identical to the
vector 3 D class which we have created
The Constructor waiting for free
parameter for the X Y and Z coordinate
so make 0 minus 10 0 this is about the
gravity on Earth about 10 m m/ second in
down so that's why the m sign or there
okay and we have actually have the word
and in here in the update function for
example right here in the main we can
call the step simulation function for
the word so word and step Sim
simulation and this is waiting for the
time elaps since the last frame I have
60 frame per second right here I just
set it to 60 instead of 30 which was
earlier and if we have 60 frame in a
second then we have 1 ided by 60 uh time
between each frame so this is the Els
time since the last frame of course you
can calculate the alap time by using
this thought variable right here and
just calculate how much time it has been
alabs an important thing right here you
should give it as a uh seconds so not
milliseconds right here like here like
as sdl delay waiting for milliseconds
the St simulation oops that's wrong
function actually
here the SE simulation is actually
waiting for waiting the alap time in
seconds all right that was it we have a
working simulation already one thing or
though is that we do not have anything
to simulate we should add a few stuff
and actually we should delete that lot
of stuff which we have created up there
so just delete the
dispatcher and then delete the Collision
config then delete the what El did we
have the solver delete the word and
delete the broad
phase okay so we have to delete all of
this stuff because we have dynamically
allocated it and as you probably know
it's a very bad thing not to delete
stuff which we have dynamically
allocated okay so right here in the init
we already have stuff now we should
actually let's add a static object so
for example ground for now the ground
will be a simple plane of course later
on you can set it as a height map and
other things but the simple plane should
do the trick right now now I don't I
will not create a function for this
because there will be only one plane
this is kind of infinite plane it is
only for testing and debugging purposes
in ballet physics but this is one of the
simplest thing we can create and then
let's start it so first of all we need
to define the position which you are
going to use to to define the position
we have to use a simple structure called
B BT transform this BT transform is
pretty much an orientation and the
position so basically it contain a
rotation in a qu neon which is it's you
can think of it as a 3D Matrix and a
vector which uh containing
the uh position okay so first of all we
should in we should set the identity so
T do set identity so it's pretty much
set back the position to 0000 0 the
orientation to 0 01 which is for quatum
pretty much meaning there is no rotation
at all okay right here we should set the
position to T do set origin this is a
function where you can set the origin so
basically this is the position where you
want the plane and we just put it to 0 0
0 coordinate there will be a it will be
a good place for that okay the next
thing which we are going to do is
basically set the shape which we are
going to use later on for example for
spheres and boxes you can you have to
use different kind of shapes for example
for spheres you have the BT sphere shape
for box you have the BT box shape so
just logically for the planes you have
the BT static plane shape okay this is
the shape for a plane okay and it's a
static plane it's an infinite plane
basically not planes like we did in um
uh the FPS game so this is infinitely in
every direction okay so you have to
allocate it dynamically because uh the
rigid body need it as so need it to be
exist and if it is not located
dynamically then the end of this init
function this is pretty much destroyed
and we will get a few errors or crashes
basically so let's just make a new BT
static plane shape okay this function
actually waiting for two parameter a
vector which is defining the normal
Vector of the plane so BT BT the vector
three so the normal Vector this is 0 1 0
so basically this plane is looking
upward and then we need to set the uh a
number as the second parameter a BT
scolar basically a BT scolar you will
see that in a lot of demos if you check
out the demos it is probably it is a
float or double depending if you have uh
compiled it as a double Precision
library then this is a double as it is a
float I will just
press zero in here so basically it mean
that we have a plane on the X Z axis
because it's luk upward the normal
vector and the distance from this
original point the original point is
zero so it basically lies on the X Z
axis all right and then we have to just
create a motion shape it's a bit hard to
explain what motion shape is but pretty
much this is how you can set the motions
and positions to an object although in
case of static although in case of the
static plane shape you can already set
the position right here with this zero
variable and the normal Vector for other
shapes you can set the position in the
Constructor for example for spheres you
just give the radius and you can't have
set the position in the Constructor of
the shape you have to use this motion
shape so again it's pretty simple to
make BT Motion state and this Motion
state H should be dynamically allocated
as well so new BT default motion shape
it's pretty long you have to get used to
this kind of uh class names but I but
it's time as you create more and more
stuff it will be you will get used to it
okay and it's waiting for one parameter
which is this transform right here the
BT transform so basically the position
and the rotation okay now we have this
motion now uh two more things to go
before we can add this body to the word
is first create a rigid body
construction
info uh okay so let me just create that
so it is inside the BT so the BT rigid
body class if I go here and uh let's
just do a search for uh rigid body rigid
okay right here in the BT rigid body.
and right here you will find a new
structure created in the public called
BT rigid body construction info as you
can see you can set a lot of stuff in
here MTH the motion and so on so lot of
stuff you can actually set later on we
will take a look you can find
restitution and friction you may want to
uh set this kind of stuff but uh for now
let's just use the Constructor and set
whatever we need to set the MTH the
Motion state and collision shape and the
interior okay so these are the four
parameter the first the math so BT rigid
body constru con
raction info and I just call it info you
don't need to dynamically allocate this
one because this will be copied in the
BT rigid body so I will not dynamically
allocate it the first parameter is the
MTH the MTH is an interesting thing if
you set it to zero this will be static
so whatever object you set the MTH to
zero exactly zero it will be static and
anything else than zero so basically
greater than zero because negative MTH
doesn't make sense so great any MTH
greater than zero will be dynamic okay
so set the set it to a static because
it's it's a static plane okay so it will
not react to gravity and forces and so
on so it is static anything with mass of
zero is static okay the next is the
Motion state the Motion state is uh the
Motion state we have created in the last
in the last line so right here the
Motion state okay the next is the
Collision shape the Collision shape is
pretty much the shape you have created
as you can see it's waiting for BT
Collision shape but all of the Collision
shape have uh but all of the Collision
shapes are actually inherit from this BT
Collision shape so you can just press
this plane into here okay the last
parameter is default which is uh the
local interior as you can see it is a BT
B is 0000 0 so it's a local interior for
now let's not use this local interior
for anything the 00 0 will be good for a
static body for Dynamics body we need to
use the function
called calculate local interior to
actually calculate the interior all
right now we have the info so we can
actually create the body so BT rigid
body and I just call it body doesn't
really matter and and I just create a
new BT rigid body okay with this
info okay almost there we have one more
thing to do is to actually add this to
the word so word which we have created
up here which is the discr dynamic word
and and this has a function add rigid
body and this add rigid body is
basically just add we just pass whatever
body do we want to add and I like to do
one more thing is to uh store all of our
bodies in a vector so let me just create
a vector SD Vector with the BT rigid
body BT rigid body pointer and I just
call it bodies okay and let me just add
this body which we have created to these
bodies so bodies. push back
and body so I will add to this body so
later on it will be easier to free
everything as you can see we have three
new uh so three new Dynamic calls the
the shape the Motion state and the rigid
body itself and later on we can go
through with a for Loop the bodies and
we can get the shape and the motion from
that body we can delete those two and
also delete the rigid body itself so we
will not have a problem okay so one more
uh one uh so we have created an added
display of course it is not rendered yet
but so the steps which we have to do is
first create a transform the transform
is pretty much where where do we want
the body and what rotation do we want
the body because we said the identity we
have pretty much zero rotation and we
actually have and I actually set it
manually to the zero position this is
the center of the object the center of
the MTH basically okay then I create
whatever shape do I want for example
sphere box static plane cone and so on
you can add a lot of kind of stuff in
ballet physics for now we will just use
the simple uh kind of objects like
sphere and cylinder and Cone and some uh
like that okay the next is the Motion
state which is pretty much just set the
position as our perspective inside the
class it does interpolation as well for
you but you don't have to really know
about that or care about that okay the
rigid body construction info is pretty
pretty much the body definition if you
have seen my box 2D tutorial you can set
all kind of different stuff right here
like the MTH the motion the plane and
the bunch of other kind of different uh
stuff right here we don't really care
about
this for now so we will just simple add
this all right and uh and then we can
create the body and finally add the body
to the word so now we have one body in
the word okay and I will actually create
a function which will add a simple
sphere to the world for now I decided
not to add anything El just sphere and
plane so I will create a b uh sphere
this will be the return value and I just
call it add sphere okay this add sphere
function will actually waiting for the
radius of the sphere obviously so float
radius it will waiting for the position
so float X float y float float Z and
finally it's waiting for the MTH so
float MTH we don't need a separate
variable to tell whether uh it has to be
static or not because if the math is
exactly zero then this is a static
object if the MTH is not zero then this
is a dynamic object so the the steps are
similar actually so similar then I will
just copy and paste these into
here like that and um and we have to
change what shape do we have because
obviously we don't want make a plane
right now but we want to create a sphere
to create a sphere we use the BT sphere
shape uh class and pointer okay the BT
sphere shape pointer actually waiting
for the radius of the C the sphere and
that's all it doesn't wait for the
position that's why we have to set the
position with the BT transform so
x y z okay right here I just set the XY
Z coordinate so this free line right
here we just set the position zero
rotation and XY Z coordinate we don't
need to convert it to meters and stuff
like that okay the next stuff which we
need to change is actually right here we
have to pass the math right here as the
first parameter not zero zero meaning
that it is a static object but it not
does not necessarily a static object
okay and as I mentioned there is this
last parameter in the BD rigid body
Construction in for right here the last
body is local in inertia okay this local
inertia is basically calculated with the
function it is in every shape function
so it's it's inherited from the BT
Collision shape function this function
is called calculate local inertia okay
so let me actually call that but only
call that if it is a dynamic body if it
is a static body we don't care the 0 0 0
should be okay so let me create a vector
BT Vector fre and I just call it inertia
Okay just set it 0 0 0 okay so if the
body is static so if the MTH is not
equal to zero I will only call the
calculate local interial in that case if
the body is static then this should be 0
0 Z okay
so uh actually let me rename this to
sphere because it might be confusing if
I let it to be plain so sphere and it
has a function calculate local inertia
okay and it's waiting for two parameters
actually one parameter is the actual MTH
so MTH and the second parameter is this
Vector right here actually a reference
to this Vector so I just pass it the
inertia okay and it will calculate the
inertia whatever that is into this
inertia of vector so we can just pass it
as a last parameter so let me rename
this to sphere and
inertia right that's almost everything
which we had to do and uh and we are
pretty pretty much finished we have to
just return with this body because as I
have said I will return with a BT
actually what am I doing this is a BT
rigid body sorry about that so BT rigid
body which is our rigid body so this add
sphere function we add the sphere let's
add the sphere immediately to our
simulation so right here in the init
after I have added the plane I will call
this add sphere function with the radius
of one and then the second parameter
should would be the X Y at the x
coordinate and then the Y that
coordinate so let's make it 0 20 0 so
let's make a sphere 20 m above ground
okay and the last parameter is the mass
let's make it a dynamic body with 1
kilogram of mass okay I have added a
sphere we have almost finished we just
need to render the sphere so let's make
another function which will render a
sphere this will be a void function and
I just call it render sphere okay and I
will show you how to render a sphere
actually let's wait the sphere right
here so BT rigid body pointer and I just
call it sphere okay to render a sphere
I'm going to use the Glu sphere so this
Glu quadric object which I have showed
you in the beginning of this tutorial
but first of all let's check if it is
really a sphere because we don't want to
render something which is not sphere as
a sphere because that will be a crash in
our program so let me first check so if
sphere and I can get the Collision shape
so get Collision shape with this getter
function and then I have there is a
variable actually a function which is a
getter function which will return what
is the type of this shape so get shape
type okay so this G shape type will
return whatever shape this uh this BT
rigid body has there are different kind
of shapes as I mentioned there are boxes
and there are cylinders there are
spheres there are more complicated
objects like compound object which are
made of these simple object but there
are even more complex objects like
height field and so on so uh the sphere
is called sphere shape proxy proxy type
okay so this BT sphere proxy type is
pretty much a constant which is I don't
know it has some value and if this rigid
body was a sphere then this have to have
the sphere shape proxy type uh as the
shape type so if it is not the shap uh
sphere shape proxy type then I just
return from this function if it is not a
sphere but someone wanted to call the
this function to render as a sphere we
just return we don't care about that
okay next thing is let's actually set
the color so GL color free F and I just
set it to be red so we can easily see
the Spheres now the next thing is to
actually get the radius from this to get
the radius in the BT sphere shape if I
just open that open that up so sphere
shape if I open up the BT sphere shape
there is a function called get radius
right here get radius as you can see but
there is a problem we have a BT rigid
body but it is not a BT sphere shape to
convert it to a BT sphere shape I can
easily cast it because I know that this
is a BT sphere shape it is only seemed
to be a rigid body because of the
polymorphism so BT sphere
shape and I call the and I convert this
sphere and get Collision shape so this
actually this is Collision shape type
not a sphere shape type so sorry about
that I don't cast the rigid body into
sphere shape but I guess I will cast the
Collision shape to a sphere shape okay
so I casted this into a sphere shape and
now the sphere shape there is a function
called get radius so get radius all
right so we have the radius let me
actually create a variable for that
float r equal to the radius okay the
next thing to do is to actually get the
motion State uh the transform get the
transform so BT transform I just create
this variable and I just call it t like
transform and to get the transform I
have to use the sphere and then I get
the Motion state from it so get Motion
state this return back this Motion state
whatever we have created right here in
the sphere as you can see we have
created this BT transform and we return
with this BT
transform with the function called get
word transform get word transform and I
just return it to T so this is the
reference now this T actually is is
filled up with the current field
position and the current field rotation
now we only need to find a way to
actually apply that translation and
rotation stuff to our sphere before we
draw it to do so we can have a a few
different kind of choice we can either
get the uh translation and rotation from
this transform let me actually open that
up so you can see how many chances you
have so
transform you can have a lot of it has a
lot of different function to get uh the
whatever stuff are in here so for
example we can have we can use the get
origin function to get the XY Z origin
and also we have this get basis uh not
this get basis yeah this get basis
function and the get basis is a 3X3
Matrix and you can get the Elder XY Z
coordinate but there is a much easier
way if we go down here there is a
function called get openg G Matrix
probably you have already know if you're
using openg G that openg GL uses
matrixes and this Matrix is basically
Define everything the position and
rotation and scaling of every single
object now this get openg GM Matrix we
return with this Matrix we only need to
multiply the current model view Matrix
with this and we are pretty much good to
go so whenever I use GL translate F or
function or GL rotate F or GL scale F
this basically multiply the current open
GL Matrix with whatever we press in here
so with this XY Z it's uh and uh yeah
that's pretty much it
so uh if we are multiply the current
Matrix with this Matrix so let me
actually get this and show you that will
be easier so t. get open open GL Matrix
and it's waiting for one parameter which
is a um which is an array with 16
elements so float M and with 16 element
16 element because as you probably know
open G Matrix are 4x4 matrixes and 4x 4
is uh 16 so just pass this Matrix
pointer right here and now you have the
open GL Matrix so let me actually use
the GL push Matrix so we will not screw
up our transformation matrix and G pop
Matrix right here so it will so this
matrix multiplication will be only
applied to this one Sphere not
everything else so GL mol Matrix F this
is a function I don't believe I have
used it earlier but this is very simple
it just multiply the current Matrix with
a matrix you just pass here so if I just
press the Matrix uh here which we have
get from this get open GM Matrix this
will multiply the current model view
Matrix or your current Matrix mode but
we are in model view Matrix usually so
it will just multiply the model view
Matrix with this Matrix so basically
what you need to know about this line is
said that the translation
trans translation and the rotation okay
now that we have this we have nothing
else to do just render the sphere so Glu
sphere okay the first parameter is the
radius which we have calculate which we
have get right here with this get radius
function the second actually the first
parameter is the quadric object second
parameter is the radius third and fourth
parameter is the resolution so not just
said it 2020 and it should be okay okay
and I'm pretty sure we are finished with
this render sphere function so again
here I just set I just check if it is
really a sphere if it is not a sphere
like we don't want to render a cylinder
like a sphere we want to render a
cylinder like a cylinder okay then I
just set get the radius of the sphere
and I convert and will cast this
Collision shape so BT Collision shape to
a BT sphere shape I can do that because
this BT sphere shape is actually
inherited from the from the convex
internal shape and the convex internal
shape is actually inherited from BT
Collision shape uh okay so I can cast
this to a sphere shape because I know it
is a sphere because I have checked in
here in the first line that this is
actually a sphere all right then I just
get the ver transformation and I get the
open GM Matrix from
this so openg GL Matrix is pretty much
storing the rotation and translation of
each object so I just use this matrix
multiplication to actually apply this
translation and rotation to the current
object of course between push and pop
Matrix so we will only apply this
translation and rotation to this object
not every other object okay and then I
just simply render the sphere that was
the render spere function of course you
have to make a rendered cylinder
function a render box function because
they are rendered
differently now that we have this part
we can we are almost finished with the
first example actually let me make a
render plane function as well because we
already have a plane right here we have
created a plane a static plane to render
a static plane that will be very simple
because it is almost the same as the
render sphere actually it's all so much
the same I will actually copy and paste
this into here
okay the thing which we have to H change
is that this is called Static plane
proxy type so static plane proxy type
that's right this is the type okay so
check if it is uh let me rename this two
planes so I will check if it is really a
plane and I will render it as as a plane
if it is a plane okay set the color I
will set to 0 8 0.8 0.8 so it's a very
light gray color okay here don't have a
radius so we can actually delete this
line right here and the rest of the line
is staying but here instead of rendering
a sphere I will render a plane I will
use immediate mode to render the plane
because that's the quickest so GL begin
GL quads and right here gln and I will
just render a th000 by a th plane so GL
vertex 3 F minus 1,000 0,000 so this is
basically
the left uh F point then GL vertex 3s
then I get - th000 0 - th000 this is
the the left uh closer plane uh closer
vertex then three vertex 3F and th000 0
and minus th000 and finally GL vertex 3F
yep the V is capital free F and this
should be th000 zero and thand okay I
have just rendered the simple plane and
let me see if I need to change anything
I will change this to plane instead of
sphere okay change this one as well and
change this and yes that's pretty much
it I have rendered a plane so now I can
go uh wait a bit uh no it's okay okay so
now one last thing is remaining is go
through all of the object and render
them so as you probably remember we
store all of the
bodies in
a in a bodies array a vector right here
bodies Vector which is storing all of
the rigid bodies so I can use a for Loop
to go through all of these there is
other ways to do that the from the word
you can Corry actually all of the object
but it is the easiest way so in IAL z i
less than the bodies that's size I ++ so
go through all of the bodies and just
check what bodies they are so if bodies
I and again I get the Collision shape
from that get Collision shape and then I
get the shape type get shape type from
these and I just uh compare them to
different kind of stuff by the way this
is the same line as this one right here
so get the shape type so I will just
first of all let's see if it is a static
plain proxy type so if it is a static
plane proxy type then I just use the
render plane function with the bodies
bodies
ey okay and as if we have one different
kind of object which is actually a
sphere we don't add anything else yet
later on we will but for now I will only
have spheres and planes so this is the
sphere shape proy type sphere undor
shape proxy type and then I call the
render sphere function
right here render sphere and again I add
the body eye to it okay and that was it
so again check if it is render sphere
render plane and I'm pretty sure we are
finished one last thing is remaining
although that we should include the
ballet header files so include and we
only have to include the header file
which is at the top so right here if I
go here we need to includ include this
was an accident we need to to include
this ballet Dynamic common.
AG so I have actually the header file if
you remember from the first tutorial I
have created a source folder so right
here in my USR folder right here in the
include I have created a folder called
ballet right here ballet and in this
ballet folder I have this com uh
Dynamics common header file so I need to
include uh I need to include the ballet
slbt
Dynamics common. header file but if you
are using uh for example ballet uh for
example code blocks for this ballet
physics you may have not created this
ballet folder but you actually just uh I
don't know copied this ballet folder or
something so just make sure that you can
somehow access to
this actually this is BT ballet Dynamics
Comm just realize so just make sure that
somehow you can access to this header
file uh so you might if you use code
blocks and you have just uh uh set this
Source folder as your include folder PS
then you need don't need this ballet and
you have to write it like that so it's
it depends on how you use it okay now
the most interesting moment of all the
things come is that we have to test
actually if this work or not my guess is
not but we will see so I will compile
everything
with the lfdl LGL lglu and lfdl image
Flags these are the normal things which
I'm which I have used to compile an
openg G Pro program but we also need the
L
ballet soft body and then L ballet
Dynamics and then the L
ballet uh collision and finally the lbet
linear math head uh math Library file to
compile this project and as as I said in
the in the first tutorial you need to
add D i/ if you are using command line
in code logs if you have set the include
path correctly you don't need to do
anything else so/ USR
slash include slash ballet else it will
not work okay so now that I have added
this we have the errors which we have to
fix so from right here okay open GL CPP
line 14 G uh BT broad phase interface
I'm pretty sure that P is NOT
capitalized okay line
22 line 22 oh of course the BT set
origin doesn't wait for fre numbers but
it's waiting for the BT Vector three
which is basically the vector class of
the bullet physics right okay so line 28
default motion Motion state not motion
shape so line
22
uh I mean 28 Motion state not shape
state right I hope I don't screw up in
the other places uh okay so line
77 right here
77 broad phas of course this is fixed
already B sequential impulse constraint
solver I have left a left the new right
here so new okay and line
84 again I screwed up here as well we
should use the BT Vector free BT Vector
free class which is the vector class of
the ballet physics 86 oh I screwed up
right here as well so
state right and
126 render plane function was not
declared that's probably because the p
should be capitalized render plane
and uh and I think the other was already
fixed so let's try it
again okay linear MTH that is because we
do not have the ballet right here just
simple linear MTH instead of ballet
linear map okay it is compiled so now if
I can run it then indeed here is our
sphere and incredible we have a sphere
one more thing actually before I go and
finish up with this tutorial because it
is almost finished is actually I want to
add a shooting segmentation error
something is not deleted in the right
order maybe we should delete the broad
phase before the word like that okay and
we should always Al delete the object I
I have almost forgot so I just use a for
Loop so for in IAL z i less than bodies
bodies do size i++ so go through all of
the objects and then in
here I just remove the object so word
and remove
Collision object which is removing one
object from the word and I just remove
the body's ey so I just remove the
object next thing to do is get the
Motion state from it so BT Motion state
pointer and Motion state and to get the
to get it we I use the get Motion state
function which have already used that so
bodies I and get motion get Motion state
okay and I would also get the shape from
it so BT Collision shape pointer shape
equals to bodies I and get Collision
shape so this this Motion state and
collision shape were two things which we
have dynamically allocated if you
remember so if I go here to the ad spere
as you can see I have dynamically
allocated to the Motion state and also I
have dynamically allocated the shape so
the Motion state and the shape so I will
delete Del these two stuff so delete the
bodies so body not the bodies body
delete the shape and also delete the
Motion state Motion
state right so we free everything which
we have reserved right here although it
work without it but always free whatever
you have uh uh use dynamically allocated
okay right here if I press the space I
want to shoot out a sphere because this
is one of the simplest way to interact
with the word by shooting okay so case
and if the space key is press so sdlk
space then I will just add a sphere so
add sphere the add sphere function if I
remember correctly waiting for a radius
then it's waiting for the XY Z
coordinate I want to shoot from my
current position so cam. getet location
which is if you remember the camera is
our camera the get location getting the
location of of the camera so I get the x
y z coordinate coordinate from the
camera so X Y Zed from the camera and
finally it's waiting for the mess I
don't know let's make it one for now
okay and I will also want to set the
velocity of this sphere so it will go
forward go where I Look to where I look
it we can easily determine that I will
use the get Vector function so uh this
is a uh Vector 3D and look H so I will
use the cam. get Vector function and
multiply it by 20 for example so the
higher this number the more force it
will have with the shoot and finally the
last thing to do is
that is that BT rigid body equal uh
sphere okay so I just get the return
value from the add sphere function which
is obviously our sphere and then I apply
this so sphere and and there is a
function called set linear velocity
which is setting the linear velocity
okay it's waiting for a vector which is
the our luk Vector so BL
Vector free uh and it's waiting for the
luk do X l.y and l. Z perfect okay so in
theory if everything goes correctly we
will get the current coordinate of the
camera and then we put a sphere there
and then we will shoot this sphere with
this much of force so 20 Newton of force
I don't know guess and then I set the
linear velocity which is basically the
velocity of the sphere to to this Vector
so the higher
the so the higher this number is the
more Force we will apply to that object
uh let's
write seems like compiled so if we run
it right now we will find the sphere and
as you can see I can show shoot out
spheres not too interesting so far but
we are definitely getting somewhere so I
can shoot out
spheres okay this shouldn't happen in
the reality but you know computers are
perfect uh anyway thank you for watching
and have a great day