-2
\$\begingroup\$

I have collision detection but I don't know how to stop an object after a collision and then move the object backward.

Any ideas?

\$\endgroup\$

closed as unclear what you're asking by DMGregory, Engineer, Kromster, Josh May 16 '17 at 16:17

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Change the entity's direction and velocity when the collision is detected. \$\endgroup\$ – Justin Skiles Oct 8 '12 at 0:45
  • \$\begingroup\$ if i change velocity to 0 for stop the object get stuck and i cant move anywhere because movespeed is updated every frame \$\endgroup\$ – bqaq Oct 8 '12 at 6:06
  • 2
    \$\begingroup\$ Please consider posting some relevant code, as this may help others to improve their answer. \$\endgroup\$ – floAr Oct 15 '12 at 12:24
2
\$\begingroup\$

In addition to Marton's answer you could also let your object "bounce" off the wall by reflecting its velocity vector:


vector reflection

  • V is the velocity vector of your object
  • N the normal of your wall
  • R reflected velocity vector

Vector ReflectVector(Vector V,Vector N)
{
    //Save vector lengths to restore later..
    float vLength = V.Length();
    float nLength = N.Length();
//Normalize V.Normalize(); N.Normalize();
//R is our output vector Vector R = V;
//"Reflect" the vector using the DOT product R = -2 * (V.Dot(&N))*N + V;
//Restore lengths V *= vLength; R *= vLength; N *= nLength;
//Return the reflected vector return R; }

example usage could be:

if(Collision)
{
    Vector N(1,0); //Imaginary wall (Left side)
    ObjectVelocity = ReflectVector(ObjectVelocity,N);
}

\$\endgroup\$
  • \$\begingroup\$ Thanks bryan but i dont wanna bounce object u just wanna make him stop and then move in any direction except wall \$\endgroup\$ – bqaq Oct 8 '12 at 8:20
1
\$\begingroup\$

A simple solution would be to implement a check during your movement code. If the player attempts to move right, check if there is an object that would stop the object moving in that tick and if so move the player next to that object and set the velocity to 0.

For example:

if (player.X + movespeed >= wall.X) {player.X = wall.X - xOffset;}
\$\endgroup\$
0
\$\begingroup\$

There are many solutions, depending on what kind of effect you want to achieve. Here's a simple 'rebound' solution:

When a collision between object 'A' and the wall is detected, do the following:

  1. Get a Vector V_r which points from the collision point to A's center point (let's call the center point P_A ).
  2. Set the object's new position P_An = P_A + (V_r * d), where d is a distance. This distance must be so, that after you set P_An, the object is no longer colliding with the wall. So d depends on your collision detection method. With pixel perfect collision, d must be large enough to put P_An on a different pixel than which P_A is on.
  3. Let's name the object's velocity vector right before the collision V_v.
  4. Set the object's new velocity to V_vn = -V_v * c, where c is a constant value somewhere between zero and one. This will provide the effect of some "inertia loss".
\$\endgroup\$
  • \$\begingroup\$ What is A's center point ? \$\endgroup\$ – bqaq Oct 8 '12 at 7:41
  • \$\begingroup\$ @bqaq Well, the position of your object's center. If your object is a circle, then it't the circle's center. \$\endgroup\$ – Marton Oct 8 '12 at 8:56
  • \$\begingroup\$ you have code sample of this ? \$\endgroup\$ – bqaq Oct 8 '12 at 10:25
  • \$\begingroup\$ @bqaq Not at the moment. Which part can't you implement? \$\endgroup\$ – Marton Oct 8 '12 at 11:39
  • \$\begingroup\$ How to figure out 1st one anyway i dont wanna bounce my object i just wanna stop him when he collide and give him ability to move \$\endgroup\$ – bqaq Oct 8 '12 at 20:37

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