# Physics.linearVelocity to pixels per frame

I divided linearVelocity of the ball which is in pixels per second by FPS so that i get pixels moved by the ball in pixels per frame. And moved the other body up by that much. Then according to me the ball should always be in the middle of the screen but if you run the code you’ll see that the ball is below height/2. What’s the mistake I am making? EDIT SILLY MISTAKE GOD KNOWS WHAT I WAS THINKING SO MUCH ABOUT IT. SOLVED!!

``````function setup()
ball = physics.body(CIRCLE,20)
ball.x = WIDTH/2
ball.y = HEIGHT/2
ball.linearVelocity = vec2(0,0)
x1,x2,y = 0,WIDTH,0
end

function draw()
FPS = 1/DeltaTime
dy = ball.linearVelocity.y/FPS
y = y-dy
background(255, 255, 255, 255)
pushStyle()
fill(255, 0, 0, 255)
ellipse(ball.x,ball.y,40)
popStyle()
floor = physics.body(EDGE,vec2(x1,y),vec2(x2,y))
pushStyle()
stroke(0, 0, 0, 255)
strokeWidth(5)
line(x1,y,x2,y)
line(0,HEIGHT/2,WIDTH,HEIGHT/2)
popStyle()
end
``````

:">

Is there any way to convert the linearVelocity to pixels per frame from pixels per second which does not include DeltaTime because including DeltaTime is making things inaccurate.

@Saurabh: Nope. Velocity is displacement over time, so by definition it is not possible (at least, not when using the physics engine; without the concept of time, a physics engine won’t work). What is the inaccuracy you are seeing? Factoring DeltaTime into your calculations should result in more accuracy, not less…

a) You don’t need to do:

``````FPS = 1/DeltaTime
dy = ball.linearVelocity/FPS
``````

You can just do:

``````dy = ball.linearVelocity * DeltaTime
``````
1. are you aware that this line:
``````floor = physics.body(EDGE, vec2(x1, y), vec2(x2, y))
``````

is creating a new physics body every draw() call? You probably want to move that into your setup() function.

Is this way possible. Where dx is the change in displacement in that frame?

``````ballx={}
table.insert(ballx,1,ball.x)
if #ballx>1 then
dx = ballx[2]-ballx[1]
end
``````

I know that it is creating a new physics body and I need it that way. I want it that way as I want to move it to a different place every time. Can we give velocity to edges??

@Saurabh: Again, I’m not exactly sure what you are trying to accomplish, or what the issue you are having is, so I don’t know if that will work; it all depends.

Re: creating the floor every frame: why don’t you just create the floor body once (as a kinematic body), and use linearVelocity to move it? Recreating the floor every frame is wildly inefficient, and, since you aren’t deleting the floor from the previous frame, it’s probably interfering with your current frame’s simulation.

Using a kinematic body, you don’t actually have to make any calculations at all if you want your floor to move at a constant rate. Just set the linear velocity (once) to the speed at which you would like it to move every frame, and let the physics engine solve that for you.

By making the floor in the draw i am only changing the x,y coordinates i am not creating a new body every time else the previous one would have stayed there and I would have come to know that it’s there you got me right. And the floor is not actually the floor your thinking of its the black lines you see in the program. Can I change the length of the physics body which is a edge if I define it only once in setup?

If the physics body is a polygon can you not change the vertices using body.points? If not you’ll need to destroy the physics body and recreate it so no bodies are left in the gc. The way I do it which is faster is create all the physics bodies when I press a button and then remove them when I press the same button

@Saurabh: no, the bodies are still there, you are just only drawing the most recent body you are creating (yes, you are creating a new “floor” body every frame…that’s what physics.body() does).

The other bodies will remain until either a) you delete them, or b) the garbage collector collects them.

@Saurabh: EDIT: sorry, that wasn’t exactly what you are trying to do, but I gotta go for a bit. I’ll post a better example later.

Oh okay thanks found the solution thanks @Toadkick!!

@Saurabh: Ah good! Glad I could help

One more thing how should I draw line over a moving inclined EDGE? Here’s the code which will need editing.

``````
--# Main
function setup()
floor = physics.body(EDGE,vec2(0,0),vec2(WIDTH,100))
floor.type = KINEMATIC
floor.linearVelocity = vec2(0,100)
end

function draw()
background(40, 40, 50)
strokeWidth(5)
stroke(255, 255, 255, 255)
line(0,floor.y1??,WIDTH,floor.y2??)--what should i put here
end

``````

Anyone??

Okay I found the answer to my previous problem but the new problem is if I move the line in the balls frame the restitution of the ball doesn’t somehow remain zero like in this code. If anyone could help me out

``````function setup()
floor = physics.body(EDGE,vec2(WIDTH/2-250,100),vec2(WIDTH,20))
floor.type = KINEMATIC
floor.restitution = 0
parameter.watch("floor.x")
ball = physics.body(CIRCLE,20)
ball.x = WIDTH/2
ball.y = HEIGHT/2
ball.linearVelocity = vec2(0,0)
ball.restitution = 0
end

function draw()
ball.x = WIDTH/2
ball.y = HEIGHT/2
floor.linearVelocity = vec2(-ball.linearVelocity.x,-ball.linearVelocity.y)
background(40, 40, 50)
strokeWidth(5)
stroke(255, 255, 255, 255)
line(floor.x+WIDTH/2-250,floor.y+100,floor.x+WIDTH,floor.y+20)
fill(255, 0, 0, 255)
ellipse(ball.x,ball.y,40)
end
``````