3 registered members (Ayumi, Akow, AndrewAMD),
1,505
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Collision problem
#329762
06/22/10 17:21
06/22/10 17:21
|
Joined: Jun 2007
Posts: 15 Norway
Gastara
OP
Newbie
|
OP
Newbie
Joined: Jun 2007
Posts: 15
Norway
|
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:
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:
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);
}
}
Last edited by Gastara; 06/22/10 17:52.
|
|
|
Re: Collision problem
[Re: Gastara]
#329809
06/22/10 22:45
06/22/10 22:45
|
Joined: Apr 2006
Posts: 737 Ottawa, Canada
Ottawa
User
|
User
Joined: Apr 2006
Posts: 737
Ottawa, Canada
|
Hi!
I believe that the /2 is done to get the radius of the object.
Hope this helps! Ottawa Ver 7.86.2 Pro and Lite-C
|
|
|
Re: Collision problem
[Re: Gastara]
#329836
06/23/10 08:02
06/23/10 08:02
|
Joined: Jan 2004
Posts: 3,023 The Netherlands
Helghast
Expert
|
Expert
Joined: Jan 2004
Posts: 3,023
The Netherlands
|
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:
coords1_x = my_player.pos_x + (bmap_width(player_tga) / 2);
that should work. regards,
|
|
|
Re: Collision problem
[Re: Helghast]
#329837
06/23/10 08:26
06/23/10 08:26
|
Joined: Aug 2008
Posts: 482
bart_the_13th
Senior Member
|
Senior Member
Joined: Aug 2008
Posts: 482
|
You need to check the pixel twice. First at:
y = my_player.pos_y + bmap_height(player_tga);
Then at:
|
|
|
Re: Collision problem
[Re: bart_the_13th]
#329930
06/23/10 23:20
06/23/10 23:20
|
Joined: Apr 2006
Posts: 737 Ottawa, Canada
Ottawa
User
|
User
Joined: Apr 2006
Posts: 737
Ottawa, Canada
|
Hi!
Have you looked at collision in the manual You could solve your problem with c_setminmax
Hope this helps! Ottawa Ver 7.86.2 Pro and Lite-C
|
|
|
Re: Collision problem
[Re: Rei_Ayanami]
#329982
06/24/10 14:08
06/24/10 14:08
|
Joined: Jun 2007
Posts: 15 Norway
Gastara
OP
Newbie
|
OP
Newbie
Joined: Jun 2007
Posts: 15
Norway
|
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.
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);
}
}
Last edited by Gastara; 06/24/10 14:09.
|
|
|
Re: Collision problem
[Re: Rei_Ayanami]
#329983
06/24/10 14:10
06/24/10 14:10
|
Joined: Apr 2006
Posts: 737 Ottawa, Canada
Ottawa
User
|
User
Joined: Apr 2006
Posts: 737
Ottawa, Canada
|
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 edit : Why are you using only a part of the code provided in 2dcollision4.c Are your bitmaps dimensions a power of 2?
Last edited by Ottawa; 06/24/10 14:54.
|
|
|
Re: Collision problem
[Re: Ottawa]
#330007
06/24/10 17:53
06/24/10 17:53
|
Joined: Jun 2007
Posts: 15 Norway
Gastara
OP
Newbie
|
OP
Newbie
Joined: Jun 2007
Posts: 15
Norway
|
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 Here's my progress
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);
}
}
|
|
|
|