1
\$\begingroup\$

I'm working on a 2D game for windows phone where the player moves blocks around to bounce a laser beams.

I need to create some simple physics for moving the blocks.

For the moment I have a simple collision class, telling me if two rectangles collide. But that's not enough because when I'm moving a block the rectangle/rectangle function doesn't work.

By Doesn't work i mean,I manage the collision but I don't how to stop the block mooving while the user sliding the block.

The collision response for the block I want is if the user tries to slide it on another object, the block should slide up against the other object and not overlap.

How would I go about implementing this functionality?

Some code about how i impletement mooving block

{
            Currentposition = Station.Position;

            TouchPanelCapabilities touchCap = TouchPanel.GetCapabilities();
            if (touchCap.IsConnected)
            {
                TouchCollection touches = TouchPanel.GetState();
                if (touches.Count >= 1)
                {
                    Vector2 PositionTouch = touches[0].Position;
                    for (int i = 0; i < ListDragObj.Count(); i++)
                    {
                        if (ListDragObj[i].Shape.Contains((int)PositionTouch.X, (int)PositionTouch.Y))
                        {
                            ListDragObj[i].selected = true;
                        }
                        else
                        {
                            ListDragObj[i].selected = false;
                        }
                        ListDragObj[i].Update(PositionTouch);
                    }
                }
            } 
        }
\$\endgroup\$
  • \$\begingroup\$ Please describe exactly what "doesn't work" means. You could easily have a coding or logic error with your existing function. \$\endgroup\$ – MichaelHouse Oct 24 '13 at 13:29
2
\$\begingroup\$

I think what you're really asking for is a scheme for the user to interact with the block that feels natural.

If you're just setting the position of the block to the position of the finger, you're going to run into some collision and placement issues. It sounds like you might want to try a Force- or Velocity-based approach.

For the Velocity-based approach, when the user 'grabs' a block, give it a velocity in the direction of the 'finger'. Create a deadzone around the block, and set a reasonable minimum and maximum velocity. This way, each frame you can advance the block and check for collisions. If there were collisions, you move the block back by the collision depth, and kill the velocity perpendicular to the collision surface. This will allow it to slide along walls and such, if your finger is inside of it. (It's not uncommon for people to make the mistake of setting the entire velocity to zero, not just the perpendicular component).

For a Force-based approach, rather than setting the velocity in the way I described, set a force on the object that way. This will generate acceleration. You're going to want some damping on the velocity (Friction. In other words, reduce your velocity by a velocity-dependent amount every frame (v *= (1- c / deltaT )). You probably would want to kill the velocity more quickly when the finger is in the deadzone. This will give the blocks a more springly feel, and you can also play around with varying the friction dependent on the surface if that sort of thing is interesting or relevant to your game.

\$\endgroup\$
  • 1
    \$\begingroup\$ Thanks, you helped me to understand my problem; Gonna try this. \$\endgroup\$ – Gabson Oct 25 '13 at 9:34
  • \$\begingroup\$ how can i know the direction of the finger ? \$\endgroup\$ – Gabson Oct 25 '13 at 10:23
  • \$\begingroup\$ In the scheme I outlined, you never need to know the direction of the finger, just the position. When I talk of "in the direction of the finger" I am referring to a vector pointing from the block to the position of the finger. \$\endgroup\$ – TASagent Oct 25 '13 at 14:34
  • \$\begingroup\$ If, however, you do need the direction the finger is moving, take its current position minus its last position. \$\endgroup\$ – TASagent Oct 25 '13 at 14:34
0
\$\begingroup\$

You need a so called sweep test. Check if the block before its movement is behind the target after its movement. You can use a plane for the in front/behind test, or make the movement in small steps, so you don't miss the target.

\$\endgroup\$
  • \$\begingroup\$ What you mean by make the movement in small steps ? The user drag the block on screen it will be buged if the block moove slowly. \$\endgroup\$ – Gabson Oct 24 '13 at 12:34
  • \$\begingroup\$ And what to do if i found a collision ? not draw the image so it stay at the same position or ? \$\endgroup\$ – Gabson Oct 24 '13 at 12:50
  • \$\begingroup\$ e.g. if the user drags 100 pixel, move it 10 pixel, test for collision move it 10 pixels further, test for collision until you reach 100 pixel. If you found collision stop at this location. \$\endgroup\$ – codymanix Oct 25 '13 at 12:05
  • \$\begingroup\$ I understand, but the loop isn't too fast ? and how can i get the position where it goes before mooving it ? too abstract for me.. \$\endgroup\$ – Gabson Oct 25 '13 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.