Predicting path of a body/shape
Posted: Tue Oct 11, 2011 5:02 am
I'm trying to predict the path of a moving ball ahead of time, but am running into a variety of difficulties. The example program below helps demonstrate what I'm trying to simulate. Press spacebar to make the ball start moving.
In the current state, the ball obviously doesn't even remotely follow the predicted path. If I set the ball's restitution to 1, then it becomes pretty close, but the center of the ball does not follow the guide lines. I think the problem might be that the circle actually collides with the wall sooner than the guide line expects, by a factor of ~ the radius, but I'm not positive. I'm also not even sure if my "expected" path for the ball is even technically/physically correct.
Any ideas? Sorry if this is confusing
Code: Select all
require 'rectangle'
local world = love.physics.newWorld(0, 0, love.graphics.getWidth(), love.graphics.getHeight())
local height = 200
local y = (love.graphics.getHeight() - height) / 2
local x1 = 200
local y1 = 250
local angle = 1.2
local moving = false
local ball = {}
ball.body = love.physics.newBody(world, x1, y1, 1)
ball.shape = love.physics.newCircleShape(ball.body, 0, 0, 10)
ball.shape:setFriction(0)
ball.body:setBullet(true)
local topWall = {}
topWall.body = love.physics.newBody(world, 0, y)
topWall.shape = love.physics.newRectangleShape(topWall.body, love.graphics.getWidth() / 2, 0, love.graphics.getWidth(), 1)
topWall.shape:setFriction(0)
local bottomWall = {}
bottomWall.body = love.physics.newBody(world, 0, y + height)
bottomWall.shape = love.physics.newRectangleShape(bottomWall.body, love.graphics.getWidth() / 2, 0, love.graphics.getWidth(), 1)
bottomWall.shape:setFriction(0)
function love.update(dt)
world:update(dt)
end
function love.draw()
love.graphics.line(0, y, love.graphics.getWidth(), y)
love.graphics.line(0, y + height, love.graphics.getWidth(), y + height)
love.graphics.circle('line', ball.body:getX(), ball.body:getY(), ball.shape:getRadius(), 50)
local lastX, lastY = x1, y1
-- draw the zig zag lines
for i=1, 6 do
local currX, currY, knownSide
if (angle < 0) then
knownSide = lastY - y
currY = y
else
knownSide = y + height - y1
currY = y + height
end
local missingSide = math.abs(knownSide / math.tan(angle))
currX = lastX + missingSide
love.graphics.line(lastX, lastY, currX, currY)
lastX, lastY = currX, currY
angle = angle * -1
end
end
function love.keypressed(key)
if (key == ' ' and not moving) then
moving = true
local factor = 10
ball.body:applyImpulse(math.cos(angle) * factor, math.sin(angle) * factor)
end
end
Any ideas? Sorry if this is confusing