[SOLVED] What am I doing wrong? (about defining variables)

@quezadav I created a physics ellipse and rolled it across the screen plotting the center point. I don’t think it’s accurate enough to get an exact answer for the Euler problem 525.

@quezadav Here my version.

https://m.youtube.com/watch?v=mZGsXL_kelI

@dave1707, Wow! You did it in one day! I did it in a week or so 8-} … I can’t watch the video… It says, it’s private.

@quezadav I looked at the settings for the video and it said public. I changed it to private and then back to public again. Not sure why it’s saying private.

@dave1707 Now I can watch it. Nice! Did you set a slow velocity? By me, with my “drawn” ellipse, the simulation slowed down while graphing the path because, I think, of my code’s “mess”. On the other hand, I’m trying now to find the distance traveled by the ellipse’s center (adding the distances between points… which, also slows down my simulation :D) without success. I’ll be debugging my code and checking out what’s happening.

@quezadav I move the ellipse by setting the angularVelocity. One thing I noticed with my ellipse, depending on how many points I use to define the ellipse, sometimes there are points that don’t seem to work. Some sections of the ellipse will fall thru the floor.

@quezadav Here’s my code.

displayMode(FULLSCREEN)

function setup()
    c,tab={},{}
    a,b=50,100
    for x=a,-a,-3 do
        y=math.sqrt(b^2-((b^2*x^2)/a^2))
        table.insert(tab,vec2(x,y))
    end
    for x=-a,a,3 do
        y=math.sqrt(b^2-((b^2*x^2)/a^2))
        table.insert(tab,vec2(x,-y))
    end
    e=physics.body(POLYGON,unpack(tab)) 
    e.x=-100
    e.y=200
    e.friction=1
    f=physics.body(EDGE,vec2(-100,100),vec2(WIDTH,100))
    f.type=STATIC
    f.friction=1
end

function draw()
    background(40, 40, 50)
    stroke(255)
    strokeWidth(2)
    noFill()
    line(0,100,WIDTH,100)
    pushMatrix()
    e.angularVelocity=-100
    translate(e.x,e.y)
    rotate(e.angle)
    ellipse(0,0,a*2,b*2)
    popMatrix()
    table.insert(c,vec2(e.x,e.y))
    fill(255)
    for a,b in pairs(c) do
        ellipse(b.x,b.y,4)
    end
end

@dave1707 I move my ellipse by means of the accelerometer, so the velocity can be different any time. Here the body’s friction has an important effect on the body’s displacement. I construct my ellipse with a maximum of 40 points, which also has effect because of the not-so-rounded ellipse. How do you define your ellipse? I used as a proof the method described in comment #7 of this discussion: http://objectmix.com/graphics/394568-converting-ellipse-polygon.html

@dave1707 Thanks for the code, very smart. Here’s mine. It’s a proof of concept, very inefficient but working :smiley: Some issues with it but still in progress.

--[[In progress..
Rolling ellipse. Based on a challenge of Project Euler (https://projecteuler.net/problem=525)
Using information from: 
    http://www.kolve.com/mt_rollingEllipse/rollingEllipse.htm
    http://www.maplesoft.com/applications/view.aspx?SID=3612&view=html
]]--
displayMode(FULLSCREEN)

function setup()
    a,b=100,200 -- ellipse's major and minor axis
    a_2,b_2=a/2,b/2 -- half of that
    x,y=WIDTH-a,300 -- ellipse's initial position
    xr=x -- right limit's position
    yf=200 -- "floor"'s position
    xspeed=-3 -- moving to the left
--    xspeed=0
    p=math.pi*(a_2+b_2) -- ellipse's perimeter (a better approximation can be used)
    rot=0 -- rotation angle's initialization
    pts={} -- points for drawing the center's path
    d=0 -- for curve's length
end

function draw()
    background(0, 0, 0, 0)
    stroke(255)
    strokeWidth(2)
    table.insert(pts,vec2(x,y))
    pushMatrix()
        translate(x,y)    
        x = x + xspeed -- calculating x
 --       if x > xr or x < 100 then xspeed=xspeed*-1 end -- for changing direction in x
        rot=-2*math.pi*x/p -- calculating rotation angle
        y=yf+math.abs((-math.sin(rot)*math.tan(rot)*(a_2)^2)-(math.cos(rot)*(b_2)^2))/  math.sqrt((math.tan(rot)*a_2)^2+(b_2)^2) -- calculating y
        rotate(math.deg(rot)) -- rotating the ellipse
        if math.abs(math.deg(rot)) < 360 then xspeed=0 end -- stoping the ellipse (but not the simulation... working on this)
        ellipse(0,0,a,b) -- the ellipse
        ellipse(0,b/4,10) -- a focus
    popMatrix() 
    for z=2,#pts-1 do
        line(pts[z-1].x,pts[z-1].y,pts[z].x,pts[z].y) -- drawing the center's path... slows down the simulation... working on this)
        d=math.sqrt(math.abs(pts[z-1].x-pts[z].x)^2+math.abs(pts[z-1].y-pts[z].y)^2)+d -- adding distance bewtween points... somehow wrong... working on this
    end
    print(d)
    line(0,yf,WIDTH,yf) -- the "floor"
    line(100,0,100,HEIGHT) -- the left "limit"
    line(xr,0,xr,HEIGHT) -- the right limit
    ellipse(x,y,10) -- ellipse's center (set here for not rotating it)
end