Collision problem

Posted By: Gastara

Collision problem - 06/22/10 17:21

I’m playing with code from the AUM 84 where collision is done by checking the color of the pixels near the player. I'm doing some test with some simple graphic. When this line is divided by 2:
Code:
coords1_x = my_player.pos_x + bmap_width(player_tga) / 2;

the half of the player is moving through the corner when half of the players width are outside of the red color’s top. I want all the width of the player sprite to be blocked so I remove the /2 from the line above. That works fine, but just on one corner of the red color's top. At the opposite side, it’s not blocking when some part of the players width are outside the red color’s top (see picture below). How is it possible to avoid this, so that the player is not moving on the inside or through the corners of the red color? It works fine on one side, but not on both.

Here it's the collision fine (the small quadrangle is player)



Here it's moving through and on the inside of the corner



Here is the code:

Code:
function collisions_startup()
{
	wait (3); // wait until the video functions are available
	var coords1_x, coords1_y, coords2_x, coords2_y, coords3_x, coords3_y, coords4_x, coords4_y;
	var format, pixel1, pixel2, pixel3, pixel4;
	COLOR pixel1_color, pixel2_color, pixel3_color, pixel4_color;
 
	while (1)
	{
		
		// check player's feet
      coords1_x = my_player.pos_x + bmap_width(player_tga); // 
		coords1_y = my_player.pos_y + bmap_height(player_tga); // play with 2 or - 20
		format = bmap_lock (level_tga, 0);
		pixel1 = pixel_for_bmap(level_tga, coords1_x, coords1_y);
		pixel_to_vec (pixel1_color, NULL, format, pixel1); // store the color of the pixel1 in pixel1_color 
		if (pixel1_color.red == 255) // detected a red pixel below player's feet?
		{
			my_player.pos_y -= 2;
			}
	
		bmap_unlock (level_tga);
		wait (1);
	}
	
}




Posted By: Ottawa

Re: Collision problem - 06/22/10 22:45

Hi!

I believe that the /2 is done to get the radius of the object.
Posted By: Gastara

Re: Collision problem - 06/23/10 07:14

I was also thinking about that, but in this example it divides the width of the player by 2. So when more than half of the player is outside the corner, it passes through and is no longer blocked (see picture below). As I understand it, this is because the half of the player’s width is not blocked since it’s divided by to. When I remove / 2, it blocks all the width on one corner fine, but on the other corner it stop blocking when just one pixel are outside the red color.


Posted By: Helghast

Re: Collision problem - 06/23/10 08:02

it actually divides the whole thing in 2 with your code...
use brackets to add half the bitmap size. and make sure the center_x/y are set to the middle if it's a panel.

and then use this:
Code:
coords1_x = my_player.pos_x + (bmap_width(player_tga) / 2);



that should work.

regards,
Posted By: bart_the_13th

Re: Collision problem - 06/23/10 08:26

You need to check the pixel twice. First at:
Code:
y = my_player.pos_y + bmap_height(player_tga);



Then at:
Code:
y = my_player.pos_y;


Posted By: Ottawa

Re: Collision problem - 06/23/10 23:20

Hi!



Have you looked at collision in the manual
You could solve your problem with c_setminmax
Posted By: Rei_Ayanami

Re: Collision problem - 06/24/10 09:08

@Ottawa : huh? this is all 2D tongue so c_setminmax makes no sense wink
Posted By: Gastara

Re: Collision problem - 06/24/10 14:08

Thanks for all response. Done a lot of testing with if statements where I added and subtracted the width of the sprite (40 in this case), but no result. It works fine on one side but not on the other… So I end up with this solution. Not the best or must genius solution I think, but it works. The screen size is 1400, so I give coords1_x a value of 700. I use that value when doing the check and now it works fine.

Anyway, I’m shore it’s a better way to do it.

Code:
function collisions_startup()
{
	wait (3); 
	
	var coords1_x, coords1_y;
	var format, pixel1;
	COLOR pixel1_color;
 
	while (1)
	{
		
		// This check the bottom of the player sprite
		
                coords1_x = 700;
		
		if (coords1_x <= (my_player.pos_x + bmap_width(player_tga))) 
		{
		coords1_x = my_player.pos_x + bmap_width(player_tga) - 40;
	   }
		if (coords1_x >= (my_player.pos_x + bmap_width(player_tga))) 
		{
		coords1_x = my_player.pos_x + bmap_width(player_tga);	
		}	
	 
		coords1_y = my_player.pos_y + 40;
		format = bmap_lock (level_tga, 0);
		pixel1 = pixel_for_bmap(level_tga, coords1_x, coords1_y);
		pixel_to_vec (pixel1_color, NULL, format, pixel1); 
		if (pixel1_color.red == 255) 
		{
			my_player.pos_y -= 2;
			}
	
		bmap_unlock (level_tga);
		wait (1);
	}

}


Posted By: Ottawa

Re: Collision problem - 06/24/10 14:10

Hi!

@Rei_Ayanami : Yes, your right!

@Gastara : Looked at aum84 and now the question is this

Did you finish the exercise and was everything working fine at the end?

Ottawa wink

edit : Why are you using only a part of the code provided in 2dcollision4.c
Are your bitmaps dimensions a power of 2?
Posted By: Gastara

Re: Collision problem - 06/24/10 17:53

Hi

@Ottawa: I just posted the function for the bottom of the player since I started to solve that problem first. When that was done, I use the same principle for the head of the player with some modifications.

@Ottawa: Yes, my bitmaps dimension is a power of two.

The function works fine now for player’s bottom and head. Still some glitch at the corner when players left side meet the red color. Need to solve that problem now smile

Here's my progress

Code:
function collisions_startup()
{
	wait (3); 
	
	var coords1_x, coords1_y, coords2_x, coords2_y, coords3_x, coords3_y, coords4_x, coords4_y;
	var format, pixel1, pixel2, pixel3, pixel4;
	COLOR pixel1_color, pixel2_color, pixel3_color, pixel4_color;
 
	while (1)
	{
		
		// check player's bottom
		// Problem solved
		coords1_x = 700;
		
		if (coords1_x <= (my_player.pos_x + bmap_width(player_tga))) 
		{
		coords1_x = my_player.pos_x + bmap_width(player_tga) - 40;
	   }
		if (coords1_x >= (my_player.pos_x + bmap_width(player_tga))) 
		{
		coords1_x = my_player.pos_x + bmap_width(player_tga);	
		}	
	 
		coords1_y = my_player.pos_y + 40;
		format = bmap_lock (level_tga, 0);
		pixel1 = pixel_for_bmap(level_tga, coords1_x, coords1_y);
		pixel_to_vec (pixel1_color, NULL, format, pixel1); 
		if (pixel1_color.red == 255) 
		{
			my_player.pos_y -= 2;
			}
			
			
	// check player's top
	// Problem solved
      coords2_x = 700;
		
		if (coords2_x <= (my_player.pos_x + bmap_width(player_tga))) 
		{
		coords2_x = my_player.pos_x + bmap_width(player_tga) - 40;
	   }
		if (coords2_x >= (my_player.pos_x + bmap_width(player_tga))) 
		{
		coords2_x = my_player.pos_x + bmap_width(player_tga);	
		}	
	
	   coords2_y = my_player.pos_y; 
		pixel2 = pixel_for_bmap(level_tga, coords2_x, coords2_y);
		pixel_to_vec (pixel2_color, NULL, format, pixel2); 
		if (pixel2_color.red == 255) 
		{
			my_player.pos_y += 2;
		}
		
		// Check players left side
		// Not solved, blocking fine, but still a glitch at the corners.
		// Player follow the edge around the corner when moving
		
		coords3_y = 450;
		
	   if (coords3_y <= (my_player.pos_y + bmap_width(player_tga))) 
		{
		coords3_y = my_player.pos_y + bmap_width(player_tga) - 40;
	   }
	   if (coords3_y >= (my_player.pos_y + bmap_width(player_tga))) 
		{
		coords3_y = my_player.pos_y + bmap_width(player_tga);
	   }
		
		coords3_x = my_player.pos_x + 2; // play with 2
		pixel3 = pixel_for_bmap(level_tga, coords3_x, coords3_y);
	   pixel_to_vec (pixel3_color, NULL, format, pixel3); 
		if (pixel3_color.red == 255) 
		{
			my_player.pos_x += 2;
		}
		
		// Check players left side
		// Not done yet
		
		
	
	
		bmap_unlock (level_tga);
		wait (1);
	}

}


Posted By: Ottawa

Re: Collision problem - 06/24/10 20:31

Hi!

I looked at the AUM84's png and tga in my paint program
The code in the AUM reacts to the size of these bmaps and is very flexible.

Do your bmaps have the same size in pixels?

Your code, I believe, will not be flexible because you use : coords1_x = 700; in the while loop. Your bmp returns to this position every time the loop is made.

Playing with AUM code wink
Ottawa wink
Posted By: Gastara

Re: Collision problem - 06/24/10 21:19

Hi!

Hm, yes I think I understand. I use 700 because my background image is 1400 in width and do the check against each side since the red color is in the middle.
Yes, I agree, it’s maybe not flexible, but it works fine for bottom and top of the player.
Not solved the sides yet. I will however try to find another solution and not do the check as in my last example.
Anyway, if you tested the code in AUM you maybe notice that the player is inside the water a little.
That’s no problem to solve. The big problem is just this corner glitch or what I shall call it. So I agree, it’s best to find another solution.
Posted By: Ottawa

Re: Collision problem - 06/24/10 22:57

Hi Gastara!

The part of the player that is in (over) the water is the head.
But the feet are on solid ground.

Play with 2dcollision4.c. Go around the water part in a very tight
circle (rectangle wink ). You will notice that part of the head is always
in (over) the water zone at the bottom.
The perspective of the player being close
to the water and the head over the water is very normal. Take a picture
of yourself near a pond or a lake. wink

My suggestion is to keep this great code and to play with your
.tga or png
Then only modify the distance in lines like : coords2_y = my_player.pos_y + 20; // play with 20
Posted By: Gastara

Re: Collision problem - 06/25/10 05:47

Hi!

Yes, I see the logical with the sprite used in that example (AUM 84 - 2dcollision4), and it works fine then.
I just removed the + 20 from (coords2_y = my_player.pos_y;) in player head since I don’t want some part of players head to be inside the red color in this case.
At the corner it blocks only the half of the player since it’s divided by 2.
That’s logical since when more than half of the player is outside the red colors edge, it will not block.
That’s fine with the code from AUM, but thinks about it like a Pong game. If the player head is the ball,
it would not block correctly when it reaches the pad. If half of the player is outside the corner of the pad, it will pass through.
Without /2, it will block all the width of the player when it reaches the pad, but just on one corner.

PS! I’m not going to make a pong game with this code solution. smile

Pong examples:

Here it blocks fine when divided by 2


Here it don’t block since more than half of the player are on the outside of the red color.


If I remove / 2 it block all the players width fine


but not at the opposite corner when some part of the player are ouside the red color


I will anyway try to find a solution for this without the if statements I used in my last example smirk .




Posted By: flits

Re: Collision problem - 06/25/10 09:20

i have been playing with this for a while and this is what i did create

http://www.opserver.de/coni_users/web_users/pirvu/au/demo/zips/collision.zip

but if you want a better solution there is even a better one on the aum resorce demo's page
Posted By: Gastara

Re: Collision problem - 06/25/10 10:15


Nice, thank you. I shall look at it. smirk
© 2023 lite-C Forums