Moving by "x" with phisic

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
Cruiser23rus
Prole
Posts: 9
Joined: Mon May 04, 2009 11:41 am

Moving by "x" with phisic

Post by Cruiser23rus »

Hello. I do game. My character must move by "x" at left and right. When i don't use phisic model, i do it with change "x" of grafic in "update (dt)" function.
But now, when i use phisic model, i must do another method? Something with body? And what function it's must be? I wright my code heare.
Help me, please.

Code: Select all

function load()
love.graphics.setBackgroundColor(212, 233, 247) 

  -- Create a world with size 2000 in every direction. 
   world = love.physics.newWorld(2000, 2000) 
   world:setGravity(0, 100) 
 
   -- Create the ground body at (0, 0) with mass 0. 
   ground = love.physics.newBody(world, 0, 0, 0) 
    
   -- Create the ground shape at (400,500) with size (600,10). 
   ground_shape = love.physics.newRectangleShape(ground, 0, 545, 2000, 10)  

    image = love.graphics.newImage("images/back.png")
    colob1 = love.graphics.newImage("images/kolob.png")
      
   -- Create a Body for the circle. 
   body = love.physics.newBody(world, 50, 512) 
    
   -- Attatch a shape to the body. 
circle_shape = love.physics.newCircleShape(body, 28) 
  body:setMassFromShapes()
end

function draw() 
    love.graphics.draw(image, 515, 350)
  love.graphics.draw(colob1,body:getX(), body:getY(), body:getAngle()) 
end

function update(dt)    
   world:update(dt) 
end 

function keypressed(k) 
   if k == love.key_up then     
      body:applyImpulse(0, -300000)         
   end 
end 

Cruiser23rus
Prole
Posts: 9
Joined: Mon May 04, 2009 11:41 am

Re: Moving by "x" with phisic

Post by Cruiser23rus »

Why I have collison when i try to move at "x"?

Try my code

Code: Select all

function load()
love.graphics.setBackgroundColor(212, 233, 247) 

  -- Create a world with size 2000 in every direction. 
   world = love.physics.newWorld(2000, 2000) 
   world:setGravity(0, 100) 
 
   -- Create the ground body at (0, 0) with mass 0. 
   ground = love.physics.newBody(world, 0, 0, 0) 
    
   -- Create the ground shape at (400,500) with size (600,10). 
   ground_shape = love.physics.newRectangleShape(ground, 0, 545, 2000, 10) 
 
 

    image = love.graphics.newImage("images/back.png")
    colob1 = love.graphics.newImage("images/kolob.png")
        x=50
        y=512  
   -- Create a Body for the circle. 
   body = love.physics.newBody(world, x, y) 

   -- Attatch a shape to the body. 

circle_shape = love.physics.newCircleShape(body, 28) 
  body:setMassFromShapes()



end





function draw() 
    love.graphics.draw(image, 515, 350)
  love.graphics.draw(colob1,body:getX(), body:getY(), body:getAngle()) 


end



function update(dt) 
   
   world:update(dt) 
if love.keyboard.isDown(love.key_left) then 
   x = x - 500 * dt 
    end 
    if love.keyboard.isDown(love.key_right) then 
   x = x + 500 * dt 
    end 
body:setX( x )
end



 
function keypressed(k) 
  
 
   if k == love.key_up then 
 
      body:applyImpulse(0, -300000) 

    
   end 
     


end


User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Moving by "x" with phisic

Post by Robin »

I'm not sure what you mean by "collision", I can't find any collision code in your code. I also tested it, and it seemed to work fine. Maybe you could say what the problem is in other words?
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 129 guests