Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Vector Problem

  • 27-12-2010 10:24pm
    #1
    Registered Users, Registered Users 2 Posts: 627 ✭✭✭


    Hey, I'm having trouble understanding a piece of Math I came across and am looking for help. Bare with me as its interpreted through programming.
    The three vectors at the start are the position of the player, and each end of a line that is used for a collision. I dont understand the use of the cross product as its 2d nor the assignment of "dr",I understand the math but not its use in these circumstances. Delta is the main source of confusion so any help would be cool.

    Cheers

    Vector planePt1(landscape.xPos,landscape.yPos);
    Vector planePt2(landscape.xPos[i+1],landscape.yPos[i+1]);
    Vector player(this->pos.x,this->pos.y);
    x1 = planePt1.x - player.x;
    y1 = planePt1.y - player.y;
    x2 = planePt2.x - player.x;
    y2 = planePt2.y - player.y;
    dx = x2 - x1;
    dy = y2 - y1;
    dr = sqrtf(powf(dx, 2) + powf(dy, 2));//length of dxy
    D = x1*y2 - x2*y1;//Cross product of x1y1x2y2
    delta = powf(radiusOfPlayer*0.9,2)*powf(dr,2) - powf(D,2);

    //Collision detected
    if (delta >=0 )


Comments

  • Registered Users, Registered Users 2 Posts: 13,077 ✭✭✭✭bnt


    Some of that code is a little confusing e.g. the "Vectors" are what I would call points, not vectors. If I start at the beginning, the first question I would have is: what's in the arrays in "landscape"? The first two lines pull values from this variable according to index variable "i" to define two points. The third point is the position of the player. The next bit is a bit clearer if you draw out these three points on paper.

    (x1,y1) is a vector (for real this time) of how far the player is from point 1, and the same for (x2,y2) and point 2; then (dx,dy) is a vector defining the difference between those two vectors, and dr is the absolute length of that vector. In other words, it appears to me, dr will be zero when the player is equidistant from the other two points.

    The business with the cross product ... it should be used on 3D vectors, as you suspect, and I'm not 100% sure what's going on here, but I suspect the 3rd dimension is assumed to be zero and not shown. So the first points are assumed to be on the x-y plane (z=0), and that "cross product" function returns D, a single value for "height above the plane" - which is a cockeyed way of doing it if true.

    But I can't quite visualize what the last function is supposed to do or what "delta" means. Higher values of D mean lower values of delta, which is a good thing (since values of delta >0 mean a collision. A value of dr approaching zero (equidistant) means the first term of the formula tends to zero (also good). My guess is that this test involves an object flying between gates (defined by the first two points), while not hitting the top, or something like that.

    You are the type of what the age is searching for, and what it is afraid it has found. I am so glad that you have never done anything, never carved a statue, or painted a picture, or produced anything outside of yourself! Life has been your art. You have set yourself to music. Your days are your sonnets.

    ―Oscar Wilde predicting Social Media, in The Picture of Dorian Gray



  • Registered Users, Registered Users 2 Posts: 627 ✭✭✭Dboy85


    Thanks for the reply, you are right in saying they are points but I have a class called "Vector" to create these variables in c++ so thats where the the Vector term comes from.
    I'll explain what i'm doing to try make it a little clearer for you. I have a class for creating a 2d terrain and created an object called "Landscape". Within this object are an array of x points and an array of y points. So the first point in the terrain is xpos[0] & ypos[0]. I then draw a line between each element of the arrays.

    e.g xpos[0] ypos[0] -> xpos[1] ypos[1] is a line...

    There are about 200 elements in those arrays and I track the xposition of the player on screen to determine which section of the terrain the player is in because it can only crash into one.

    What i'm detecting is when the player crashes into these lines.
    Im sorry if I made it more confusing ;)

    I used a simpler version of vector collision using dot product and trig functions but this one seems to be the most reliable but for the life of me I cant understand it. I can put it into math instead of code if it makes it easier for you to understand?


  • Registered Users, Registered Users 2 Posts: 1,082 ✭✭✭Fringe


    I'm not familiar with whatever language this is but that thing is not the cross product. I would say it's the determinant which would correspond to the area of a parallelogram. I think Delta is also some kind of area so a collision is detected when the area criteria is met which would be Delta >= 0 but as I said, I'm not exactly sure about everything.


  • Registered Users, Registered Users 2 Posts: 627 ✭✭✭Dboy85


    We're getting somewhere now alright. Good shout about the determinant fringe.

    I rearranged it to confirm what you were saying and what I did was:

    if the determinant of the terrain vectors, after subtracting the length of the player vector for each, is less than or equal to 0 their is a collision.
    This works because the player and terrain will be on the same line as x1y1x2y2 vectors decrease as the player gets closer to them.

    I'll add that it doesn't factor in the radius of the player. The player can move into the terrain as long as its center xy co ordinates dont lie flat on the collision line. So its not realistic to visualize as you can collide to a certain extent without it being detected.

    Now back to the delta business. In calculating "delta" he multiplies the radius of the player by the length between points, i dont know why, and then subtracts the determinant of the terrain vectors which decrease each time the player moves closer to the terrain. When the terrain determinant is less than (player radius * length of the terrain) it makes a perfect collision.

    Hope this makes it a bit clearer as I still dont fully understand why in particular why he multiplies the player radius by the length of the terrain...

    Thanks to both of you for helping so far! Beers on me at the next meet up and shots for the solution :cool:

    Here it its more readable and tidied a bit.

    //Isolate the 2nd plane point vector
    dx = x2 - x1;
    dy = y2 - y1;
    //Find the distance of 2nd plane point from 1st plane point
    dr = sqrtf(powf(dx, 2) + powf(dy, 2));
    //Determinant of both plane vectors
    D = x1*y2 - x2*y1;
    //Area calculation for collision

    //radius of player * distance between plane points - determinant of plane points
    delta = (radius * dr) - D;


  • Registered Users, Registered Users 2 Posts: 13,077 ✭✭✭✭bnt


    Just a quick note about the cross product / determinant question: it's both, actually. If you have two 3D vectors and write them in a matrix form, the cross product is the determinant of that matrix. I found that very helpful when I was trying to remember how to do cross products by hand in exams - since I'd already learned how to do determinants.

    You are the type of what the age is searching for, and what it is afraid it has found. I am so glad that you have never done anything, never carved a statue, or painted a picture, or produced anything outside of yourself! Life has been your art. You have set yourself to music. Your days are your sonnets.

    ―Oscar Wilde predicting Social Media, in The Picture of Dorian Gray



  • Advertisement
  • Closed Accounts Posts: 6,081 ✭✭✭LeixlipRed


    Did the OP not say it's 2D though?


  • Registered Users, Registered Users 2 Posts: 627 ✭✭✭Dboy85


    Its a 2d game, z axis doesnt belong round here


  • Closed Accounts Posts: 6,081 ✭✭✭LeixlipRed


    Cross product isn't definied in two dimensions so just be careful there. Haven't read all the posts so not sure if that's a proper contribution or not :D


  • Registered Users, Registered Users 2 Posts: 627 ✭✭✭Dboy85


    Fringe pointed out earlier that its the determinant of the 2 vectors. cheers tho;)


  • Registered Users, Registered Users 2 Posts: 627 ✭✭✭Dboy85


    So any ideas why he multiplies the radius of the player by the length of the terrain soon to be collided with?


  • Advertisement
Advertisement