Public Group

# How to calculate coordinates?

This topic is 2141 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am having trouble with calculating coordinates for my game. I have a line that has a fixed endpoint. I want this line's start point to get closer to the endpoint with a constant speed. But that point just shoots off the screen. Can you help me.

                        if fleets[i][0][0] != fleets[i][1][0] and fleets[i][0][1] != fleets[i][1][1]:

#this here reads coordinates into proper variables. (xo,yo) is the starting point
#(x1;y1) is the endpoint

x0 = float(fleets[i][0][0])
y0 = float(fleets[i][0][1])
x1 = float(fleets[i][1][0])
y1 = float(fleets[i][1][1])

#here I calculate the total distance between these points

d2 = (x1 - x0)*(x1 - x0) + (y1 - y0)*(y1 - y0)
d = math.sqrt(d2)

#here is the distance that has passed since the last main function cycle

d3 = 1 * (elapsedTime/1000)

#I calculate how many percentages of the total distance I travelled since the last main function cycle

p = (d3 / d2) * 100

#if both x'es are equal then there is no point in calculating the change in x

if x0 == x1:
x = x0

#if x0 is bigger than x1, i calculate the amount travelled along the x axis and substract that from the x0, getting my new x0

if x0 > x1:
x_p1 = (x0 - x1)/100
x_p2 = x_p1 * p
x = x0 - x_p2

#if x0 is smaller than x1, i ,again, calculate the distance travelled along x-axis, but then i add the travelled distance to the starting y0 to get my new x0

if x0 < x1:
x_p1 = (x1 - x0)/100
x_p2 = x_p1 * p
x = x0 + x_p2

#same happens here as happened with x'es

if y0 == y1:
y = y0

if y0 > y1:
y_p1 = (y0 - y1)/100
y_p2 = y_p1 * p
y = y0 - y_p2

if x0 < x1:
y_p1 = (y1 - y0)/100
y_p2 = y_p1 * p
y = y0 + y_p2

#the x0 and y0 that we started with at the beginning of this calculation are now replaced with the new coordinates

fleets[i][0][0] = x
fleets[i][0][1] = y

#when I created this travelling line, I calculated the time it should travel
#here already travelled time is updated

fleets[i][3] = fleets[i][3] + (elapsedTime/1000)

Edited by Ironmaggot

##### Share on other sites

I would do something like that

x0 = float(fleets[i][0][0])
y0 = float(fleets[i][0][1])
x1 = float(fleets[i][1][0])
y1 = float(fleets[i][1][1])

xdif = x1 - x0;
ydif = y1 - y0;

if(elapsedtimeinsec > animationlengthinsec)
elapsedtimeinsec = animationlengthinsec;

x0 = x0 + (xdif * (elapsedtimeinsec / animationlengthinsec))
y0 = y0 + (ydif * (elapsedtimeinsec / animationlengthinsec))

how to implement this in your code is up to you.

Also, using vectors would greatly reduce code duplication ie.:

Vec2 Origin = float(fleets[i][0])
Vec2 Dest = float(fleets[i][1])

Vec2 Dif = Dest - Origin;

if(elapsedtimeinsec > animationlengthinsec)
elapsedtimeinsec = animationlengthinsec;

Origin += (Dif * (elapsedtimeinsec / animationlengthinsec))

Edited by Vortez

##### Share on other sites

if x0 < x1: y_p1 = (y1 - y0)/100 y_p2 = y_p1 * p y = y0 + y_p2

perhaps

		if y0 < y1:
y_p1 = (y1 - y0)/100
y_p2 = y_p1 * p
y = y0 + y_p2



• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11