Multiple Collision Checking

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
adge
Citizen
Posts: 54
Joined: Mon Dec 14, 2015 8:50 pm

Multiple Collision Checking

Post by adge »

Hi there!

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;
            }
        }
    }
}
I don't fully understand this myself. I found it somewhere on the net but I recommend that some would use it If you want to do elastic collisions with circles.
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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Multiple Collision Checking

Post by s-ol »

adge wrote: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.
That is exactly how you do it and neither considered ugly nor around the edge.
Also you can do OOP in Lua, you can go ahead and just Google "Lua oop" for some information. However I think that bouncing balls are actually a good example for an application where OOP doesn't really make sense.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
adge
Citizen
Posts: 54
Joined: Mon Dec 14, 2015 8:50 pm

Re: Multiple Collision Checking

Post by adge »

Why wouldn't it make sense? I'm just curious.
Because you would have to cycle through each ball anyway?

Is this then known as procedural programming?
Post Reply

Who is online

Users browsing this forum: DarkblooM and 1 guest