Jump to content

How to convert mouse coordinate to RS position


lisabe96

Recommended Posts

Entity hover debug shows you which tile you are hovering over if thats what you need.

 

That's exactly what I need but I need it in code.

Maybe this example will be more clear:

void mouseMoved(MouseEvent e) {
  Tile tile = new Tile(mouseX, mouseY);
  log(tile.getPosition());
}

This obvious doesn't work, I'm after how I would get this working within osbot

Edited by lisabe96
Link to comment
Share on other sites

That's exactly what I need but I need it in code.

Maybe this example will be more clear:

void mouseMoved(MouseEvent e) {
  Tile tile = new Tile(mouseX, mouseY);
  log(tile.getPosition());
}

This obvious doesn't work, I'm after how I would get this working within osbot

Not 100% since im not where I can freely test. The solution might be 

mouse().getArea();

As I said I recall doing this at one point in scripting but I'd have to test it.

Link to comment
Share on other sites

Perhaps you can do something like:

 

for each tile in myplayer.getArea(radius):

  if tile.polygon contains (mx, my):

    return tile

not the most efficient solution tho

I hope to find something more efficient, but I'll give it a try if I can't figure out anything else

 

 

Not 100% since im not where I can freely test. The solution might be 

mouse().getArea();

As I said I recall doing this at one point in scripting but I'd have to test it.

This would return java.awt.geom.Area

Link to comment
Share on other sites

I hope to find something more efficient, but I'll give it a try if I can't figure out anything else

 

 

This would return java.awt.geom.Area

I briefly looked at org.osbot.core.debug.HoverDebug and I think it does the same thing, looping through every tile but it compares points in a polygon.

Link to comment
Share on other sites

Got it working :D

It doesn't look good when the camera is tilted as it draws the screen and not the game world obviously.

But it works.

 

MQ0IhH1.png

private List<Position> tiles = new ArrayList<>();


@Override
public void mouseDragged(MouseEvent e) {
	handle(e);
}

@Override
public void mouseClicked(MouseEvent e) {
	handle(e);
}

private void handle(MouseEvent e) {
	log(e.getX() + ", " + e.getY());
		
	Position pos = getPosition(e.getPoint());
		
	if (exists(pos)) {
		tiles.remove(pos);
		return;
	}
	tiles.add(pos);
}

@Override
public void onPaint(Graphics2D graphics) {
	Graphics2D g = (Graphics2D) graphics;
	g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF));
		
	g.setColor(Color.GREEN);
		
	if (!tiles.isEmpty()) {
		for (Position t : tiles) {
			if (t.isVisible(getBot())) {
				g.draw(t.getPolygon(getBot()).getBounds());
			}
		}
	}
}

private Position getPosition(Point mousePosition) {
	for(int x = 0; x < 104; x++) {
		for(int y = 0; y < 104; y++) {
			Position pos = new Position(getMap().getBaseX()+x, getMap().getBaseY()+y, getMap().getPlane());
			if(pos.isVisible(getBot()) && pos.getPolygon(getBot()).contains(mousePosition)) {
				return pos;
			}
		}
	}	
	return null;
}
	
private boolean exists(Position pos) {
	if (!tiles.isEmpty()) {
		for (Position tile : tiles) {
			if (tile.equals(pos)) {
				return true;
			}
		}
	}
	return false;
}
Link to comment
Share on other sites

 

Got it working biggrin.png

It doesn't look good when the camera is tilted as it draws the screen and not the game world obviously.

But it works.

 

MQ0IhH1.png

private List<Position> tiles = new ArrayList<>();


@Override
public void mouseDragged(MouseEvent e) {
	handle(e);
}

@Override
public void mouseClicked(MouseEvent e) {
	handle(e);
}

private void handle(MouseEvent e) {
	log(e.getX() + ", " + e.getY());
		
	Position pos = getPosition(e.getPoint());
		
	if (exists(pos)) {
		tiles.remove(pos);
		return;
	}
	tiles.add(pos);
}

@Override
public void onPaint(Graphics2D graphics) {
	Graphics2D g = (Graphics2D) graphics;
	g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF));
		
	g.setColor(Color.GREEN);
		
	if (!tiles.isEmpty()) {
		for (Position t : tiles) {
			if (t.isVisible(getBot())) {
				g.draw(t.getPolygon(getBot()).getBounds());
			}
		}
	}
}

private Position getPosition(Point mousePosition) {
	for(int x = 0; x < 104; x++) {
		for(int y = 0; y < 104; y++) {
			Position pos = new Position(getMap().getBaseX()+x, getMap().getBaseY()+y, getMap().getPlane());
			if(pos.isVisible(getBot()) && pos.getPolygon(getBot()).contains(mousePosition)) {
				return pos;
			}
		}
	}	
	return null;
}
	
private boolean exists(Position pos) {
	if (!tiles.isEmpty()) {
		for (Position tile : tiles) {
			if (tile.equals(pos)) {
				return true;
			}
		}
	}
	return false;
}

 

 

Try doing this instead:

g.drawPolygon(t.getPolygon(getBot()));
  • Like 1
Link to comment
Share on other sites

I'm not sure drawPolygon will work if the position's Z coordinate is not 0 (could be wrong).

 

If this is the case you could use:

Polygon p = position.getPolygon(getBot(), myPosition().getZ());
if(p != null) g2d.draw(p);

Which I can confirm DOES work on with all planes.

 

Cheers

Thanks, will use.

Better avoid possible problems :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...