I'm quite new to löwe and was just wondering as I looked through the Documentation if there are keywords like "self" or "other". For example I had a project in GameMaker where some circles were created and then simulated elastic collision physics on each other. This was somehow quite easy as you got the basic collision and response code working because GameMaker is somehow "object-oriented".
First you would create an object which will represent the circles/balls. This object would make some use of different events. GameMaker works through events. You have different events for different things. Code in the "create event" would only be executed on the creation of the object the create event is connected with. Step event would be executed every frame...
First in the create event you would add some code that specified the direction and the velocity the ball had.
In the step event I had this code running, which would cycle through each other object_ball and check for collisions and calculate response.
Code: Select all
with obj_ball
{
if id!=other.id
{
dx=other.x-x;
dy=other.y-y;
dis=sqrt(sqr(dx)+sqr(dy));
if dis<=radius+other.radius
{
//normalize
dx/=dis;
dy/=dis;
//calculate the component of velocity in the direction
vp1=hspeed*dx+vspeed*dy;
vp2=other.hspeed*dx+other.vspeed*dy;
if (vp1-vp2)!=0
{
dt=(radius+other.radius-dis)/(vp1-vp2);
//dt=(radius+other.radius-dis)/(vp1-vp2);
//move the balls back so they just touch
x-=hspeed*dt;
y-=vspeed*dt;
other.x-=other.hspeed*dt;
other.y-=other.vspeed*dt;
//projection of the velocities in these axes
va1=(hspeed*dx+vspeed*dy);
vb1=(vspeed*dx-hspeed*dy);
va2=(other.hspeed*dx+other.vspeed*dy);
vb2=(other.vspeed*dx-other.hspeed*dy);
//new velocities in these axes. take into account the mass of each ball.
vaP1=(va1+bounce*(va2-va1))/(mass/other.mass);
vaP2=(va2+other.bounce*(va1-va2))/(other.mass/mass);
//vaP1=(va1+bounce*(va2-va1))/(1+mass/other.mass);
//vaP2=(va2+other.bounce*(va1-va2))/(1+other.mass/mass);
hspeed=vaP1*dx-vb1*dy;
vspeed=vaP1*dy+vb1*dx;
other.hspeed=vaP2*dx-vb2*dy;
other.vspeed=vaP2*dy+vb2*dx;
//we moved the balls back in time, so we need to move them forward
x+=hspeed*dt;
y+=vspeed*dt;
other.x+=other.hspeed*dt;
other.y+=other.vspeed*dt;
}
}
}
}
However this code cycles through every instance of the object ball that is in the current room and check for collisions. At least there was the draw event which made the circle visible.
So this is somehow object oriented. The object itself would be the class and this class would create instances of itself on start up.
However I don't know how I would do that in LUA as it isn't that object oriented I guess? I would go with creating a table for the object_ball which would store values like x,y, velocity in x,y. Then I would store all these tables in another table and the collision code would cycle through each table checking for collisions and calculating responses.
This seems a bit "ugly" and "around the edge" to me. And as I'm just starting off I thought there had to be another way to solve this.