[MT] "Line of Sight" tool - general advice

The RPTools applications are written in Java. If you're interested in contributing to any project here by submitting patches to the source code, this is the forum to ask questions about how to do so. Please put the two-letter tool name abbreviation in your thread Title. To enter this group, go to the Usergroups page of your User Control Panel and join the Java Developer group.

Moderators: dorpond, trevor, Azhrei

Post Reply
User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

[MT] "Line of Sight" tool - general advice

Post by Rumble »

Okay, so as a project inspired by Plothos' thread in Feature Requests, I'm playing around with trying to create a "targeting tool" (or, more accurately, a "Line of Sight" tool).

The basic idea is that you can activate this tool, and then if you click on a token, you can choose to draw "sightlines" from the the token's center, or one of the four corners of the area it occupies, to all of the corners on a target token. This is relatively game-specific but it seemed like an interesting project to push further into MT's source with.

In any case, I've got all parts of it working, except one thing I can't seem to figure out. The basic procedure is that if a user Ctrl-leftclicks on a token, the LOS tool places the "origin" point of the sightlines at the nearest grid vertex. That was easy; there's a handy method for that.

When you then right click on the target, you can select "Check Line of Sight" from a popup menu, and it will draw lines from the origin point to the four corners of the target's boundaries (boundaries at this point; cell footprint, hopefully, in the future). This works great, up until I drag the map. At that point, when I click on a token to start a Line of Sight check, it selects the nearest vertex, but when I actually draw the sightlines, they originate from a point that is offset from that vertex (by, I assume, the map offset).

So I know that it involves offsets somehow, but here's where I'm a bit lost:

I have ScreenPoints, CellPoints, and ZonePoints, and I have view offset and grid offset, and I'm trying to figure out what combination of which is what I need. Here's the general process:

1. The user ctrl-clicks on a token. This uses what is basically "snap-to-grid" to select the nearest vertex: it starts as a ScreenPoint, is converted to a ZonePoint, used to find the nearest vertex, and then converted back into a ScreenPoint in its final form.

Code: Select all

selectedCorner = new ScreenPoint(e.getX(), e.getY());
zp = selectedCorner.convertToZoneRnd(renderer);
zp = renderer.getZone().getNearestVertex(zp);
selectedCorner = ScreenPoint.fromZonePoint(renderer, zp);
 
2. The right-click selection of "check line of sight" triggers the drawing of the lines, starting from selectedCorner and going to the four corners of the target square.

Code: Select all

Zone zone = renderer.getZone();
Grid grid = zone.getGrid();
int offY = renderer.getViewOffsetY();
int offX = renderer.getViewOffsetX();
        
g.setColor(Color.YELLOW);
g.translate(offX, offY);
g.scale(renderer.getScale(), renderer.getScale());
        
if (isCheckingLOS){
   if(losTarget == null){
        return;
   }
  
  g.setColor(Color.WHITE);
  g.drawLine((int)selectedCorner.getX(),
                  (int)selectedCorner.getY(), 
                  (int)losTarget.getBounds(zone).getMaxX(),  
                  (int)losTarget.getBounds(zone).getMaxY());

// 3 more lines drawn here, to the relevant corners

            g.draw(losTarget.getBounds(zone));
            g.dispose();
}
 
This draws the lines successfully, and if I do all this without moving the map, then the lines are drawn and if I move the map, all points stay "attached" to the tokens they were created on (that is, if I move the map around, the tokens and the line of sight drawing move together as desired).

However, if I move the map, and then try a new "selectedCorner" (for instance, to check LOS from another token), it will draw the origin from some point offset from where I want it to be.

What I'm not sure is where in the code I need to be putting the offsets, in order to have it be "immune" to map dragging (that is, so that the starting point is always where I expect it to be, and so is the ending point).


Anyway, I've attached the whole targeting tool code, too, if anyone wants to help a n00b out. Hopefully you'll at least see what I was trying to do, and can give me advice.

Remember, I have very little formal experience with programming, so use small words and act as though you are speaking to someone educated in the Penal System.

Thanks!
Attachments
rumble_targ_tool.txt
(8.42 KiB) Downloaded 113 times

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: [MT] "Line of Sight" tool - general advice

Post by Azhrei »

Wish I could help, but Trevor will likely have to respond to this. I'm not familiar at all with the coordinate systems used by MapTool's imaging systems: Zone, Cell, and Screen.

I would assume that Zone coordinates are relative to the map and need to be translated to Screen coordinates for drawing.

Sorry. :(

User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

Re: [MT] "Line of Sight" tool - general advice

Post by Rumble »

Azhrei wrote:Wish I could help, but Trevor will likely have to respond to this. I'm not familiar at all with the coordinate systems used by MapTool's imaging systems: Zone, Cell, and Screen.

I would assume that Zone coordinates are relative to the map and need to be translated to Screen coordinates for drawing.

Sorry. :(
No worries. Like I said, I've almost got it, but there's apparently something I missed in the conversion between screen and zone, and I can't figure it out.

It's frustrating, because it's so close, and I avoided this problem with the Terrain Tool (although that tool is a bit different than the line drawing stuff). It may be that I need to set on transform for the drawn lines, but another for the origin point. Dunno.

But that's cool - it can wait. Trevor will, I'm sure, illuminate me when he gets a chance.

User avatar
trevor
Codeum Arcanum (RPTools Founder)
Posts: 11311
Joined: Mon Jan 09, 2006 4:16 pm
Location: Austin, Tx
Contact:

Re: [MT] "Line of Sight" tool - general advice

Post by trevor »

For context, there are three specific point systems in MT:

ScreenPoint: representing a point withing the MT map frame, these can be used directly with the paint commands

ZonePoint: represents a position in the map space/world. The screen shows a specific rectangle view of the whole map, a zonepoint is the coordinate system for the map where one zone point is equal to one map point

CellPoint: This is a more abstract system, it represents the virtual grid that is placed onto the map.

Essentially, a ZonePoint is map space, CellPoint is grid space on the map, and ScreenPoint is the rectangle view onto the map.

Looking at the code, you correctly get a screen point for the starting position, but iirc the line:

losTarget.getBounds(zone).getMaxX()

returns a point in zone space. So technically what you would do is create a ZonePoint with the getMaxY() and getMaxX() then convert it to a ScreenPoint, the use those x, y values.
Dreaming of a 1.3 release

User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

Re: [MT] "Line of Sight" tool - general advice

Post by Rumble »

trevor wrote:For context, there are three specific point systems in MT:

ScreenPoint: representing a point withing the MT map frame, these can be used directly with the paint commands

ZonePoint: represents a position in the map space/world. The screen shows a specific rectangle view of the whole map, a zonepoint is the coordinate system for the map where one zone point is equal to one map point

CellPoint: This is a more abstract system, it represents the virtual grid that is placed onto the map.

Essentially, a ZonePoint is map space, CellPoint is grid space on the map, and ScreenPoint is the rectangle view onto the map.

Looking at the code, you correctly get a screen point for the starting position, but iirc the line:

losTarget.getBounds(zone).getMaxX()

returns a point in zone space. So technically what you would do is create a ZonePoint with the getMaxY() and getMaxX() then convert it to a ScreenPoint, the use those x, y values.

Ahhhhhh. That is very helpful! I will give that a try, and see what develops!

Thanks!

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: [MT] "Line of Sight" tool - general advice

Post by Azhrei »

And Rumble, while you're in there, how about copy/paste'ing Trevor's post into a comment somewhere?! Maybe at the beginning of the Zone class?

I'll get javadoc into the source code if it's the last thing I do!!!

:mrgreen:

User avatar
trevor
Codeum Arcanum (RPTools Founder)
Posts: 11311
Joined: Mon Jan 09, 2006 4:16 pm
Location: Austin, Tx
Contact:

Re: [MT] "Line of Sight" tool - general advice

Post by trevor »

Azhrei wrote: I'll get javadoc into the source code if it's the last thing I do!!!
It might be worth having in external documentation, javadoc is great and all, if you happen to know exactly where to look. Meta level concepts like the 3 spaces are probably better in a wiki or something. Meh, maybe not. I don't like editing wikis.
Dreaming of a 1.3 release

User avatar
aku
Dragon
Posts: 856
Joined: Wed Nov 15, 2006 9:03 am
Contact:

Re: [MT] "Line of Sight" tool - general advice

Post by aku »

of course, having it SOMEWHERE, is better than nowhere at all :D

User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

Re: [MT] "Line of Sight" tool - general advice

Post by Rumble »

Victory! You set me on the right track there. Turns out I needed to use ZonePoints (so that the origin and destination tracked to the map) and some back-and-forth between Zone and Screen, but it now works exactly as I hoped.

As for documentation...one thing at a time, here. :D

User avatar
trevor
Codeum Arcanum (RPTools Founder)
Posts: 11311
Joined: Mon Jan 09, 2006 4:16 pm
Location: Austin, Tx
Contact:

Re: [MT] "Line of Sight" tool - general advice

Post by trevor »

This is exactly why I created the three spaces. At their core, they are all just an x,y coordinate, but very quickly I was confusing which "space" I was working in and the points never matched up.

The distinction of the ZonePoint, SpacePoint, and CellPoint was specifically to make it more clear what coordinate system that was a point of.
Dreaming of a 1.3 release

Post Reply

Return to “Java Programming Info”