Hi guys
Im sure this will be simple for many of you.
Essentially I'm still working on my zombie slaying epic (a present for a friend) which I started in July last year.
The problem background:
I have built firearms discharge hits through an equation whereby firing a round generates a calculation for each zombie in the vicinity based on the position of the crosshair (mouse.x and mouse.y variables) is assessed against an area bounded by > and < x and y variables. This if statement should add clarity to what I am trying to explain:
if (cZ1.Room==cP1.Room && cZ1.x <mouse.x+15 && cZ1.x> mouse.x-15 && cZ1.y>mouse.y-5 && cZ1.y<mouse.y+80)
cZ1 is character zombie 1.
cP1 is the player character
So it's looking at whether the x and y co ordinates of zombie 1 fall within certain spatial parameters of the mouse cursor position.
The problem:
This works really, really well in situations where the whole room fits into one screen. In larger screens which involve scrolling, the system falls apart. For example:
You may have moved the character a ways off to the right, causing the screen to scroll (as it should). Then you place the weapon crosshair (the mouse cursor) over a zombie and pot it one. As far as the zombie co ordinates (cZ1.x and cZ1.y). cZ1 may correctly have the x co ordinate of 500 (for example) whereas the placement on the screen will cause the mouse cursor when placed over said zombie to have a x co ordinate of 20 (for example).
I am hoping to find a way to resolve this. I suspect that the solution involves somehow translating the mouse x and y variables into a new set of variables built around the room positioning. Have been working on this problem for a couple of weeks and no joy I'm afraid.
I apologise if I have not explained the problem clearly. Any help would be dearly appreciated.
Pharlap
That drove me crazy for a while! finally realized mouse co-ords are within the current viewport not the same as room co-ords... You need to adjust the character's xy to match the screen's xy...
code/
cZ1.y - GetViewportY() > mouse.y - 5 && cZ1.y - GetViewportY() < mouse.y + 80;
/code
the Viewport returned is the top left corner of the screen, give that a try.
Yep, in general: to get from room coords to screen coords, subtract the viewport.
To get from screen to room coords, add it.
Easy to memorize if you remember that room coords can be a lot greater than screen coords.
Excellent, thank you my friends, I will try it over the weekend and let you know hot it goes.... its the last obstacle to game completion