Log In  

the image is map of my game.

pink rectangles are empty sprite on the map.

If game characters enters the location of these empty sprites, some events should happen.

I asked similar question some days ago, a person said me to use mget method.

https://www.lexaloffle.com/bbs/?pid=51536#p51536

I thought I should compare player's x coordinate and y coordinate to coordinates of sprite. I tried some days but nothing helped.

p={
    x= --x coordinate of player
    y= --y coordinate of player
}

p.x and p.y are coordinates of player. I can't change that because my code has 600~700 lines..

could someone help me with simple examples or description?

P#52131 2018-04-29 08:58 ( Edited 2018-04-30 19:27)

1

Without code sample, it’s a bit difficult to understand what’s not working.

Key question is: what is your player unit? Are x/y pixel coordinates?
The ‘difficult’ part to use mget is to convert your world unit into a 16x16 grid.
Pseudo code:

— assuming player = screen coordinates
local i,j=flr(p.x/8),flr(p.y/8)
local sprite=mget(i,j)
local flags=fget(sprite)
— do something with flags
P#52161 2018-04-30 01:44 ( Edited 2018-04-30 05:45)

yes x/y pixel coordinates.

I made a minor mistake in question. it is enemies x and y which should I use.

e={
x=--x coordinate
y=--y coordinate
}

Now I am just doing

if(e.y== y coordinate of empty sprite and e.x== x coordinate of sprite)

and repeat that

this code is really ineffective

P#52186 2018-04-30 10:37 ( Edited 2018-04-30 14:37)

Your code like that will only check when the enemy and this "empty sprite" coincide 100% on location, meaning that it will only trigger when they are fully overlapped. That's not really collision unless you want it to be exactly that way. You need to check against their bounding boxes or another traditional collision method.

With that said, if you know where the "empty sprite" is (x and y) and know how wide/tall it is (looks like 8px) then you have all the info you need to make a bounding box collision without the use of a sprite unit. Or are you using flags?

Also be wary that if you are moving your enemies in float intervals, the X and Y will rarely ever be ==, but a bounding box collision will help you out with that anyway.

Again as freds72 said with exact code it's hard to tell what's going on but if you are indeed just checking for each sprite's X and Y you are only colliding down to that very precise point (the sprite's top-left corner pixel)

P#52187 2018-04-30 12:11 ( Edited 2018-04-30 16:11)
function get_coll_map(o,flag)
    o.coll={};
 local x1=o.x/8;
 local y1=o.y/8;
 local x2=(o.x+7)/8;
 local y2=(o.y+7)/8;
 local a=fget(mget(x1,y1),flag);
 local b=fget(mget(x1,y2),flag);
 local c=fget(mget(x2,y2),flag);
 local d=fget(mget(x2,y1),flag);
 local randombottom = rnd(16)+96;

 if a then
  add(o.coll,{x=clamp_map_cord(x1);y=clamp_map_cord(y1)});
 end
 if b then
  add(o.coll,{x=clamp_map_cord(x1);y=clamp_map_cord(y2)});
 end
 if c then
  add(o.coll,{x=clamp_map_cord(x2);y=clamp_map_cord(y2)});
 end
 if d then
  add(o.coll,{x=clamp_map_cord(x2);y=clamp_map_cord(y1)});
 end
 return a or b or c or d;
end

function clamp_map_cord(cord)
    return flr(cord)%16*8;
end

this is the code which I have now. I got that code from my friend several months ago but now he is not a member of my project so I can't ask him. This code works on red sprites. I don't understand how.
Now I should add some codes which work for empty(pink) sprite.

I don't understand what is the point of adding 7 or dividing with 8 ..

What should I do?

P#52196 2018-04-30 14:55 ( Edited 2018-04-30 18:57)

Suggest to have a look at the demos/collide sample shipped with Pico.
This will teach you the basic of working with maps and flags (and don't be afraid to start over from clean code, even more so if you don't understand it...)

That being said:

  • x/8 : converts screen coordinates to map coordinates (should be clearer using flr(x/8) as map coordinates are integers)
  • (x+7)/8 : screen coordinates + width (more or less) of your sprite, converts to map coordinates. Seems to be used checks if the box defined by (x,y,x+7,y+7) is colliding with a map cell.
P#52197 2018-04-30 15:27 ( Edited 2018-04-30 19:27)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-16 14:47:26 | 0.007s | Q:19