Strange Logic Error
Hi, I am writing a mousebased tile engine map editor for a game I am working on but I am having trouble converting the mouse coordinates on the screen to coordinates in the map array. When the camera is at 1,1 the code below works but if I move it to 2,1 the x coordinate becomes one tile off, and on 3,1 it becomes two off etc... I cannot figure out why this is. Is there any large obvouse errors in the code below?
if (mouse_b & 1)
{
mousepos = mouse_pos;
x = (int)(mousepos >> 16 & 65535)/16;
y = (int)(mousepos & 65535)/16;
map[camera.x + x][camera.y + y][1].tile=0;
}
-- FlySwat99
-- FlySwat99
Aren''t mouse values reported relative to the screen? I''m pretty sure they are... And that would mean you need to have translation between where the screen is and the absolute location of your map tiles.
Ahh, but you do not notice that I have taken the mouse x and y coordinates and divided them by 16, thus giving it the tile coordinate on the screen. Then when you add the current camara location to the x and y you should have the proper tile selected...Unfortantly it doesnt seem to work correctly.
-- FlySwat99
-- FlySwat99
-- FlySwat99
Since I don't know the specifics of how your map editor works, the solution to your problem is quite simple.
If your tiles are 16x16 detecting where the tiles are supposed to be placed in the array is pretty easy.
Say you store the offsets of the map in ,iMapOffsetX and iMapOffsetY and you also store the currently selected tile in iCurrentTile, all you need to do is too add these numbers to the coords then assign that part in the array to iCurrentTile. Like so.
Hope this helps! If not, let me know.
------------------------------
"I'm a decorated astronaut, I don't make those kind of mistakes."
"Oh now wait a minute. Look I'll show ya. I'll enter the same calculations using what we like to call 'The Right Way'."
-Rem
Edited by - Remnex on March 8, 2001 1:54:35 PM
If your tiles are 16x16 detecting where the tiles are supposed to be placed in the array is pretty easy.
Say you store the offsets of the map in ,iMapOffsetX and iMapOffsetY and you also store the currently selected tile in iCurrentTile, all you need to do is too add these numbers to the coords then assign that part in the array to iCurrentTile. Like so.
|
Hope this helps! If not, let me know.
------------------------------
"I'm a decorated astronaut, I don't make those kind of mistakes."
"Oh now wait a minute. Look I'll show ya. I'll enter the same calculations using what we like to call 'The Right Way'."
-Rem
Edited by - Remnex on March 8, 2001 1:54:35 PM
------------------------------"I'm a decorated astronaut, I don't make those kind of mistakes.""Oh now wait a minute. Look I'll show ya. I'll enter the same calculations using what we like to call 'The Right Way'."-RemZirem Software
I may be wrong, but to me that looks just the same as his original code.
Harry.
Harry.
Harry.
Hmm.. that is disturbing. The question is, why does mine work and his does not?
------------------------------
"I''m a decorated astronaut, I don''t make those kind of mistakes."
"Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''."
-Rem
------------------------------
"I''m a decorated astronaut, I don''t make those kind of mistakes."
"Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''."
-Rem
------------------------------"I'm a decorated astronaut, I don't make those kind of mistakes.""Oh now wait a minute. Look I'll show ya. I'll enter the same calculations using what we like to call 'The Right Way'."-RemZirem Software
Well, I plugged in your code which was basicly just mine in a different order and it reveals the same results....
I am postive the error is not in any of my other code, It is a very strange one indeed. Would you like to see my source code?
-- FlySwat99
I am postive the error is not in any of my other code, It is a very strange one indeed. Would you like to see my source code?
-- FlySwat99
-- FlySwat99
Fixed!
It was a stupid error but my mouse polling routines were crosses so I was sending X to y and Y to X. I realized this after I discovered that it only works when both Camera values were the same.
Here is the working source.
Thanks for all your help people.
-- FlySwat99
Edited by - FlySwat99 on March 8, 2001 7:52:30 PM
It was a stupid error but my mouse polling routines were crosses so I was sending X to y and Y to X. I realized this after I discovered that it only works when both Camera values were the same.
Here is the working source.
|
Thanks for all your help people.
-- FlySwat99
Edited by - FlySwat99 on March 8, 2001 7:52:30 PM
-- FlySwat99
Hmm... interesting. Well I am glad that you figured it out!
------------------------------
"I''m a decorated astronaut, I don''t make those kind of mistakes."
"Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''."
-Rem
------------------------------
"I''m a decorated astronaut, I don''t make those kind of mistakes."
"Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''."
-Rem
------------------------------"I'm a decorated astronaut, I don't make those kind of mistakes.""Oh now wait a minute. Look I'll show ya. I'll enter the same calculations using what we like to call 'The Right Way'."-RemZirem Software
Ah, you''ve just discovered a common error in programming known I believe as "cross-indexing". Generally you just try to use meaningful variable names to reduce this problem (less likely you''ll confuse the two), but in this case x and y are accepted as standard variables for a position, so this was to be one of those errors you just have to check twice.
Ut
Ut
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement