collision particles 
	 
	
		| Author | 
		Message | 
	 
		 
		Homer_simpson
 
  
 
    
		 | 
		
		
			
				  Posted: Tue Dec 02, 2008 2:14 pm    Post subject: collision particles  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				i was messing around with the numbers on one of my old physics program and accidentally got a neat effect. particles stick together until they are broken again when they collide with eachother 
 
Quote: View.Set ("offscreenonly,graphics:700;350")
 
 
function distance (x1, y1, x2, y2 : real) : real
 
    result sqrt (((x2 - x1) ** 2) + ((y2 - y1) ** 2)) %y
 
 
end distance
 
function findangle (x1, y1, x2, y2 : real) : real
 
    var ang, slope : real
 
    if not (x2 = x1) then
 
        slope := (y2 - y1) / (x2 - x1)
 
    else
 
        slope := 999999999
 
    end if
 
    ang := arctand (slope)
 
    if slope > 0 then
 
        if y2 < y1 then
 
            ang := 180 + ang
 
        end if
 
    end if
 
    if slope < 0 then
 
        if x2 < x1 then
 
            ang := 180 + ang
 
        end if
 
        if x2 > x1 then
 
            ang := 360 + ang
 
        end if
 
    end if
 
    if slope = 0 then
 
        if x2 > x1 then
 
            ang := 0
 
        end if
 
        if x2 < x1 then
 
            ang := 180
 
        end if
 
    end if
 
    result ang
 
end findangle
 
type Particle_Type :
 
    record
 
        x, y, vx, vy, w : real
 
    end record
 
 
const MaxParticles := 50
 
 
var Particles : array 1 .. MaxParticles of Particle_Type
 
 
 
procedure RenewParticle (var p : Particle_Type, x, y : int, a, r, w : real)
 
    p.x := x
 
    p.y := y
 
    p.vx := a
 
    p.vy := r
 
    p.w := w
 
end RenewParticle
 
 
for i : 1 .. MaxParticles
 
    RenewParticle (Particles (i), Rand.Int (1, 700), Rand.Int (0, 350), 0, 0, Rand.Int (10, 100))
 
end for
 
colorback (black)
 
cls
 
 
RenewParticle (Particles (1), 20, 150, 4, 2, Rand.Int (10, 100))
 
RenewParticle (Particles (2), 500, 150, -4, 2, Rand.Int (10, 100))
 
var chars : array char of boolean
 
 
color (white)
 
var col := false
 
var temp1, temp2, temp3, magnitude : real
 
loop
 
    Input.KeyDown (chars)
 
    if chars ('+') then
 
    end if
 
 
    for i : 1 .. MaxParticles
 
        if not col then
 
 
            Particles (i).x += Particles (i).vx
 
            Particles (i).y += Particles (i).vy
 
        end if
 
        drawfilloval (round (Particles (i).x), round (Particles (i).y), 10, 10, gray)
 
 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ball collision%%%%%%%%%%%%%%%%%%%%%%%%
 
        for ii : 1 .. MaxParticles
 
            if distance (Particles (i).x, Particles (i).y, Particles (ii).x, Particles (ii).y) <= 20 and i not= ii then
 
 
                %put ii
 
                temp1 := findangle (Particles (i).x, Particles (i).y, Particles (ii).x, Particles (ii).y)
 
                temp2 := findangle (0, 0, Particles (ii).vx, Particles (ii).vy)
 
                %put "collision angle = ", temp1
 
                %put "Vellocity angle = ", temp2
 
                %put "relative angle  = ", temp2 - temp1
 
                %put "------------"
 
                temp3 := arccosd (-cosd (temp2 - temp1)) + temp1
 
                magnitude :=-2% sqrt ((Particles (ii).vx ** 2) + (Particles (ii).vy ** 2))
 
                /*drawline (round (Particles (i).x), round (Particles (i).y), round (Particles (ii).x), round (Particles (ii).y), red)
 
 
                 drawline (round (Particles (ii).x), round (Particles (ii).y), round (Particles (ii).x) + round (Particles (ii).vx * 4), round (Particles (ii).y) + round (Particles (ii).vy * 4),
 
                 yellow)
 
                 drawline (round (Particles (ii).x), round (Particles (ii).y), round (Particles (ii).x) + round (cosd (temp3) * magnitude * 4), round (Particles (ii).y) + round (sind (temp3) *
 
                 magnitude * 4),
 
                 11)
 
 
                 drawline (0, round (Particles (ii).y), 700, round (Particles (ii).y), green)%*/
 
                %col := true
 
                Particles (i).vx := cosd (temp3) * magnitude
 
                Particles (i).vy := sind (temp3) * magnitude
 
            end if
 
        end for
 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ball collision%%%%%%%%%%%%%%%%%%%%%%%%
 
 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%wall collision%%%%%%%%%%%%%%%
 
        if Particles (i).x >= 690 and Particles (i).vx > 0 then
 
            Particles (i).vx := -Particles (i).vx
 
        end if
 
        if Particles (i).x <= 10 and Particles (i).vx < 0 then
 
            Particles (i).vx := -Particles (i).vx
 
        end if
 
        if Particles (i).y >= 340 and Particles (i).vy > 0 then
 
            Particles (i).vy := -Particles (i).vy
 
        end if
 
        if Particles (i).y <= 10 and Particles (i).vy < 0 then
 
            Particles (i).vy := -Particles (i).vy
 
        end if
 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%wall collision%%%%%%%%%%%%%%%
 
 
 
 
    end for
 
    View.Update
 
    cls
 
 
end loop
 
  | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
		 
		Sponsor Sponsor 
		 
  
		 | 
		
 | 
	 
	 
		  | 
	 
				 
		Homer_simpson
 
  
 
    
		 | 
		
		
			
				  Posted: Tue Dec 02, 2008 2:27 pm    Post subject: Re: collision particles  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				here's a slightly different effect:
 
Quote: View.Set ("offscreenonly,graphics:700;350")
 
 
function distance (x1, y1, x2, y2 : real) : real
 
    result sqrt (((x2 - x1) ** 2) + ((y2 - y1) ** 2)) %y
 
 
end distance
 
function findangle (x1, y1, x2, y2 : real) : real
 
    var ang, slope : real
 
    if not (x2 = x1) then
 
        slope := (y2 - y1) / (x2 - x1)
 
    else
 
        slope := 999999999
 
    end if
 
    ang := arctand (slope)
 
    if slope > 0 then
 
        if y2 < y1 then
 
            ang := 180 + ang
 
        end if
 
    end if
 
    if slope < 0 then
 
        if x2 < x1 then
 
            ang := 180 + ang
 
        end if
 
        if x2 > x1 then
 
            ang := 360 + ang
 
        end if
 
    end if
 
    if slope = 0 then
 
        if x2 > x1 then
 
            ang := 0
 
        end if
 
        if x2 < x1 then
 
            ang := 180
 
        end if
 
    end if
 
    result ang
 
end findangle
 
type Particle_Type :
 
    record
 
        x, y, vx, vy, w : real
 
    end record
 
 
const MaxParticles := 50
 
 
var Particles : array 1 .. MaxParticles of Particle_Type
 
 
 
procedure RenewParticle (var p : Particle_Type, x, y : int, a, r, w : real)
 
    p.x := x
 
    p.y := y
 
    p.vx := a
 
    p.vy := r
 
    p.w := w
 
end RenewParticle
 
 
for i : 1 .. MaxParticles
 
    RenewParticle (Particles (i), Rand.Int (1, 700), Rand.Int (0, 350), 1, 1, Rand.Int (10, 100))
 
end for
 
colorback (black)
 
cls
 
 
RenewParticle (Particles (1), 20, 150, 4, 2, Rand.Int (10, 100))
 
RenewParticle (Particles (2), 500, 150, -4, 2, Rand.Int (10, 100))
 
var chars : array char of boolean
 
 
color (white)
 
var col := false
 
var temp1, temp2, temp3, magnitude : real
 
loop
 
    Input.KeyDown (chars)
 
    if chars ('+') then
 
    end if
 
 
    for i : 1 .. MaxParticles
 
        if not col then
 
 
            Particles (i).x += Particles (i).vx
 
            Particles (i).y += Particles (i).vy
 
        end if
 
        drawfilloval (round (Particles (i).x), round (Particles (i).y), 10, 10, gray)
 
 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ball collision%%%%%%%%%%%%%%%%%%%%%%%%
 
        for ii : 1 .. MaxParticles
 
            if distance (Particles (i).x, Particles (i).y, Particles (ii).x, Particles (ii).y) <= 20 and i not= ii then
 
 
                %put ii
 
                temp1 := findangle (Particles (i).x, Particles (i).y, Particles (ii).x, Particles (ii).y)
 
                temp2 := findangle (0, 0, Particles (ii).vx, Particles (ii).vy)
 
                %put "collision angle = ", temp1
 
                %put "Vellocity angle = ", temp2
 
                %put "relative angle  = ", temp2 - temp1
 
                %put "------------"
 
                temp3 := arcsind (-sind (temp2 - temp1)) + temp1
 
                magnitude := sqrt ((Particles (i).vx ** 2) + (Particles (i).vy ** 2)) * 1.05
 
                if magnitude > 11 then
 
                    magnitude := 1
 
                end if
 
                /*drawline (round (Particles (i).x), round (Particles (i).y), round (Particles (ii).x), round (Particles (ii).y), red)
 
 
                 drawline (round (Particles (ii).x), round (Particles (ii).y), round (Particles (ii).x) + round (Particles (ii).vx * 4), round (Particles (ii).y) + round (Particles (ii).vy * 4),
 
                 yellow)
 
                 drawline (round (Particles (ii).x), round (Particles (ii).y), round (Particles (ii).x) + round (cosd (temp3) * magnitude * 4), round (Particles (ii).y) + round (sind (temp3) *
 
                 magnitude * 4),
 
                 11)
 
 
                 drawline (0, round (Particles (ii).y), 700, round (Particles (ii).y), green)%*/
 
                %col := true
 
                Particles (i).vx := cosd (temp3) * magnitude
 
                Particles (i).vy := sind (temp3) * magnitude
 
            end if
 
        end for
 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ball collision%%%%%%%%%%%%%%%%%%%%%%%%
 
 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%wall collision%%%%%%%%%%%%%%%
 
        if Particles (i).x >= 690 and Particles (i).vx > 0 then
 
            Particles (i).vx := -Particles (i).vx
 
        end if
 
        if Particles (i).x <= 10 and Particles (i).vx < 0 then
 
            Particles (i).vx := -Particles (i).vx
 
        end if
 
        if Particles (i).y >= 340 and Particles (i).vy > 0 then
 
            Particles (i).vy := -Particles (i).vy
 
        end if
 
        if Particles (i).y <= 10 and Particles (i).vy < 0 then
 
            Particles (i).vy := -Particles (i).vy
 
        end if
 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%wall collision%%%%%%%%%%%%%%%
 
 
 
 
    end for
 
    View.Update
 
    cls
 
 
end loop
 
  | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		 | 
	 
 
	
	
	 
	
	 |