[SOLVED] Detecting Object Touch Problem. Body Position is different than the touched Position

Topics: Developer Forum, Project Management Forum, User Forum
Nov 2, 2012 at 10:06 AM

Can you please help me also with this? If I add a body for example a CircleShape body, then I add a touch event for the screen. why the body's position origin is in the middle of the screen? How can I detect touch then? please.

Nov 2, 2012 at 11:11 AM
Edited Nov 2, 2012 at 11:11 AM

// HERES MY CODE

	private Border _border;
        private Body _circle;
        private Sprite _circleSprite;
        private DebugViewXNA _debugView;
        private Vector2 _screenCenter;
        private Vector2 _cameraPosition;
        private float _meterInPixels = 16f;

        public override void LoadContent()
        {
            base.LoadContent();

            _screenCenter = new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2f, ScreenManager.GraphicsDevice.Viewport.Height / 2f);
            _cameraPosition = Vector2.Zero;

            World.Gravity = new Vector2(0f, 9.82f);
            _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
            _circle = BodyFactory.CreateRectangle(World, ConvertUnits.ToSimUnits(20f), ConvertUnits.ToSimUnits(20f), 1f);
            _circle.BodyType = BodyType.Dynamic;
            _circle.Restitution = 0.4f;
            _circleSprite = new Sprite(ScreenManager.Assets.TextureFromShape(_circle.FixtureList[0].Shape,
                                                                                MaterialType.Waves,
                                                                                Color.Orange, 1f));
            _debugView = new DebugViewXNA(World);
            _debugView.AppendFlags(DebugViewFlags.DebugPanel);
            _debugView.DefaultShapeColor = Color.White;
            _debugView.SleepingShapeColor = Color.LightGray;
            _debugView.LoadContent(ScreenManager.GraphicsDevice, ScreenManager.Content);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            TouchCollection touchLocations = TouchPanel.GetState();
            foreach (TouchLocation touchLocation in touchLocations)
            {
                if (touchLocation.State == TouchLocationState.Pressed)
                {
                    string msg =

                        "touchX: " + touchLocation.Position.X + "\n" +
                        "touchY: " + touchLocation.Position.Y + "\n" +
                        "circleX: " + (_circle.Position.X) + "\n" +
                        "circleY: " + (_circle.Position.Y);

                    Debug.WriteLine(msg);
                }
            }

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void HandleInput(InputHelper input, GameTime gameTime)
        {
            base.HandleInput(input, gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
            ScreenManager.SpriteBatch.Draw(_circleSprite.Texture, ConvertUnits.ToDisplayUnits(_circle.Position),
                                           null,
                                           Color.White, _circle.Rotation, _circleSprite.Origin, 1f,
                                           SpriteEffects.None, 0f);
            ScreenManager.SpriteBatch.End();
            _border.Draw();

            Matrix projection = Matrix.CreateOrthographicOffCenter(0f, ScreenManager.GraphicsDevice.Viewport.Width / _meterInPixels,
                                                             ScreenManager.GraphicsDevice.Viewport.Height / _meterInPixels, 0f, 0f,
                                                             1f);
            Matrix view = Matrix.CreateTranslation(new Vector3((_screenCenter / _meterInPixels) - (_screenCenter / _meterInPixels), 0f)) * Matrix.CreateTranslation(new Vector3((_screenCenter / _meterInPixels), 0f));

            _debugView.RenderDebugData(ref projection, ref view);

            base.Draw(gameTime);
        }
Nov 2, 2012 at 12:15 PM
Edited Nov 2, 2012 at 12:17 PM

I've solved it myself.

I need to convert the world to screen.

 

Vector2 circlePositionSim = Camera.ConvertWorldToScreen(_circle.Position);
Debug.WriteLine("WIDTH " + _circleSprite.Texture.Width + ", HEIGHT: "+ _circleSprite.Texture.Height +"\n" +                                    
"circleX: " + circlePositionSim.X + "\n" +                                    
"touchX: " + touchLocation.Position.X + "\n" +                                    
"circleY: " + circlePositionSim.Y + "\n" +                                    
"touchY: " + touchLocation.Position.Y);

if (touchLocation.Position.X >= (circlePositionSim.X - (_circleSprite.Texture.Width / 2)) &&                        
touchLocation.Position.X <= (circlePositionSim.X + (_circleSprite.Texture.Width / 2)) &&                        
touchLocation.Position.Y >= (circlePositionSim.Y - (_circleSprite.Texture.Height / 2)) &&                        
touchLocation.Position.Y <= (circlePositionSim.Y + (_circleSprite.Texture.Height / 2)))                    
{                        
Debug.WriteLine("touched");                    
}