position of points on a line?

Started by
8 comments, last by uralkemal 17 years, 1 month ago
I want to get the position of the points which are on a drawed line. Because i want to collide objects with the line. Start and end points are known. So what is the formula? Thanks...
Advertisement
use the formula y = mx + b, where b is the starting y-position(lower y coordinate) and where m is

y2 - y1
-------
x2 - x1

Then, plugin the x value and your y is the corresponding height on the line.
bi-FreeSoftware
Quote:Original post by AdamGL
use the formula y = mx + b...
I have to disagree with this advice, for the following reasons:

1. There's really no reason (that I can think of at least) to express intersection tests involving linear components in terms of sampled points. The implicit or parametric form should be preferred; either of these will be both faster and more accurate, generally speaking.

2. The y = mx + b formula is only directly applicable in 2D. Although one might surmise that this is a 2D problem from the wording of the OP's post, this was not actually specified.

3. Finally, the explicit form for 2D lines is generally a poor choice for games and graphics applications. It's taught in algebra because it's a classic example of a linear equation and is easy to graph and visualize. However, unlike the implicit and parametric forms it cannot represent lines with arbitrary orientations in a stable and uniform manner, and therefore is usually of little use in the given context.
You are probably better with the parametric form of the line, since there isn't a divide by zero problem if x2 and x1 are the same in the previous formula (and that is 2d only anyway).

If vS, vE are the position vectors of the start and end, then the equation of the line segment from vS to vE is

vs + t(vE - vS)

where t is in the range 0 to 1. This can be multiplied out to get

(1-t)vS + t(vE)
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Quote:Original post by jyk

Quote:
the explicit form for 2D lines is generally a poor choice for games and graphics applications.


true but i really need it. I only want a range for the position of points.
Quote:Original post by uralkemal
true but i really need it.
Why? Practically speaking, anything you can do with the explicit form you can do with the implicit or parametric form as well (unless there's something I'm overlooking), and with greater generality and robustness.

I'll say once again that if this is for collision detection purposes, representing the linear component as a set of discrete points is probably not the best approach. However, if for some reason you really want to do this, you can compute the points quite easily using the parametric form (see Paradigm Shifter's post, above).
Quote:Original post by jyk
Quote:Original post by uralkemal
true but i really need it.
Why?


Yesterday i found a game. I wanna show you the game. This is not the game i want to do. But similar. You can download: http://nichtnormal.com/daten/learn2fly.zip
It looks like what you're really looking to do is collide segments (the line between the previous position and the present position for your falling object, and the line for the bounce object). Colliding single points against a line isn't going to work.

I can't find any good references to point you to, but I imagine someone else on this board can show you some sample code for segment-segment collision.
[size=2]Darwinbots - [size=2]Artificial life simulation
Quote:Original post by uralkemal
Quote:Original post by jyk
Quote:Original post by uralkemal
true but i really need it.
Why?


Yesterday i found a game. I wanna show you the game. This is not the game i want to do. But similar. You can download: http://nichtnormal.com/daten/learn2fly.zip
Don't have time to look at the game atm, but I think you may be missing the point here. I believe you when you say you want to detect intersections with a line segment, but what I and others are trying to tell you is that a) representing the segment as a set of discrete points is not the way to go, and b) y = mx + b is not a useful representation in this context.

Here is the first hit from a Google search for 'intersection of lines'. Searching for that and similar phrases should turn up plenty of useful references.
thanks everybody, i think i understood, A bit late:)

This topic is closed to new replies.

Advertisement