• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

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

4 posts in this topic

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

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
Sign in to follow this  
Followers 0