How do I (Math problem) calculating a value?

Get help using Construct 2

Post » Thu Jun 25, 2015 12:39 pm

Anyone here any good at math?

I need to calculate the distance between an object and a point in a diagonal line between 2 other objects.
A bit hard to explain but i will try.

Image

The point at the top (DTC) is an object. I need to calculate the Y distance between this object and a point straight below in the diagonal line.

The diagonal line would be calculated based on 2 points. the lower left point and the upper right point. Anyone comprehend what I'm trying to do?

The DTC object can move in both X and Y axis so the distance value can be variable.

Any help appriciated.
Follow my progress on Twitter
or in this thread Archer Devlog
B
42
S
18
G
19
Posts: 1,058
Reputation: 14,050

Post » Thu Jun 25, 2015 1:27 pm

I'm not good at maths but heres a round about method.

Create a sprite 1 pixel in size at the upper right point, then run a loop the distance between the upper right point and the lower left point in that direction and move it 1 pixel each loop. Stop the loop when its y coordinate matches the DTC's. Now get the distance between the sprite and the DTC.

Also i think this is called "raycasting". Maybe do a search for that term.\
B
43
S
23
G
21
Posts: 735
Reputation: 12,132

Post » Thu Jun 25, 2015 2:32 pm

@Ethan thanks, but that's not really it i suppose. I'm googling a little bit and might be on to something. I need to calculate the X&Y position of a point in the slope, based on the object.... when i have that value, it's not that hard to calculate the distance between that point and the object.
Follow my progress on Twitter
or in this thread Archer Devlog
B
42
S
18
G
19
Posts: 1,058
Reputation: 14,050

Post » Thu Jun 25, 2015 3:04 pm

You can find a equation by googling the distance between a line and a point.

If a and b are the two points of the diagonal and p is the other point then the distance from p to the line would be:
Abs(((P.x-a.x)*(b.y-a.y)-(p.y-a.y)*(b.x-a.x))/distance(a.x,a.y,b.x,b.y))

Edit:
Oops you wanted y distance, that would be this:

Abs(Lerp(a.y, b.y, (P.x-a.x)/(b.x-a.x))-p.y)
Last edited by R0J0hound on Thu Jun 25, 2015 3:42 pm, edited 1 time in total.
B
94
S
33
G
118
Posts: 5,394
Reputation: 75,849

Post » Thu Jun 25, 2015 3:13 pm

B
43
S
23
G
21
Posts: 735
Reputation: 12,132

Post » Thu Jun 25, 2015 6:56 pm

@Ethan. Good example, not so useful in this case since i was looking for a formula, but quite impressive how u solved it....

@R0J0hound. That's just brilliant. Exactly what i was looking for.

Thanks both. I should have payed more attention in math class. :D
Follow my progress on Twitter
or in this thread Archer Devlog
B
42
S
18
G
19
Posts: 1,058
Reputation: 14,050

Post » Thu Jun 25, 2015 8:17 pm

R0J0hound wrote:You can find a equation by googling the distance between a line and a point.

If a and b are the two points of the diagonal and p is the other point then the distance from p to the line would be:
Abs(((P.x-a.x)*(b.y-a.y)-(p.y-a.y)*(b.x-a.x))/distance(a.x,a.y,b.x,b.y))

Edit:
Oops you wanted y distance, that would be this:

Abs(Lerp(a.y, b.y, (P.x-a.x)/(b.x-a.x))-p.y)


Everything works perfect, but now i noticed that 'p' needs a negative distance value if below the line. How would i go about getting that? hmmmmm
Follow my progress on Twitter
or in this thread Archer Devlog
B
42
S
18
G
19
Posts: 1,058
Reputation: 14,050

Post » Thu Jun 25, 2015 9:15 pm

Take away the ABS().
ImageImageImage
B
71
S
22
G
258
Posts: 3,791
Reputation: 143,198

Post » Fri Jun 26, 2015 11:47 am

Ahhh.. simple as that! ;) Now i know what abs does as well... learning new things every day ;)
Follow my progress on Twitter
or in this thread Archer Devlog
B
42
S
18
G
19
Posts: 1,058
Reputation: 14,050


Return to How do I....?

Who is online

Users browsing this forum: ardhitosen, kurtbal and 9 guests