Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [FIXED] Mouse Coords in a tiled world.  (Read 599 times)
0 Members and 1 Guest are viewing this topic.
Offline SwordsMiner

JGO Coder


Medals: 3
Projects: 2
Exp: 1 year


The one and only.


« Posted 2014-04-15 23:23:40 »

Hey guys. I need some help with my platformer game, you see- I cannot figure out at all how to find where the mouse is on the screen. You may be saying:
1  
Well, just add them mouse coords and camera coords, then it will work.

but no. It doesnt. I have no clue why! Let me keep in mind, it works if I use the player's x,y I just cant add the mouse factor into that.

Since I probally havent given enough info on the code itself, you can generalize, but this is what ive tried so far.

WORKS.
1  
actionPos =  new Point(x,y);


DOESNT WORK. WELL, SORT OF
1  
actionPos =  new Point(mouse.x+camera.x,mouse.y+camera.y); //camera is just the players x and y with half the screen subtracted


Any suggestions? Now here is the silly thing. It works, but the action position races ahead of the cursor. Basically at 0,0 on the screen, where camera.x, camera.y is, it is 100% accurate, but 2 pixels away, it may be 4-6 pixels away, at about half the screen its a quarter screen away from the cursor, etc.

I hope you guys can help. Ill give any more stuff on the issue if its needed! This is urgent!

If I made you laugh, helped you at all, or did something cool, I only ask that you smash that appreciate button with your nose.
Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #1 - Posted 2014-04-15 23:59:32 »

Are you doing any sort of scaling when you render that you haven't accounted for when calculating your mouse/world position?

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline achap1989

Senior Newbie


Medals: 1



« Reply #2 - Posted 2014-04-16 00:02:09 »

IF you are scaling your tiles you will need to dived the x or y position by the scale amount.

Example if I am scaling by 32 I would take x/32  or y/32
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SwordsMiner

JGO Coder


Medals: 3
Projects: 2
Exp: 1 year


The one and only.


« Reply #3 - Posted 2014-04-16 00:05:30 »

IF you are scaling your tiles you will need to dived the x or y position by the scale amount.

Example if I am scaling by 32 I would take x/32  or y/32
Are you doing any sort of scaling when you render that you haven't accounted for when calculating your mouse/world position?

Thats figured out. I need the mouse position. Translating it to the tiles themselves works. Just for the sake of it though, these are the methods that add blocks

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void setBlockAtPixel(int x, int y, Block blockFromID) {
      x = x/Block.blockSize.width;
      y = y/Block.blockSize.height;
      final Point cpos = new Point((int)(x/Chunk.xa), (int)(y/Chunk.ya));
      final Chunk chunk = chunks[cpos.x][cpos.y];
      Point lpos = new Point(x%Chunk.xa, y%Chunk.ya);
      setBlock(chunk.foreground, cpos.x, cpos.y, lpos.x, lpos.y, blockFromID);
   }
public void setBlock(Block[][] blocks,int cx, int cy, int x, int y, Block b){
      final Point cpos = new Point(cx,cy);
      Point lpos = new Point(x%Chunk.xa, y%Chunk.ya);
      b.cpos = cpos;
      b.lpos = lpos;
      b.updateCoords();
      blocks[lpos.x]
            [lpos.y] = b;
   }





If I made you laugh, helped you at all, or did something cool, I only ask that you smash that appreciate button with your nose.
Offline LiquidNitrogen
« Reply #4 - Posted 2014-04-16 00:06:19 »

my program has an issue like that when the window is different size than the view port, the drawing is scaled up and down nicely, but the mouse co-ords are still relative to the physical screen resolution.
Offline SwordsMiner

JGO Coder


Medals: 3
Projects: 2
Exp: 1 year


The one and only.


« Reply #5 - Posted 2014-04-16 00:07:04 »

my program has an issue like that when the window is different size than the view port, the drawing is scaled up and down nicely, but the mouse co-ords are still relative to the physical screen resolution.
Already fixed by the engine, no matter the screen size the end will always be 1920 or 1080 depending on the axis. The engine also tries to render it at this however the field of view is  not.
1  
2  
3  
4  
public void initCamera() {
      setFOV((int)(240*1.5),(int)(135*1.5)); //lazy
     glEnable(GL_TEXTURE_2D);
   }

If I made you laugh, helped you at all, or did something cool, I only ask that you smash that appreciate button with your nose.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #6 - Posted 2014-04-16 03:30:48 »

Quote
Already fixed by the engine, no matter the screen size the end will always be 1920 or 1080 depending on the axis. The engine also tries to render it at this however the field of view is  not.

Just want to point out, that statement is completely wrong. You cannot have a window that is less than 1920x1080 pixels still take up 1920x1080 pixels. If a window is 800x600, how the hell could you still have 1920x1080 pixels within the window? Its physically impossible.

Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #7 - Posted 2014-04-16 04:13:29 »

Maybe he means in-game units?
Online HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #8 - Posted 2014-04-16 04:47:09 »

my program has an issue like that when the window is different size than the view port, the drawing is scaled up and down nicely, but the mouse co-ords are still relative to the physical screen resolution.

Try calling glViewport with the window size.

Offline trollwarrior1
« Reply #9 - Posted 2014-04-16 04:49:49 »

You might need to be doing x-camera.x,y-camera.y instead of addition.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SwordsMiner

JGO Coder


Medals: 3
Projects: 2
Exp: 1 year


The one and only.


« Reply #10 - Posted 2014-04-16 21:18:25 »

Im very happy to announce it was fixed. For anyone wondering, the engine was calculating the mouse position wrong, basically as liquid said, resizing makes a difference. Well- my ortho view does not render the amount of pixels the window has, it only shows a fixed amount and thats why my windows resize so nicely (unlike terraria and minecraft, I think they dont resize as well).

Heres the code I fixed it with.

1  
actionPos =  new Point((int)((((double)GDisplay.camera.getFovX()/(double)GDisplay.getDisplaySize().width)*Mouse.getX())+GDisplay.camera.getX()),(int)((((double)GDisplay.camera.getFovY()/(double)GDisplay.getDisplaySize().height)*Mouse.getY())+GDisplay.camera.getY()));



If I made you laugh, helped you at all, or did something cool, I only ask that you smash that appreciate button with your nose.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #11 - Posted 2014-04-16 21:23:04 »

Quote
it only shows a fixed amount
You mean it doesn't scale the objects in game, correct? I would highly advise against this. If someone plays in a different screen ratio, all your objects are going to look distorted. Minecraft and Terraria handle resizing the correct way by scaling, I have no idea why that would be a bad thing.

Offline SwordsMiner

JGO Coder


Medals: 3
Projects: 2
Exp: 1 year


The one and only.


« Reply #12 - Posted 2014-04-16 21:27:46 »

Quote
it only shows a fixed amount
You mean it doesn't scale the objects in game, correct? I would highly advise against this. If someone plays in a different screen ratio, all your objects are going to look distorted. Minecraft and Terraria handle resizing the correct way by scaling, I have no idea why that would be a bad thing.

I tested it on the normal ratios. They all look perfect. All I am saying is your FOV wont increase or decrease if you double or halve the resolution.

If I made you laugh, helped you at all, or did something cool, I only ask that you smash that appreciate button with your nose.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #13 - Posted 2014-04-16 21:30:21 »

Ah I see what you mean. That doesn't happen with Minecraft or Terraria, but no matter, I won't derail your thread anymore.


Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (34 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (31 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (44 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!