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

Few mathsey problems

Options
  • 01-08-2001 9:07pm
    #1
    Registered Users Posts: 1,842 ✭✭✭


    I'm writing a little game at the moment, and have become kind of stuck. I'd be grateful for any help anyone can provide.

    It's a little windows Spacewar! clone, and I need some kind of collision-detection, because it's pretty damn useless otherwise! smile.gif

    First problem is to know when a missile has hit a spaceship, or to put it another way, when a given point is within a given circle.

    The information I'd have for this is:
    mX,mY, missile x and y co-ords

    sX, sY, spaceship center co-ords, and sR, radius of the circle.

    I really have no idea how to go about this...

    The other problem is the lasers, I need to know if a line goes through a given circle. Could probably adapt the solution of the above problem?

    Information available for this is the x,y values of both points of the line (laser) and the circle x,y and radius as above.

    Please help, game is useless without collision detection smile.gif


Comments

  • Registered Users Posts: 27,645 ✭✭✭✭nesf


    It's not complicated really, you could just write a subroutine to check if either if
    the distance (mx,my) to (sx,sy) is = sR

    If it is then a missile has hit. You can use simple junior cert level geometry for this.

    You could write a more complicated sub for the second prob, where you check the perp distance from the line to the centre of the spaceship circle, leaving cert formula, if this is <= sR then you got a hit.

    Any help to you?


  • Closed Accounts Posts: 35 The Pinky


    Yeah nesf is right there

    Just create a subroutine that checks to see if the missle x,y is less than the radius distance of the ship x,y

    e,g

    Ship x,y = 5,2 We'll call then x1, and y1
    Missle x,y = 7, 3 Call these x2,y2
    Radius = 4

    Distance between two points formula

    square root of:
    (x1-x2)squared + (y1-y2)squared
    (5-7)squared + (2-3)squared
    (-2)squared + (-1)squared
    4 + 1
    = 5
    square root of 5 = 2.23
    5 is less than the radius
    therefore a hit!!

    At least I think I'm right someone better back me up on this!!

    Richey


    Narf!!!

    [This message has been edited by The Pinky (edited 02-08-2001).]


  • Registered Users Posts: 1,842 ✭✭✭phaxx


    I have a distance formula already that works grand.

    d = sqr( (x1 * y2) - (x2 * y2) )

    The only problem now is the line one, but I think I can solve that by getting a few points along the line, 25%, 50%, 75% and 100%, and because the length of the line(laser) and size of the ship is constant, I can make sure one of those points is inside the ship's hitbox (circle, whatever)

    Does anyone have a better way?



  • Closed Accounts Posts: 1,136 ✭✭✭Bob the Unlucky Octopus


    Moved to programming- the math folks there could probably help you more smile.gif

    Bob the Unlucky Octopus


  • Registered Users Posts: 27,645 ✭✭✭✭nesf


    Aye, I do, it's stated above smile.gif

    Get a leaving cert maths, hons, book and check the line geometry section for the formula for finding the perp distance between a line and a point. If this distance is less than or equal to the lenght of the radius then you got yourself a hit.

    It's simple and only requires one formula.


  • Advertisement
  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    or..If you really wanna try some funky-ass progamming, try the intesection of line and circle circumference formula, found once again in any leaving cert hons maths book. Basically if any value is found, it's a hit, and if an error msg is returned (infinite or illogical value) it's not a hit


  • Closed Accounts Posts: 11 AzReAl


    Here i am seeking to get into computer programming and math problems arise....NOOOOOO


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    As the lads b4 have said. All you have to do is implement Pythagoras's Theorem, it's very easy.

    So to calc the radius between ships or their scope whatever use this formula

    radius_scope = sqrt(opposite² + adjacent²)

    All you have to do then is each time movement occurs, check to see if an object is <= a centain radius_scope to check for a collision.

    ;-phobos-)


Advertisement