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

To infinity, or not to infinity

Options
  • 05-05-2006 5:07pm
    #1
    Registered Users Posts: 8,449 ✭✭✭


    Hey guys, this is gonna be hard to explain but I'm trying to move a point x,y along at a given speed and slope. It works for all cases except when the slope is infinity or - infinity.

    I have no idea how to deal with infinity being the return value of the slope. Does that mean that ye'd have to code specifically for when x = 0 (slope is infinity/-infinity)?

    As i said the point moves perfectly except for when slope is +-infinity.

    Anybody have a clue what I'm talking about?


Comments

  • Registered Users Posts: 304 ✭✭PhantomBeaker


    Teehee... This is generally why mathematicians love vectors and hate slopes :)

    This might help you with a redesign, unless you specifically HAVE to use a slope.

    Basically what we're talking about when we talk about slope is how far a line moves in the y-axis when you move in the x-axis. So if you have a slope of 0.5 it means that for every unit you move in the x direction, you move only a half unit in the y direction. Get me so far? so your slope = (y-movement / x-movement)

    you get plus infinity when your line goes straight up (so that would be any positive non-zero value for y and x=0 - so division by 0 leads to infinity). Similar for negative infinity, except it's down.

    The thing about the concept of "slope" is it only holds in 2-dimensional space. You can also think of slope in terms of a 2-dimensional vector describing the direction of your motion. You can then describe your vectors in the following way, if you think of them as (x y) (and if you want to get the slope, just divide the y value by x) you get these sorts of values: a 45-degree angle (slope of 1) is (1 1), the direction of horizontal line is (1 0) (slope of 0), and a vertical line (slope of infinity) is (0 1).

    So with that sort of scheme you can describe any direction of a line. (For the record, if you ever deal with 3d stuff, it's exactly this sorta stuff, but with vectors with 3 values (x y z) because there's no meaningful way to represent a 3d direction as just one value)

    If you really want, you can then say your velocity is: speed by your directional vector. All that means is you multiply each component of your vector (i.e. x and y) by your speed, and add it to your point's values.

    Actually you'll find this will sidestep a LOT of problems for ya :)

    Enjoy.

    Æ (yes, I'm aware I'm probably answering a homework question, but I like to answer them in such a way that they still have work to do :) )


  • Registered Users Posts: 8,449 ✭✭✭Call Me Jimmy


    Thanks a lot for the reply! Actually this is for a game I'm doing on my own (should be studying for exams).

    I understand the concept of slope and understand why it is infinity, it just seemed like there was no constant way of using slope for EVERY direction and now you have confirmed that.

    I'm interested in hearing more about the idea of using a vector instead of slope. I'm not sure I fully understand how to implement what your saying.
    Basically I will have two points - a start and destination- and I want to be able to move an object in a controlled manner from the first point to the second.
    Thanks so much for the help


  • Registered Users Posts: 5,112 ✭✭✭Blowfish


    Try this. The way you have described the line is the explicit version. You can use the implicit version mentioned there for 2d, but it's best to stick with the parametric verison of the line, as it can be used in 3d aswell. As phantom beaker said it uses vectors. Basically it describes the line in relation to a variable t, which you generally use as time.


  • Registered Users Posts: 8,449 ✭✭✭Call Me Jimmy


    Cool thanks for that :)

    I'm gonna give it a read now, see if i can get my head around it.


  • Registered Users Posts: 8,449 ✭✭✭Call Me Jimmy


    seems a bit too complicated... I never did vectors in sixth year, not sure if that would make a difference but there's a lot on that page and I can't seem to find exactly what I'm looking for.

    I looked at the implicit equation part and it says how to convert an explicit to an implicit but, i just can't seem to do it.


  • Advertisement
  • Registered Users Posts: 5,112 ✭✭✭Blowfish


    Yeah you really need to know vectors for this kind of stuff (and matrices for 3d).

    If it's just something you are just hacking it out for the fun of it, then you could take the easy way out (using the explicit) and just use an if. i.e. switch the equation around and use x instead of y.
    In reality though the best way is definitely with the parametric, and especially if you plan on doing 3d stuff in the future.


  • Registered Users Posts: 8,449 ✭✭✭Call Me Jimmy


    Yeah well I wanted to be able to do it the proper way for the future, I haven't really been paying much attention to maths (I'm first year Computer Apps DCU) but I really want to be able to do this sorta thing.

    My main problem atm is getting a velocity from 2 points,using a a formula.
    Like say I have 2 points P1(5,10), P2(20,5) and i want to know the amount to move x and y each time before I move them at all... I just don't understand how to get this information without using the slope idea.


  • Registered Users Posts: 8,449 ✭✭✭Call Me Jimmy


    Well I've managed to get the vector thing working. What I did was (i think its called normalizing the vector), I basically divided the destination x and y coordinates by the distance between the start and destination. points.

    This gave me a unit vector which I multiplied by the speed to get it going faster.(x and y are less than 1 in almost all case) The only problem is that when drawing images I have to round off the coordinates to ints and it causes a bit of jumpiness.

    Anyone know of a way of drawing an image in using float or double coordinates?


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Generally no matter what language you code in (what language ARE you coding in?) you can only place an image pixel accurate. And unfortunately there are no floating point pixels :p.

    If you don't want jumpiness, try moving the image one pixel at a time, but if your velocity is '10', move it 10 x 1 pixel each second. If velocity is 100, it moves 100 x 1 pixel each second... if ya know what i mean.


  • Registered Users Posts: 8,449 ✭✭✭Call Me Jimmy


    I'm coding in java. The thing about moving one pixel per second or whatever is that when i normalize the vector (as part of getting correct ratio of x to y movement) then the x and y movement are almost always less than 0. Also if i move one pixel per second that effects the slope.

    This means that during some cycles there is no image movement at all.

    Maybe I'm missing something...


  • Advertisement
  • Registered Users Posts: 4,188 ✭✭✭pH


    Just move things around on virtual screen co-ordinates (say 10x the real screen or more), and then divide by 10 to display on the real screen.

    This will give you the smoothness you're looking for, and the vectors in effect have sub-pixel precision.


  • Registered Users Posts: 689 ✭✭✭JoeB-


    Calculate the images present positon rather than its relative position to the last position....

    As far as the co ordinates go, just treat each direction seperately... X,Y and Z if necessary...

    Starting Point P1(x1,y1), Ending Point P2(x2,y2) ...

    X co ordinate = x1 + (stepnumber * ((x2-x1)/number of steps))
    Y co ordinate = y1 + (stepnumber * ((y2-y1)/number of steps))

    You can compare the values of each increment.. ((x2-x1)/number of steps) or ((y2-y1)/number of steps), see which is smaller and then choose an appropriate number of steps so the images moves at least a pixel each time or whatever....

    This approach can easily be scaled up for 3D...

    Cheers
    Joe


  • Registered Users Posts: 8,449 ✭✭✭Call Me Jimmy


    thanks a lot joe, it is working but there is one problem atm and that is accelerating and decelerating isn't working properly but hopefully i can fix that.

    Also, could you just give a brief summary of the mathematical theory behind your simplified formula? It is based on vectors right?

    thanks, :)


Advertisement