Jump to content

  • Log In with Google      Sign In   
  • Create Account

Trying to get the ratio correct. Can't match the mouse cursor's X and Y to the white dot.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 tom_mai78101   Members   -  Reputation: 577

Like
0Likes
Like

Posted 14 November 2013 - 07:14 AM

I'm currently researching how to utilize aspect ratio in a magnified area full of pixels. My main focus of this project is to match the mouse cursor to the dot on the screen, while the mouse cursor is moving around in the area. However, I am unable to completely match the mouse cursor to the dot.

 

SrToyA3.png

 

This first image here shows how it is supposed to look when the mouse is moving around in the magnified area. I use the program, PSR, or Problem Steps Recorder for short, in order to capture the mouse cursor and the background screenshot together. 

 

eu1AwgJ.png

 

This second image here shows the same scenario, only that the mouse cursor is moved to the far right and far down of the area. This is just one of the situations where the mouse cursor isn't directly on top of the dot, and the dot moves faster than the mouse cursor.

 

Here's the current code:

public class DeadGame {
	
	public InputHandler inputHandler;
	
	public DeadGame(GameComponent g) {
		inputHandler = new InputHandler();
		
		g.addMouseListener(inputHandler);
		g.addMouseMotionListener(inputHandler);
	}

	public void tick() {
		
	}
	
	public void render(int[] data) {
		/*
		 * Codes that are marked with // are debugging codes; tinkering with the aspect ratios and math to try to get the dot to stay with the cursor.
		 * Apparently, this is incorrect.
		 * 
		 * */
		//		double ratio = (double) GameComponent.WIDTH / (double) GameComponent.HEIGHT;
		//		int x = (int) ((inputHandler.mouseX / GameComponent.SCALE) * ratio);
		//		int y = (int) ((inputHandler.mouseY / GameComponent.SCALE) * ratio);
		//		
		//		System.out.printf("Mouse: %d, %d    (Correct: %d, %d)     Aspect Ratio: %f\n", x, y, (int) (inputHandler.mouseX * ratio), (int) (ratio * inputHandler.mouseY), (double) GameComponent.WIDTH / (double) GameComponent.HEIGHT);
		//
		//		int length = y * (GameComponent.WIDTH / GameComponent.SCALE) + x;
		//		if (length < data.length && length > 0)
		//			data[length] = -1;
		
		int x = inputHandler.mouseX / GameComponent.SCALE;
		int y = inputHandler.mouseY / GameComponent.SCALE;
		
		int length = y * (GameComponent.WIDTH / GameComponent.SCALE) + x;
		if (length < data.length && length > 0)
			data[length] = -1;
	}
}

InputHandler code:

	@Override
	public void mouseMoved(MouseEvent e) {
		mouseX = e.getX();
		mouseY = e.getY();
	}

How do I manage to magnify the area? Below are the codes used to create the effect.

 

Variables used:

	public static final int WIDTH = 640;
	public static final int HEIGHT = 480;
	public static final int SCALE = 4;

Initialization code used for the background area. I used BufferedImage and java.awt.Canvas:

	public GameComponent() {
		threadRunning = false;
		
		Dimension d = new Dimension(WIDTH, HEIGHT);
		this.setPreferredSize(d);
		this.setMaximumSize(d);
		this.setMinimumSize(d);
		this.setSize(d);
		
		image = new BufferedImage(WIDTH / SCALE, HEIGHT / SCALE, BufferedImage.TYPE_INT_ARGB);
		pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
		
		game = new DeadGame(this);
	}

Code used to render it to the back buffer. I suspect that this is the place where the aspect ratio is off entirely here, but I can't really tell. Main suspects are the java.awt.Canvas.getWidth() and java.awt.Canvas.getHeight() functions.

	public void render() {
		BufferStrategy bs = this.getBufferStrategy();
		if (bs == null) {
			this.createBufferStrategy(3);
			bs = this.getBufferStrategy();
		}
		
		int w = this.getWidth();
		int h = this.getHeight();
		
		Graphics g = bs.getDrawGraphics();
		
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, w, h);
		
		clear();
		game.render(pixels);
		
		g.drawImage(image, 0, 0, w, h, null);
		g.dispose();
		bs.show();
	}

So, what do I have to do to fix this issue? Thanks in advance.



Sponsor:

#2 jHaskell   Members   -  Reputation: 1087

Like
0Likes
Like

Posted 14 November 2013 - 07:28 AM

You're likely off because you're performing integer division, where 3/2 = 1, not 1.5.

Try switching to floats and then cast the result to integer at the drawing stage (or, if the drawing routines have overloads that support floats, then don't even bother with the case).



#3 tom_mai78101   Members   -  Reputation: 577

Like
0Likes
Like

Posted 14 November 2013 - 07:38 AM

I did that in the debug codes. That made it even more messed up, letting the white dot spazzing out everywhere.

 

EDIT: Just when I replied back to you, I thought of something about the order of operations in Java. Not math, but the order of method calls in Java.

		frame.setResizable(false);

That piece of code is the culprit. If I do this:

		frame.pack();
		frame.setResizable(false);

The ratio is off. If I do this:

		frame.setResizable(false);
		frame.pack();

Then everything is correct.


Edited by tom_mai78101, 14 November 2013 - 07:41 AM.


#4 Glass_Knife   Moderators   -  Reputation: 4995

Like
2Likes
Like

Posted 14 November 2013 - 12:26 PM

There is something else going on here.  Everything you posted is correct, as far as I can see.  I whipped up a quick test with a Canvas, and using your same code, my dot matches the mouse just fine. 

 

By changing from integer division to floating-point, all that would do is perform division with a remainder that you truncate anyway when you cast to the integer pixel location in the array.  Not doing this changes nothing in this case.  However, the test code you commented out that uses the ratio doesn't make sense.

 

At this point, I would suggest creating a very simple example that does this and see that is works.  Then you can figure out where in your game framework is causing the trouble.


I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#5 tom_mai78101   Members   -  Reputation: 577

Like
0Likes
Like

Posted 15 November 2013 - 04:02 AM

Yeah, I realized the ratio is completely useless. I actually don't know what to do with the ratio. Even then, the actual calculations depends only on integers.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS