How do I Translate mouse click on minimap onto screen?

0 favourites
  • 7 posts
From the Asset Store
Click! Click! 136 click sounds for your video games or media projects
  • I got a minimap working with this:

    Map.X+player.X*Map.Width/LayoutWidth
    Map.Y+player.Y*Map.Height/LayoutHeight[/code:2qq0j5jj]
    
    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 [b]Map[/b]
       mouse-> on [b]left[/b] button clicked on [b]Map[/b]
    
    Hope that makes sense.
  • 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:

    minimapXCoordinateInPercent = (mouse.x / minimap.Width) 
    minimapYCoordinateInPercent = (mouse.y / minimap.Height)[/code:3uhu9216]
    
    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:3uhu9216]realCoordinateXOnMap = minimapXCoordinateInPercent * layoutWidth
    realCoordinateYOnMap = minimapYCoordinateInPercent * layoutHeight[/code:3uhu9216]
    
    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:3uhu9216]minimapXCoordinateInPercent = (70 / 100) 
    minimapYCoordinateInPercent = (90 / 100)[/code:3uhu9216]
    
    The calculation above will give you 70/100 = 0.7 and 90/100 = 0.9 respectively.
    
    [code:3uhu9216]realCoordinateXOnMap = 0.7 * 2000
    realCoordinateYOnMap = 0.9 * 2000[/code:3uhu9216]
    
    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.
  • 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

    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.

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

    [quote:1y5qhylh]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:

    minimapXCoordinateInPercent = ((mouse.x - minimap.X) / minimap.Width) 
    minimapYCoordinateInPercent = ((mouse.y - minimap.Y) / minimap.Height)[/code:1y5qhylh]
    
    Then, take the numbers into my second set of formula in my previous post to get the coordinate on the layout.
  • 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.

    I'm looking into maybe I have something in my event sheet that is messing it up.

  • 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):

    [quote:14s7i079]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?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Awesome, thanks TwinBlazar!

    Yeah, the Mouse.AbsoluteX, Mouse.AbsoluteY worked.

    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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)