Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hi there i have collision detection but i dont know how to stop object after collision and then move backward for example

any ideas ?

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

3 Answers 3

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;}
share|improve this answer

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);
}

share|improve this answer
    
Thanks bryan but i dont wanna bounce object u just wanna make him stop and then move in any direction except wall –  bqaq Oct 8 '12 at 8:20

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".
share|improve this answer
    
What is A's center point ? –  bqaq Oct 8 '12 at 7:41
    
@bqaq Well, the position of your object's center. If your object is a circle, then it't the circle's center. –  Marton Oct 8 '12 at 8:56
    
you have code sample of this ? –  bqaq Oct 8 '12 at 10:25
    
@bqaq Not at the moment. Which part can't you implement? –  Marton Oct 8 '12 at 11:39
    
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 –  bqaq Oct 8 '12 at 20:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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