[SOLVED] Car jumping ahead?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

[SOLVED] Car jumping ahead?

Post by xNick1 »

Hi guys,
I'm having some problems with a car.
It doesn't move in a fluent way, it kind of jumps every few frames.
I think it's screen tearing

Could you check it out?
Love file attached.
I tried to print values and stuff.
It seems that my camera is too slow and can't follow the car.

If I set car velocity to 30 or less, it doesn't have that problem.
I face that problem if the car goes faster. (I need it to run fast)

The car has that problem, the player doesn't o.O
car.love
(133.47 KiB) Downloaded 48 times
Last edited by xNick1 on Sun May 21, 2017 4:01 pm, edited 3 times in total.
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Car jumping ahead?

Post by xNick1 »

https://www.youtube.com/watch?v=-bP3Wjhj9Y0


car :

Code: Select all

local wWidth, wHeight = lg.getDimensions()

CarSpring = Object:extend()

function CarSpring:new(x)
    self.x = x
    self.timeStill = 2
    self.timeRight = 0.2
    self.timePassedStill = 0
    self.timePassedRight = 0
    self.timeChangeSpeed = 2
    self.selectedStill = 1
    self.selectedRight = 1
    self.direction = "still"
    self.still = {
        lg.newImage("car/idle/car.png")
    }
    self.width = wWidth / 4
    self.height = wHeight / 5
    self.sx = self.width / self.still[1]:getWidth()
    self.sy = self.height / self.still[1]:getHeight()
    for k, i in ipairs(self.still) do
        i:setFilter('nearest', 'nearest')
    end
    self.right = {
        lg.newImage("car/right/car.png"),
        lg.newImage("car/right/car1.png")
    }
    for k, i in ipairs(self.right) do
        i:setFilter('nearest', 'nearest')
    end
    local y = wHeight - self.height
    self.y = y + wHeight / 100
end

function CarSpring:update(dt)
    if self.direction == "still" then
        self.timePassedStill = self.timePassedStill + self.timeChangeSpeed * dt
        
        if self.timePassedStill >= self.timeStill then
            self.timePassedStill = 0
            
            if self.selectedStill == #self.still then
                self.selectedStill = 1
            else
                self.selectedStill = self.selectedStill + 1
            end
        end
    end
        
    if self.direction == "right" then
        self.x = self.x + 400 * dt
        self.timePassedRight = self.timePassedRight + self.timeChangeSpeed * dt
        
        if self.timePassedRight >= self.timeRight then
            self.timePassedRight = 0
            
            if self.selectedRight == #self.right then
                self.selectedRight = 1
            else
                self.selectedRight = self.selectedRight + 1
            end
        end
    end

end

function CarSpring:draw()
    lg.setColor(255, 255, 255)
    if self.direction == "still" then
        lg.draw(self.still[self.selectedStill], self.x, self.y, 0, self.sx, self.sy)
    end
        
    if self.direction == "right" then
        lg.draw(self.right[self.selectedRight], self.x, self.y, 0, self.sx, self.sy)
    end
    
end
main:

Code: Select all

lg = love.graphics
li = love.image
la = love.audio
lm = love.mouse
lk = love.keyboard
lt = love.timer
le = love.event
ls = love.system
lw = love.window
lf = love.filesystem

local wWidth, wHeight = lg.getDimensions()

Object = require("classic")
require("camera")

local entities = {
    "carSpring"
}

for k, e in ipairs(entities) do
    require(e)
end



local carSpring = CarSpring(wWidth / 2)

function love.load()
end

function love.update(dt)
             
        carSpringMovement(dt, wWidth, wHeight)
        cameraCarSpringMovement(dt, wWidth, wHeight)
        carSpring:update(dt)
 

function love.draw()
    lg.setColor(255, 255, 255)
    
    camera:set()
        
    carSpring:draw()
    
    camera:unset()

end

function carSpringMovement(dt, wWidth, wHeight)
    carSpring.direction = "still"
    
    if lm.isDown(1) then
        local x, y = lm.getPosition()
        local center = wWidth / 2;
        if x > center then
            carSpring.direction = "right"
        end
    end
        
end

function cameraCarSpringMovement(dt, wWidth, wHeight)
    if carSpring.x > wWidth / 3 then
        camera.x = carSpring.x - wWidth / 3
    end

end



SOLVED: Thank you MasterGeek_
Post Reply

Who is online

Users browsing this forum: No registered users and 70 guests