Sign in to follow this  
Followers 0
embatbai

input doesn't work when using camera

2 posts in this topic

title says it all. I have a camera focused on the character and this function:
state.clicked = function(){
this.isClicked = true;
this.heart.x = this.game.input.x;
}

The heart doesn’t line up with where I clicked and changes in relation to the character when she moves.

Share this post


Link to post
Share on other sites

This sounds very similar to an issue that was raised elsewhere recently, so we have some good answers.

 

The camera is actually a transformation matrix applied to the entire scene. You can rotate and scale it. You can scale it a different amount on different axes and then rotate it, causing shear. While you might never want to do this, it will make simple input mapping solutions break down and cry.

 

Luckily, there are solutions.

 

Camera.transformPoint( Kiwi.Geom.Point ) will take a point in screen space (such as input values) and tell you where that is in world space.

 

There’s also an inverse, Camera.transformPointToScreen( point ), which may be useful for assessing whether an object is on screen or not.

 

Finally, input components are available on Sprites, which send a Signal on input (sprite.input.onRelease.add(), etc). These do respond correctly to camera transformations, even the extreme sort with scaling etc. You can get a fair amount of data out of this. Basically, you pass a function to the Signal, and it’s called when that signal is triggered by the input system. A bare anonymous function with no parameters works fine, e.g. function() { Kiwi.Log.log( "#debug", "Input received" ); }.

 

However, the input system will pass it a couple of optional parameters, which the function can use: a Sprite and a Cursor (just the sprite and cursor that are interacting, really). You can see these working in the console by adding parameters to the function passed to the signal: function( sprite, cursor ) { Kiwi.Log.log( "#debug", "Input received", sprite, cursor ); }. In particular, the cursor comes with a lot of data about its position on the screen, what modifiers are in use, how long it’s been down etc. Note that this information does not take any camera transformations into account, so it will still have to be transformed by the above methods; however, the camera is consulted when determining whether the cursor and the sprite interacted at all.

 

Hope this helps!

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

  • Recently Browsing   0 members

    No registered users viewing this page.