I used ChatGPT

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

I used ChatGPT

Post by kingnoob »

Do you use ChatGPT?

Here is my session.

ME
in two dimensional space if an object is at floating point coordinates (x1, y1) and is moving at a constant velocity to floating point coordinates (x2, y2) how are delta x and delta y calculated for 1/60th of a second?

CHAT
To calculate the change in x (delta x) and change in y (delta y) for 1/60th of a second, you can use the following formulas: (ME: the formulas are not printable as given. i edited them)

dx = (x2 - x1)/60​
dy = (y2 - y1)/60

These formulas assume that the object is moving at a constant velocity over the given time interval. The idea is to find the average rate of change in x and y per second and then divide by 60 to get the change for 1/60th of a second.

ME
your answer seems to be incorrect or incomplete. so let me add some constraints. Let's say that to travel along the longer axis by a delta of 1 how is the delta along the shorter axis calculated

CHAT
Certainly, let me clarify. If you have an object moving from point (x1, y1) to (x2, y2),
and you want to calculate the change in x and y for each time step,
you can use the following formulas based on the ratio of the longer and shorter axes:

if x2 - x1 >= y2 - y1 then
dx = (x2 - x1) / 60
dy = (y2 - y1) / 60 * (y2 - y1) / dx
else ...

These formulas ensure that the object moves at a constant speed along the longer axis while maintaining the appropriate ratio with the shorter axis.
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Re: I used ChatGPT

Post by kingnoob »

Well that is not exactly what I wanted. So I went back to ChatGPT and asked a better question.
my previous two questions (1. in two dimensional space if an object is at floating point coordinates (x1, y1) and is moving at a constant velocity to floating point coordinates (x2, y2) how are delta x and delta y calculated for 1/60th of a second? 2. your answer seems to be incorrect or incomplete. so let me add some constraints. Let's say that to travel along the longer axis by a delta of 1 how is the delta along the shorter axis calculated) did not yield an answer i could use so let me ask another. if i want to travel a distance of 1 along the (x1, y1) (x2, y2) path how do i calculate delta x and delta y?
This time I asked ChatGPT to give the formulas in Lua.

Code: Select all

-- Calculate the differences between coordinates
delta_x = x2 - x1
delta_y = y2 - y1

-- Calculate the magnitude of the vector (delta_x, delta_y)
magnitude = math.sqrt(delta_x^2 + delta_y^2)

-- Normalize the deltas
delta_x_normalized = delta_x / magnitude
delta_y_normalized = delta_y / magnitude

-- Scale the normalized deltas by the desired distance (1 in this case)
distance_to_travel = 1
delta_x_scaled = delta_x_normalized * distance_to_travel
delta_y_scaled = delta_y_normalized * distance_to_travel
So the above code is still not the answer. Even if distance_to_travel is 0.001 it will take a thousand steps no matter the distance. So how do we calculate distance_to_travel so that the velocity remains the same no matter the initial distance? Back to ChatGPT I go. lol
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Re: I used ChatGPT

Post by kingnoob »

ME
Here is a post i made on a lua forum. "Well that is not exactly what I wanted. So I went back to ChatGPT and asked a better question.
my previous two questions (1. in two dimensional space if an object is at floating point coordinates (x1, y1) and is moving at a constant velocity to floating point coordinates (x2, y2) how are delta x and delta y calculated for 1/60th of a second? 2. your answer seems to be incorrect or incomplete. so let me add some constraints. Let's say that to travel along the longer axis by a delta of 1 how is the delta along the shorter axis calculated) did not yield an answer i could use so let me ask another. if i want to travel a distance of 1 along the (x1, y1) (x2, y2) path how do i calculate delta x and delta y?
This time I asked ChatGPT to give the formulas in Lua.
CODE: SELECT ALL

-- Calculate the differences between coordinates
delta_x = x2 - x1
delta_y = y2 - y1

-- Calculate the magnitude of the vector (delta_x, delta_y)
magnitude = math.sqrt(delta_x^2 + delta_y^2)

-- Normalize the deltas
delta_x_normalized = delta_x / magnitude
delta_y_normalized = delta_y / magnitude

-- Scale the normalized deltas by the desired distance (1 in this case)
distance_to_travel = 1
delta_x_scaled = delta_x_normalized * distance_to_travel
delta_y_scaled = delta_y_normalized * distance_to_travel
So the above code is still not the answer. Even if distance_to_travel is 0.001 it will take a thousand steps no matter the distance. So how do we calculate distance_to_travel so that the velocity remains the same no matter the initial distance?" Can you deduce what i am looking for from this post?
ChatGPT
Certainly! It seems like you're trying to calculate the movement along a path defined by two points (x1, y1) and (x2, y2) in a way that the velocity remains constant, regardless of the initial distance between the points. In other words, you want to determine the appropriate value for distance_to_travel such that the object moves a certain distance along the path in a consistent time interval (1/60th of a second in this case).

Here's a breakdown of the code you provided and an explanation of how to adjust it for your needs:

Code: Select all

-- Calculate the differences between coordinates
delta_x = x2 - x1
delta_y = y2 - y1

-- Calculate the magnitude of the vector (delta_x, delta_y)
magnitude = math.sqrt(delta_x^2 + delta_y^2)

-- Normalize the deltas
delta_x_normalized = delta_x / magnitude
delta_y_normalized = delta_y / magnitude

-- Calculate the desired velocity
desired_velocity = 1 / 60  -- Assuming 60 steps per second

-- Calculate the distance to travel to achieve the desired velocity
distance_to_travel = desired_velocity * magnitude

-- Scale the normalized deltas by the calculated distance
delta_x_scaled = delta_x_normalized * distance_to_travel
delta_y_scaled = delta_y_normalized * distance_to_travel
In this modified code, the key addition is the calculation of distance_to_travel based on the desired velocity. This value is then used to scale the normalized deltas, ensuring that the object moves along the path with a constant velocity, regardless of the initial distance between the points (x1, y1) and (x2, y2).
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Re: I used ChatGPT

Post by kingnoob »

After a couple of back and forths with ChatGPT I presented ChatGPT with some code.

Code: Select all

dx = x2 - x1
dy = y2 - y1
mag = math.sqrt(dx ^ 2 + dy ^ 2)
dxn = dx / mag
dyn = dy / mag
vel = 1 / 60
dist = vel * mag
dxs = dxn * dist
dys = dyn * dist
while x1 ~= x2 and y1 ~= y2 do
    x1 = x1 + dxs
    y1 = y1 + dys
end
And asked, does x1 and y1 converge on x2 and y2 and what to do about cpu math error.
The provided code attempts to move from (x1, y1) to (x2, y2) with a constant velocity. However, there are a couple of issues:

The loop termination condition may not work as expected because floating-point numbers might not be exactly equal due to precision errors. Instead, you should check if the absolute difference is below a small threshold.

There's a risk of an infinite loop if dxs and dys are very small, and the loop condition is never satisfied. To prevent this, you might want to add a maximum iteration count or other termination condition.

Here's an adjusted version of your code:

Code: Select all

dx = x2 - x1
dy = y2 - y1
mag = math.sqrt(dx ^ 2 + dy ^ 2)
dxn = dx / mag
dyn = dy / mag
vel = 1 / 60
dist = vel * mag
dxs = dxn * dist
dys = dyn * dist

-- Set a small threshold for termination
local epsilon = 1e-6

-- Maximum iteration count to avoid infinite loop
local maxIterations = 1000
local iterations = 0

while math.abs(x1 - x2) > epsilon or math.abs(y1 - y2) > epsilon do
    x1 = x1 + dxs
    y1 = y1 + dys

    iterations = iterations + 1
    if iterations > maxIterations then
        print("Max iterations reached. Breaking loop.")
        break
    end
end
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Re: I used ChatGPT

Post by kingnoob »

It appears that I'm smarter than ChatGPT. lol
I just got the impression that Chat's code was overly complicated so I proposed the following code to ChatGPT.

Code: Select all

distance = math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
delta_x = x2 - x1
delta_y = y2 - y1
delta_x_scaled = delta_x / distance
delta_y_scaled = delta_y / distance
-- Set a small threshold for termination
local epsilon = 1e-6

-- Maximum iteration count to avoid infinite loop
local maxIterations = 1000
local iterations = 0

while math.abs(x1 - x2) > epsilon or math.abs(y1 - y2) > epsilon do
    x1 = x1 + delta_x_scaled
    y1 = y1 + delta_y_scaled
    iterations = iterations + 1
    if iterations > maxIterations then
        print("Max iterations reached. Breaking loop.")
        break
    end
end
ChatGPT
Your attempt at simplification is reasonable and shares similarities with the previous code. It calculates the distance between the points (x1, y1) and (x2, y2) using the Euclidean distance formula, and then scales the differences delta_x and delta_y by the distance to get normalized values delta_x_scaled and delta_y_scaled. The subsequent loop aims to move from (x1, y1) to (x2, y2) with a constant velocity, similar to the previous code.

Here are a couple of points to consider:

Normalization Check: Ensure that the distance is not zero before normalizing (delta_x_scaled and delta_y_scaled). If the distance is zero, it may lead to division by zero errors.

if distance > 0 then
delta_x_scaled = delta_x / distance
delta_y_scaled = delta_y / distance
else
-- Handle the case where distance is zero (points are the same)
end
Termination Condition: As with the previous code, make sure to account for floating-point precision errors in the termination condition. The use of a small threshold (epsilon) is appropriate.

Iteration Limit: The inclusion of a maximum iteration count (maxIterations) is a good practice to avoid potential infinite loops. Adjust the value based on your specific use case.

Overall, your code is a valid and simplified version, and it should work well for moving from (x1, y1) to (x2, y2) with a constant velocity, considering the mentioned considerations.
A whole day with a smart AI to figure out what should be a simple thing. I'm not kingnoob for no reason. lol
Now maybe I can write some real code and start testing. :)
User avatar
pgimeno
Party member
Posts: 3551
Joined: Sun Oct 18, 2015 2:58 pm

Re: I used ChatGPT

Post by pgimeno »

kingnoob wrote: Tue Dec 26, 2023 12:44 am It appears that I'm smarter than ChatGPT. lol
Surprised? You shouldn't ;)

The formulas answered for one of the questions make no sense whatsoever (the ones "based on the ratio of the longer and shorter axes").

The answer you need is:

Code: Select all

local speed = 1 -- in pixels per second
local dt = 1/60 -- you should actually use love.update's `dt` parameter here, not define it
local dx = x2 - x1
local dy = y2 - y1
local magnitude = math.sqrt(dx*dx + dy*dy)
local multiplier = speed * dt / magnitude
if multiplier > 1 then
  -- we're going to overshoot - go to destination instead
  x1 = x2
  y1 = y2
else
  x1 = x1 + dx * multiplier
  y1 = y1 + dy * multiplier
end
User avatar
MikeHart
Prole
Posts: 10
Joined: Sun Jan 27, 2019 9:26 pm

Re: I used ChatGPT

Post by MikeHart »

After all, chatgpt is just an advanced search engine.
RNavega
Party member
Posts: 251
Joined: Sun Aug 16, 2020 1:28 pm

Re: I used ChatGPT

Post by RNavega »

To be honest, in your first question I think it answered properly. There's an ambiguity to "delta x and delta y calculated for 1/60th of a second". I don't think ChatGPT is programmed to respond with another question that asks for clarification, it's always affirmative. So it must've tried to guess what it meant.

Say, the starting position is "p0", the ending position is "p1", the velocity vector is "v" and it's accumulated over a time "t":
p0 + v * t = p1
(Meaning, "If I start from location p0 and travel with a "t" amount of "v" steps, I will reach location p1".)

So the delta X and Y (the components of 2D vector v) can be found by isolating vector v:
p0 + v * t = p1
v * t = p1 - p0
v = (p1 - p0) / t
(since v is a 2D vector, you have components vx = (p1x - p0x) / t, and vy = (p1y - p0y) / t)

Which is what the AI first responded with. Then to avoid the floating point error, the ending condition should be to use a secondary variable to count time, starting from zero and checking if it ever reaches / passes beyond "t". Then set it to exactly "t" and consider that the target is reached.
User avatar
Bobble68
Party member
Posts: 160
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

Re: I used ChatGPT

Post by Bobble68 »

I'm curious what it would be like to completely get ChatGPT to create a game in love2d - would probably need some level of automation though, so that you can automatically disregard generated code that throws errors.

Edit: Screw it I'm gonna try it
Last edited by Bobble68 on Tue Dec 26, 2023 5:56 pm, edited 1 time in total.
Dragon
User avatar
kingnoob
Prole
Posts: 29
Joined: Thu Dec 21, 2023 6:52 am

Re: I used ChatGPT

Post by kingnoob »

pgimeno wrote: Tue Dec 26, 2023 2:59 pm
kingnoob wrote: Tue Dec 26, 2023 12:44 am It appears that I'm smarter than ChatGPT. lol
Surprised? You shouldn't ;)

The formulas answered for one of the questions make no sense whatsoever (the ones "based on the ratio of the longer and shorter axes").

The answer you need is:
Thanks! However the code derived from ChatGPT actually works just fine and is super simple. The only thing needed to be added is scaling so the user can change how fast the fleets move.

Here is the pertinent fleet creation code

Code: Select all

            x1 = star[org].x
            y1 = star[org].y
            x2 = star[dst].x
            y2 = star[dst].y
            dist = math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
            fleet[i].dx = (x2 - x1) / dist
            fleet[i].dy = (y2 - y1) / dist
            fleet[i].owner = r
            fleet[i].x1 = x1
            fleet[i].y1 = y1
            fleet[i].x2 = x2
            fleet[i].y2 = y2
            
And here is the pertinent fleet update code (just some demo code)

Code: Select all

        x1 = fleet[i].x1
        y1 = fleet[i].y1
        x2 = fleet[i].x2
        y2 = fleet[i].y2
        dx = fleet[i].dx
        dy = fleet[i].dy
        d = math.sqrt(dx ^ 2 + dy ^ 2)
        x1 = x1 + dx
        y1 = y1 + dy
        if math.abs(x2 - x1) < 1 and math.abs(y2 - y1) < 1 then
            fleetTop = fleetTop - 1
            fleet[i] = fleet[fleetTop]
            fleet[fleetTop] = {}
        else
            fleet[i].x1 = x1
            fleet[i].y1 = y1
        end
If you want to see this code in action just run the attachment. And again, thanks for the reply. :)
Note: I see I left in an unused line "d = math.sqrt(dx ^ 2 + dy ^ 2)", removing it
Attachments
VerySimpleSpaceGame.zip
Fleet Move Demo
(3.57 KiB) Downloaded 19 times
Post Reply

Who is online

Users browsing this forum: No registered users and 66 guests