Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 2075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.




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. 




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();

	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:

	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);
		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) {
			bs = this.getBufferStrategy();
		int w = this.getWidth();
		int h = this.getHeight();
		Graphics g = bs.getDrawGraphics();
		g.fillRect(0, 0, w, h);
		g.drawImage(image, 0, 0, w, h, null);

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

Share this post

Link to post
Share on other sites

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

Share this post

Link to post
Share on other sites

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.


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


The ratio is off. If I do this:


Then everything is correct.

Edited by tom_mai78101

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!