Results 1 to 3 of 3

Thread: Collision handling!

  1. #1

    Collision handling!

    Hi,

    i'm having problem with my collision handling (2D).. As i can see in this forum it looks like we got some skilled ones in here.. , i do have collision detection in my routines (rectangle <-> rectangle, polygon <-> polygon). But i dunno how i should handle the collision, for example..

    My sprite makes a collision with the ground.. How do i get the "Projection vector" ?, or is there easier ways to get how much i shall move the sprite to not collide with the ground ? The ground can be a hill or whatever..

    I looked at a couple of forums but have not really found anything on collision handling just detection..

    Eager for some answers

    BTW, i'm totally crap in math! And very new to this, i tried with Omega a while ago but real life made entry.. Now i'm back to try again..

    /XTRiZ

  2. #2

    Collision handling!

    This is 1 way: First you need to find what is nearest point from collision point to ground. These 2 points form land's "normal vector". Then mirror object movement vector with normal vector.

    Code:
    P3f = record x,y,z&#58; single; end;
    
    function glmReflectVector3f&#40;v, normal&#58; p3f&#41;&#58; p3f;
    var temp&#58; single;
    begin
      temp&#58;=2*&#40;v.x*normal.x+v.y*normal.y+v.z*normal.z&#41;;
      result.x&#58;=v.x-normal.x*temp;
      result.y&#58;=v.y-normal.y*temp;
      result.z&#58;=v.z-normal.z*temp;
    end;
    edit: This should be easy to convert in 2D too, or set Z as 0...

  3. #3

    Collision handling!

    Just a little clarification there:
    And a short algebra session:

    Code:
      This is your character with the current 2D velocity vector shown&#58;
    
        O  <- character
       /
      /  <- velocity
       
       
        ^   <- Normal of the ground
        |  
    ____|_____
     GROUND
    
    Note&#58; a normal always points perpendicular to it's surface.
    
    Then you add the velocity vector to the normal vector&#58;
      
      <<===O <- character/object
       |  /
       | /
       |/
     
     To get out a vector of where your object SHOULD go..
     That's this one&#58; <<=====
     
    But there is one trick, you must normalise the normal &#58;&#41;
    In other words your normal must be of the same length as your velocity.
    So first measure the length of the velocity&#58;
    
    Len = sqrt&#40;sqr&#40;Vel.X&#41;+sqr&#40;Vel.Y&#41;&#41;;
    
    Then normalise the normal&#58;
    Len2 &#58;= sqrt&#40;sqr&#40;Norm.X&#41;+sqr&#40;Norm.Y&#41;&#41;;
    
    if Len2 <> 0 then
    begin
      Norm.X &#58;= Norm.X / Len2;
      Norm.Y &#58;= Norm.Y / Len2;
    end;
    
    Now your normal is guaranteed to have a length of exactly 1. No matter where it's 
    pointing.
    
    Now just times it by Len to get it the same length as your velocity vector&#58;
    Norm.X &#58;= Norm.X * Len;
    Norm.Y &#58;= Norm.Y * Len;
    
    Then get the result vector by adding the two&#58;
    
    Result.X &#58;= Norm.X+Vel.X;
    Result.Y &#58;= Norm.Y+Vel.Y;
    Hope that helps.
    My site: DelphiTuts.com (coming soon)...

    Download Font Studio 4.21 here.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •