How do I Translate mouse click on minimap onto screen?

Get help using Construct 2

Post » Wed May 11, 2016 10:14 pm

I got a minimap working with this:
Code: Select all
Map.X+player.X*Map.Width/LayoutWidth
Map.Y+player.Y*Map.Height/LayoutHeight


Now I've been trying to go in reverse, when I click on the minimap I want to spawn a sprite in the location on the world. (I'll be using this sprite for the camera)
I've been trying every which way but can't get it to work. I'm not the best at this math stuff. ;)

I'm currently using this to make sure I'm not changing anything else in the game when trying to create the object:
mouse->cursor is over Map
mouse-> on left button clicked on Map

Hope that makes sense. :)
B
13
S
7
G
5
Posts: 38
Reputation: 3,736

Post » Thu May 12, 2016 11:43 am

EDIT: You must use mouse.AbsoluteX and mouse.AbsoluteY !!

Not sure how are you doing it behind the scene, but what I would do is to get the coordinate of the mouse on the minimap first.

You will need this calculation:

Code: Select all
minimapXCoordinateInPercent = (mouse.x / minimap.Width)
minimapYCoordinateInPercent = (mouse.y / minimap.Height)


This will make it so you will get value between 0 to 1. With this, you can map it to the width and height of the real map:

Code: Select all
realCoordinateXOnMap = minimapXCoordinateInPercent * layoutWidth
realCoordinateYOnMap = minimapYCoordinateInPercent * layoutHeight


As an example, let's say the minimap is a sprite and is 100 x 100 pixels and the hotspot is at (0,0) on the sprite. For simplicity reason, let's assume this minimap is also at (0,0) on a HUD layer.

Now, suppose u clicked on this minimap, u will get the coordinate of the mouse. Suppose mouse.x and mouse.y are at (70,90), you can map this back to the size of the minimap. Assuming your map (or layout) is 2000 x 2000 pixels, we will do the following calculation:

Code: Select all
minimapXCoordinateInPercent = (70 / 100)
minimapYCoordinateInPercent = (90 / 100)


The calculation above will give you 70/100 = 0.7 and 90/100 = 0.9 respectively.

Code: Select all
realCoordinateXOnMap = 0.7 * 2000
realCoordinateYOnMap = 0.9 * 2000


This will give you 0.7*2000 = 1400 and 0.9*2000 = 1800. (1400, 1800) is the position on the real map from the minimap.


This is the gist of what you have to do. There are other issues you will need to take care of such as what if the position of the minimap is not at (0,0) but movable to anywhere on HUD? Or what if the map has a different width and height? But before we dive into these, you should understand the above first.
Last edited by TwinBlazar on Fri May 13, 2016 2:35 am, edited 1 time in total.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,459

Post » Thu May 12, 2016 7:48 pm

Hi TwinBlazar, thanks for the input.
I've tried what you said but it's not working, most likely cause my minimap isn't at 0,0?

Some info:
My minimap is at x 20, y 525 and is 175x175 pixels
My layout is 4500x4500

Image

If I do Mouse.X-Map.X & Mouse.Y-Map.Y I get the mines at the same size of the minimap on the layout.
B
13
S
7
G
5
Posts: 38
Reputation: 3,736

Post » Fri May 13, 2016 12:14 am

EDIT: You should use mouse.AbsoluteX and mouse.AbsoluteY !!

My minimap is at x 20, y 525 and is 175x175 pixels
My layout is 4500x4500


That's why I mentioned the minimap is at (0,0) for simplicity reason. If you have minimap elsewhere on the screen, you will need to take its position into account.

Assuming the hotspot of the minimap is right at the top left corner of the minimap sprite, you can use this:

Code: Select all
minimapXCoordinateInPercent = ((mouse.x - minimap.X) / minimap.Width)
minimapYCoordinateInPercent = ((mouse.y - minimap.Y) / minimap.Height)


Then, take the numbers into my second set of formula in my previous post to get the coordinate on the layout.
Last edited by TwinBlazar on Fri May 13, 2016 2:36 am, edited 1 time in total.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,459

Post » Fri May 13, 2016 1:36 am

Hi TwinBlazar,
I almost got the mouse.x - minimap.X part! ;)

It works good, but only the first time I click on the minimap. After that the camera goes to the bottom right corner of my layout.
I'm using a camera sprite (with a Scroll to) for the camera movement since I'm using 3 differtent types of camera styles.

Image

I'm looking into maybe I have something in my event sheet that is messing it up.
B
13
S
7
G
5
Posts: 38
Reputation: 3,736

Post » Fri May 13, 2016 2:33 am

I have a feeling you are having a problem on mouse coordinate and parallax coordinate, and I forgot one thing (sorry). See Mouse expression (https://www.scirra.com/manual/114/mouse):

Mouse expressions
AbsoluteX
AbsoluteY

Return the position of the mouse cursor over the canvas area in the HTML page. This is (0, 0) at the top left of the canvas and goes up to the window size. It is not affected by any scrolling or scaling in the game.

X
Y

Return the position of the mouse cursor in game co-ordinates. This is (0, 0) at the top left of the layout. It changes to reflect scrolling and scaling. However, if an individual layer has been scrolled, scaled or rotated, these expressions do not take that in to account - for that case, use the layer versions below.

X("layer")
Y("layer")

Return the position of the mouse cursor in game co-ordinates, with scrolling, scaling and rotation taken in to account for the given layer. The layer can be identified either by a string of its name or its zero-based index (e.g. Mouse.X(0)).


The formulae above want the coordinate of the mouse that are absolute on the game screen. Could you try Mouse.AbsoluteX, Mouse.AbsoluteY and please check for their values?
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,459

Post » Fri May 13, 2016 4:18 am

Awesome, thanks TwinBlazar!
Yeah, the Mouse.AbsoluteX, Mouse.AbsoluteY worked. :D
You're right, It was because the first time I was clicking on the minimap I was on the screen area but the second tiime I wasn't.

edit: I also changed the mouse click to left button is down so I can drag on the minimap and the camera scrolls. :)
B
13
S
7
G
5
Posts: 38
Reputation: 3,736


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 9 guests