Jump to content

How to convert mouse coordinate to RS position


Recommended Posts

Posted (edited)

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
Posted

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.

Posted

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

Posted

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;
}
Posted

 

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
Posted

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 :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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