| Stopping Two Squares 
 
	 
	
		| Author | Message |   
		| Silinter 
 
 
 
 
 | 
			
				|  Posted: Thu Apr 17, 2008 9:10 pm    Post subject: Stopping Two Squares |  |   
				| 
 |  
				| Yes, I have looked at collision detection tutorials, and I know collision detection, both square and circle. Not perfect circle however, but that's beside the point. Now, I have two squares... well, 1 square and a picture. I want the square to halt when it touches the other square. I've tried numerous ways, but they always get stuck inside each other or go right through each other. I've tried setting the velocities to 0, but that just makes it get stuck inside the square. How would I transform the square collision detection (aka if box1_x2 > box2_x1 and box1_x1 < box2_x2 and box1_y2 > box2_y1 and box1_y1 < box2_y2 then) to make the box stop, and be able to move around afterwards. I think I got this to work in the past, but I can't remember how. I thought I had all the collision detection down, and I can make it bounce off each other (by multiplying it by -1) but I can't simply make it simply stop in its place. I had an idea where it makes the x value equal to the bound box, so it stop is and it can move around afterward, but I can't get that into code. Help would be appreciated, as I need this done in a hurry!
 Thx in advance, you guys at CompSci do a great job at helping people in need
   |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sponsor Sponsor
 
  
   |  |   
		|  |   
		| zylum 
 
  
 
 
 | 
			
				|  Posted: Thu Apr 17, 2008 9:17 pm    Post subject: RE:Stopping Two Squares |  |   
				| 
 |  
				| Can you post your code? Setting the velocities to 0 would stop both boxes.. |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Silinter 
 
 
 
 
 | 
			
				|  Posted: Thu Apr 17, 2008 9:38 pm    Post subject: Re: Stopping Two Squares |  |   
				| 
 |  
				| I meant setting the box that the player control's velocity to 0, not the other one. Currently my code has no collision detection, but here it is anyway. The 3 boxes are where I want the "walls" to be, that the player has to get past to get to the desktop. 
 
 
 
	
		
	 
		| Description: |  |  Download
 |  
		| Filename: | Code.rar |  
		| Filesize: | 133.38 KB |  
		| Downloaded: | 102 Time(s) |  
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| CodeMonkey2000 
 
 
 
 
 | 
			
				|  Posted: Thu Apr 17, 2008 9:43 pm    Post subject: RE:Stopping Two Squares |  |   
				| 
 |  
				| Your collision proc should be a function that returns true if it collides, and false if it doesn't. Then you predict where the next move will take you using the function. Given the outcome take the proper action. |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Silinter 
 
 
 
 
 | 
			
				|  Posted: Thu Apr 17, 2008 9:56 pm    Post subject: RE:Stopping Two Squares |  |   
				| 
 |  
				| Yes, I know all about that function, but what would that action be? That's all I need to know, as I've already set up that function, as I've learned from the various tutorials here at CompSci. Would the action be vx := 0 and vy := 0? Or would it be to set the position of x right beside the obstacle? The action is all I need here, not how to create the function or how to apply it.
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| CodeMonkey2000 
 
 
 
 
 | 
			
				|  Posted: Thu Apr 17, 2008 10:17 pm    Post subject: RE:Stopping Two Squares |  |   
				| 
 |  
				| Depends. If you see that you collide of you go horizontally, vx = 0. If you see that you collide when you go vertically, vy =0. You should check thse two displacements separately for smoothness. |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Silinter 
 
 
 
 
 | 
			
				|  Posted: Thu Apr 17, 2008 10:20 pm    Post subject: RE:Stopping Two Squares |  |   
				| 
 |  
				| Yes, but when I set vx to 0, or vy to 0, (depends on the collision direction) it gets stuck inside the box as the vx is constantly being set to 0 and the arrow keys' commands of setting them to 20 is over-written. Here is the updated code.
 
 	  | Turing: |  	  | setscreen ("graphics:800;600,offscreenonly")
var chars : array char of boolean
var  font := Font.New ("Comic Sans MS:25")
var  done : boolean := false
var  x, y, vx, vy, frame : int := 0
var  et, st, ft : real := 0
var  mm : array 1 . . 10 of int 
st := Time.Elapsed
for  i : 1 . . 10 
    mm ( i) := Pic.FileNew ("mm"  + intstr ( i)  + ".jpg")
end for
fcn  Collision ( x1, y1, x2, y2 : int) : boolean
    result  x + Pic.Width ( mm (1))  > x1 and  x < x2 and  y + Pic.Height ( mm (1))  > y1 and  y < y2
end  Collision
loop
    Input.KeyDown ( chars) 
    frame += 1 
    x +=  vx
 
    y +=  vy
    if  frame = 11 then 
        frame := 1
    end if
    if  chars (KEY_RIGHT_ARROW) and  x < maxx  - Pic.Width ( mm ( frame)) then 
        vx := 20
    elsif  chars (KEY_LEFT_ARROW) and  x > 0 then 
        vx :=  -20
    elsif  vx = 20 or  vx =  -20 then 
        vx := 0
    end if
    if  chars (KEY_UP_ARROW) and  y < maxy  - Pic.Height ( mm ( frame)) then 
        vy := 20
    elsif  chars (KEY_DOWN_ARROW) and  y > 0 then 
        vy :=  -20
    elsif  vy = 20 or  vy =  -20 then 
        vy := 0
    end if
    if  Collision (300 , 200 , 400 , 300) then 
        vx := 0 
        vy := 0
    end if
    if  Collision (700 , 200 , 750 , 300) then 
        vx := 0 
        vy := 0
    end if
    if  Collision (100 , 500 , 150 , 700) then 
        vx := 0 
        vy := 0
    end if
    drawfillbox (300 , 200 , 400 , 300 , black)
    drawfillbox (700 , 200 , 750 , 300 , black)
    drawfillbox (100 , 500 , 150 , 700 , black)
    Pic.ScreenLoad ("desktop.bmp", 470 , 450 , picMerge)
    exit when  x >= 640 and  y >= 460 or  chars (KEY_ESC)
    Pic.Draw ( mm ( frame),  x, y, picCopy)
    View.Update
    delay (20)
    cls
end loop
Pic.ScreenLoad ("Love.jpg", 595 , 470 , picMerge)
Pic.ScreenLoad ("mmzero.bmp",  x, y, picMerge) 
ft := Time.Elapsed 
et := ( ft - st)  / 1000
Font.Draw (realstr ( et, 0), 0 , 100 , font, black) | 
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| CodeMonkey2000 
 
 
 
 
 | 
			
				|  Posted: Thu Apr 17, 2008 10:42 pm    Post subject: RE:Stopping Two Squares |  |   
				| 
 |  
				| That's why you predict where the box will be before moving it. When you call your function, it's not just x1,y1,x2,y2 it's x1+vx,y1+vy,x2,y2. And your collision function isn't working the way it should. It should take in the coordinates for both rectangles. 
 	  | Turing: |  	  | 
fcn collide(x1,y1,x2,y2,  bx1,by1,bx2,by2:int):boolean
 %what ever
 end collide
 
 | 
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sponsor Sponsor
 
  
   |  |   
		|  |   
		| Silinter 
 
 
 
 
 | 
			
				|  Posted: Thu Apr 17, 2008 10:48 pm    Post subject: Re: Stopping Two Squares |  |   
				| 
 |  
				| Srry, I'm not sure I follow. What do you do when you call the function? More specifically, what would I do to my function to make it work? |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Silinter 
 
 
 
 
 | 
			
				|  Posted: Fri Apr 18, 2008 6:33 pm    Post subject: RE:Stopping Two Squares |  |   
				| 
 |  
				| No, I simplified it, as I already know the first rectangle's coordinates (the picture), all I need is the obstacle's coordinates. And I don't see how that helps with making them simply stop beside each other. |  
				|  |  |   
		|  |  |  
	  
		|  |   
		|  |  
 |