Jump to content

View more

Image of the Day

Working on an auto spawn system. #gamedev #indiedev #screenshotsaturday https://t.co/Mm2kfekz7b
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

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

4: Adsense

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 asperatology   Members   


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.




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.

#2 jHaskell   Members   


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 asperatology   Members   


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.


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, 14 November 2013 - 07:41 AM.

#4 Glass_Knife   Moderators   


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"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

#5 asperatology   Members   


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.