no1no Posted May 24, 2016 Share Posted May 24, 2016 Hi, I'm a Phaser beginner and I'm trying to make a chess-like game. I created a background sprite with a board image. The first thing I want to do is getting the coordination of position on the board. I get it by pointing the mouse to each position and printing this.game.input.x,y in update() but it seems this is the world coordination and I can't use it to set the piece position (using chessPiece.position.set). Is there anyway to convert the world position to the local position? Link to comment Share on other sites More sharing options...
no1no Posted May 25, 2016 Author Share Posted May 25, 2016 My thread is at the end of page 1 after being approved so I hope no one mind a single bump to the top. *Bumped* Link to comment Share on other sites More sharing options...
drhayes Posted May 25, 2016 Share Posted May 25, 2016 Yes. You're trying to map the game coordinates to your chess coordinates. It'll take some math. If you think about how your game is (for example) 800 pixels wide, and a chess board is 8 tiles wide, that means each tile is 800/8 === 100 pixels wide. So if the user clicks in pixel 234, that's 234/100 === 2nd tile. Link to comment Share on other sites More sharing options...
no1no Posted May 25, 2016 Author Share Posted May 25, 2016 Thanks. But that's not really what I want. I could calculate the position using math but I figure I would use the function of converting the world position to local position in the future so I was asking anyway. For example in a RTS game I could spawn an unit at the mouse position on the board. I tried this.game.world.toLocal(new PIXI.Point(this.game.input.x, this.game.input.y), parentObject) but it's incorrect. Link to comment Share on other sites More sharing options...
drhayes Posted May 27, 2016 Share Posted May 27, 2016 Phaser.Input similarly has getLocalPosition but I've never used it so I'm not sure what it does. The description sounds like what you're looking for? But, truthfully, I don't understand what toLocal and toGlobal do, either. ( = no1no 1 Link to comment Share on other sites More sharing options...
lewster32 Posted May 27, 2016 Share Posted May 27, 2016 Local position is just object position subtracted from world position, so if your world starts at 0, 0 and your object is at 100, 100, then if your cursor is at 150,150 (world position) then it's at 50,50 relative to the object. no1no 1 Link to comment Share on other sites More sharing options...
no1no Posted May 28, 2016 Author Share Posted May 28, 2016 I see. Thank you for your answer, I can write a function base on it. Link to comment Share on other sites More sharing options...
in mono Posted May 28, 2016 Share Posted May 28, 2016 You may also want to take the sprite's anchor into account if you decide to use something different than (0, 0). Link to comment Share on other sites More sharing options...
Recommended Posts