Movement of Sprite Help

@thebugel You are not bugging me, and don’t ever feel that way when you ask a question here. Here is an example of the clock I think you want. You can set the startTime to 2245 for a quick way to test the 23:00, your end time. One way to learn is to look at code written by other people and try to understand what they’re doing. Another is to write something very small and experiment with that. Try different commands and see what they do. Look thru the online manual at the different functions and their explanation. If you don’t know what a function does, you won’t know how to use it. As for logic, that comes with experience. You can’t learn everything at once so just keep at it. I still learn things by looking at the code submitted by others here.


function setup()
    startTime=500
    et=0    -- setting to 0 starts the clock
end

function draw()
    background(40, 40, 50)
    fill(255)
    clock()
end

function clock()
    if et~=nil and ElapsedTime-et>1 then
        startTime=startTime+1
        et=ElapsedTime
    end
    min=startTime%100
    hr=math.floor(startTime/100)
    if min>=60 then
        startTime=(hr+1)*100
    end
    if startTime==2300 then
        et=nil    -- setting to nil stops the clock
        str="time expired"
    else
        str=string.format("%02d:%.02d",hr,min)
    end
    text(str,WIDTH/2,HEIGHT/2)
end

Thanks @dave1707 Would you be as kind to post the code but without the scroll, I just can’t seem to remove it without messing the code up. Scrolling will come in handy later but trying to keep it simple on one screen for now. Iv manged to integrate the clock with the code (I think) . Thanks in advance

@thebugel Below are lines in their functions that should be commented out or removed. Also in function touched(t), replace v1=vec2(t.x-mx,t.y-my) with v1=vec2(t.x,t.y) . If you make these changes and you still have problems, I’ll repost the whole code.


function setup()
    --mx=0
    --my=0
end

function draw()
    --translate(mx,my)


function touched(t)
    --[[
    if t.state==MOVING and arrived then    -- scroll screen
        mx=mx+t.deltaX
        my=my+t.deltaY
    end
    --]]
        --ms=5    -- set move screen speed at 5 pixels for off screen speed

         --v1=vec2(t.x-mx,t.y-my)  -- comment out or remove
        v1=vec2(t.x,t.y)   -- add this line

function checkArrived()
            --[[
            -- check if on screen train is at the edges
            if x+mx>50 and x+mx<WIDTH-50 and y+my>50 and y+my<HEIGHT-50 then
                ms=3    -- set move screen speed to 3 pixels
            end
            -- move screen to put or keep the train in view
            if x+mx<50 then
                mx=mx+ms
            end
            if x+mx>WIDTH-50 then
                mx=mx-ms
            end
            if y+my<50 then
                my=my+ms
            end
            if y+my>HEIGHT-50 then
                my=my-ms
            end
            --]]

@dave1707 , just had a play with it & Can’t manage to do it without errors if you don’t mind taking it out.

Going back to the stations completed part. Im not wanting every point of movement as a station, most of them are just to move the elipse about the board.

Using this as an example I have the the following code in the function set dest()

{vec2(370,538),vec2(370,314),vec2(370,90)},
{vec2(370,314),vec2(370,538),vec2(594,314),vec2(370,90)},
{vec2(594,314),vec2(370,314)},
{vec2(370,90),vec2(370,314),vec2(370,538)},

Out these four points I only want vec2(594,314) as a station. The other points are just movement points. I’m struggling to find out how to figure this out too, is it something simple?

Thanks in advance…and thanks for your time

@thebugel Here is the code with the screen scrolling code removed. I also added code so that all the points don’t have to be stations. What controls a point being a station is the first entry in each line of the dest table. I made that entry a vec3. It contains the x,y values plus a 1 or 0. A 1 means it’s a station, a 0 is just a connecting point. I also changed code in setup() as to where the train starts. Just change the “at” value to a dest table offset of the point you want the train to start. I have it starting from the 1st point in the dest table. I also added code in drawStations() to show the x,y value next to the connecting point or station. This will help when setting up the points and stations. Once you get the points set up, that line of code can be commented out. I didn’t do anything with the time, I’m not sure what you have in mind for that. I also added several more points and stations to the dest table so I could test it more. Remove what you don’t need.


displayMode(FULLSCREEN)

function setup()
    rectMode(CENTER)
    setDest()
    at=1    -- starting train position, dest table offset
    x=dest[at][1].x
    y=dest[at][1].y
    setAtDest()
    totalTime=0
    startTimer=false
    arrived=true
    speed=2
end

function draw()
    background(40, 40, 50)
    showTime()
    drawTracks()
    drawStations()
    drawTrain()
    checkArrived()
    checkAllStations()
end

function touched(t)
    if done and t.tapCount==2 then    -- restart
        restart()
    end
    if t.state==BEGAN and arrived then
        startTimer=true
        v1=vec2(t.x,t.y)
        -- is touched point a valid destination
        for a,b in pairs(dest[at]) do
            d=v1:dist(vec2(b.x,b.y))
            if d<20 then
                -- valid destination
                currDest=vec2(b.x,b.y)
                -- get dest table offset of destination
                for c,d in pairs(dest) do
                    if d[1].x==b.x and d[1].y==b.y then
                        at=c     -- dest table offset
                        break
                    end
                end
                -- determine new speed and direction of train
                v1=vec2(x,y)
                d=v1:dist(vec2(b.x,b.y))
                dx=(b.x-x)/d*speed
                dy=(b.y-y)/d*speed
                arrived=false
                break
            end
        end
    end
end

function setDest()    -- table of the destination points
    dest = {
    {vec3(370,538,0),vec2(370,314),vec2(370,90)}, 
    {vec3(370,314,0),vec2(370,538),vec2(594,314),vec2(370,90)}, 
    {vec3(594,314,1),vec2(370,314),vec2(594,700),vec2(594,200)},     
    {vec3(370,90,0),vec2(370,314),vec2(370,538),vec2(100,90)},
    {vec3(594,700,1),vec2(200,700),vec2(594,314)},
    {vec3(200,700,0),vec2(200,200),vec2(594,700)},
    {vec3(200,200,1),vec2(200,700),vec2(594,200)},
    {vec2(594,200,0),vec2(594,314),vec2(200,200)},
    {vec3(100,90,1),vec2(370,90)}
           }
end

function drawTracks()
    stroke(255)
    strokeWidth(3)
    for z=1,#dest do
        for s=2,#dest[z] do
            line(dest[z][1].x,dest[z][1].y,dest[z][s].x,dest[z][s].y)
        end
    end
end

function drawTrain()
    fill(255)
    ellipse(x,y,30)
end

function drawStations()
    for a,b in pairs(dest) do
        fill(255)
        
        -- display x,y values on the screen
        text("("..b[1].x..","..b[1].y..")",b[1].x+40,b[1].y-30)
        -- comment above line when text not needed
        
        strokeWidth(0)
        if b[1].z==1 then    -- a station
            fill(255)
            if atDest[a]==true then
                fill(0,255,0)
            end
            rect(b[1].x,b[1].y,40,40)           
        else
            ellipse(b[1].x,b[1].y,10)
        end
    end 
end  

function checkArrived()
    -- check if train arrived at destination
    if not arrived then
        v1=vec2(currDest.x,currDest.y)
        d=v1:dist(vec2(x,y))
        if d>3 then -- train hasn't arrived yet
            x=x+dx
            y=y+dy
        else
            x=currDest.x    -- train arrived, set exact dest x,y position
            y=currDest.y
            arrived=true
            atDest[at]=true
        end
    end    
end

function setAtDest()  
    atDest={}     
    for z=1,#dest do
        atDest[z]=false
    end
    atDest[at]=true
end

function checkAllStations()
    done=true
    for z=1,#atDest do
        if dest[z][1].z==1 and atDest[z]==false then
            done=false
        end
    end
end

function showTime()
    fill(255)
    if not startTimer then
        et=ElapsedTime
    end
    print(done)
    if not done then
        totalTime=string.format("%0.2f",ElapsedTime-et)
    else
        text("All stations reached in",WIDTH/2,HEIGHT-50)  
        text("Double tap screen to restart",WIDTH/2,HEIGHT-160)
    end
    text(totalTime.."  seconds",WIDTH/2,HEIGHT-90)
end

Execellent thanks @dave1707 I’m well underway now. I’m wanting to create another tab called “train” just to get used to adding a new class which I’ve not done before. In here for now is just going to be the rotation of the train between way points (so the train is always facing direction of travel). I was looking at the code for pacmam and under the pacman class contains its rotation code, is it just a case of writing something similar or will the sprite only rotate at certain gps points?

@thebugel You shouldn’t have any problems rotating the train so it is always pointing in the direction it’s moving. Somewhere on the forum I have a little car that moves around the screen and it always points in the direction it’s moving. I’ll see if I can locate it and you can look at that code. I’ll post the link here when I find it.

@thebugel I couldn’t find what I was talking about, so I threw together this little demo. The space ship will rotate to keep pointing towards your finger as you move your finger around the screen. Maybe this will help with the rotation of the train.

thanks…should the demo have been under your last post?

@thebugel Sorry, here’s the code. I was in a hurry and never look after the previous post.


displayMode(FULLSCREEN)

function setup()
    cx=400;cy=600
    tx=0;ty=0
    z=0
end

function touched(t)
    if t.state==BEGAN or t.state==MOVING then
        z=math.deg(math.atan2(cy-t.y,cx-t.x))
    end
end

function draw()
    background(40, 40, 50)
    fill(255)
    pushMatrix()
    translate(cx,cy)
    rotate(z+90)
    sprite("Space Art:Red Ship",0,0)
    popMatrix()
end