View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# How to calculate coordinates?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Ironmaggot  Members

Posted 12 December 2013 - 03:36 PM

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, 12 December 2013 - 03:38 PM.

### #2Vortez  Members

Posted 12 December 2013 - 04:12 PM

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, 12 December 2013 - 04:18 PM.

### #3pyirrlicht  Members

Posted 13 December 2013 - 06:54 AM

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



Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.