A Little Löve Game

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Matzexxxxx
Prole
Posts: 7
Joined: Tue Mar 06, 2012 6:20 pm

A Little Löve Game

Post by Matzexxxxx »

Hello.
I Worked until now with a few physic methods and the general callback, i also worked with love.draw and love.load.
I want to create a little Game, but i do not know some things, where u can help me maybe ;)


The First Thing is:
How can i let a Body / a Shape move to an other Body automatically?

Second:
Can Somebody Explain me how i can control a contact, if my player collide with something? i read the tutorial, but i didnt understand it ...

Last is, that my Walls at the end of the window dont work...
There are bodys, shapes and the bodys really exists, with the mass of 0.
I can draw them, but my body ignore it.

Can someone help me and:
If i have more questions about something, can i ask again?

Thank you ;)
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: A Little Löve Game

Post by Ellohir »

First thing: You can use https://love2d.org/wiki/Shape:setMask to set which shapes collide with others.

Second: Using https://love2d.org/wiki/World:setCallbacks you can set what function will execute when two shapes collide. There's an example at https://love2d.org/wiki/Tutorial:Physic ... _Callbacks which is quite simple.

Third: Everything dissapears from the World when goes out of the window. Or, at least, it's what I think. Putting the walls inside the window should fix that.


And, of course, you can ask again as many times as you need. I don't have much experience with the physics engine but there will be certainly someone who does :nyu:
Matzexxxxx
Prole
Posts: 7
Joined: Tue Mar 06, 2012 6:20 pm

Re: A Little Löve Game

Post by Matzexxxxx »

Thank you very much for this fast answer ;)
With the first thing i mean that something like a ball, for example runs automatically to my player when i open the game.
with body:setX() i dont think it will work, right?
The CollisionCallbacks i already read, but i didnt understand it :D

Edit: The Walls are at the border of the window, so they should work, but they dont really..
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: A Little Löve Game

Post by Ellohir »

Oh, sorry, I misread your first question. Yes, setX() will "teleport" the object. The most logical way would be to apply a force (with applyForce or applyImpulse) to the body. The x and y components can be calculated just substracting the player position to the object position and normalizing (so that the force is the same no matter how close or far the objects is from the player). I use this normalizing function:

Code: Select all

function math.normalize(x,y) local l=(x*x+y*y)^.5 if l==0 then return 0,0,0 else return x/l,y/l,l end end
Matzexxxxx
Prole
Posts: 7
Joined: Tue Mar 06, 2012 6:20 pm

Re: A Little Löve Game

Post by Matzexxxxx »

Thank you ;)
Just copy and paste the function?
or what variables i have to replace?
and sorry for my bad english, i am from germany normally :)

Its Better when i Post My Script i think.
Can you maybe look why my walls dont work?
Here the link:http://pastebin.com/raw.php?i=6wxhHq4q
User avatar
felix24
Party member
Posts: 163
Joined: Tue Jul 26, 2011 4:51 pm
Contact:

Re: A Little Löve Game

Post by felix24 »

in order to use the love.physics module, you have to set up a world.
eg

Code: Select all

world = love.physics.newWorld( -1000, -1000, 1000, 1000)
the four arguments are the min and max x and y coordinates of your world.

then you need to set the gravity for the x and y axis, and the scale of the world by setting the value (in pixels) of 1 meter.
eg

Code: Select all

world:setGravity(0, 200)
world:setMeter(64)
then you need to tell the world which functions to call when collisions occur.
eg

Code: Select all

world:setCallbacks(add, persist, remove, result)
the four arguments in setCallbacks() are the four functions that you need to define.
add is called when two shapes collide
persist is called when two shapes stay in contact
remove is called when two shapes stop colliding
and you can ignore result

do all of the above in love.load

now all you have to do is create the four functions mentioned above and decide what you want to happen when they are called.
in order for them to be called at all though, you need to keep the world updated. do this in love.update by calling:

Code: Select all

world:update(dt)
hopefully that somehow explains the physics side of things a bit.

anyway, after looking at your code i'm not sure you even need to use physics. if you just want to stop the player moving off the screen you could just check if they are at the boundary and stop them moving if so. try this:

Code: Select all

if downspeed == true and objects.player1.b:getY() < 595  then
    objects.player1.b:setY(objects.player1.b:getY() +5)
end
if upspeed == true and objects.player1.b:getY() > 5  then
    objects.player1.b:setY(objects.player1.b:getY() -5)
end
if rightspeed == true and objects.player1.b:getX() < 795  then
    objects.player1.b:setX(objects.player1.b:getX() +5)
end
if leftspeed == true and objects.player1.b:getX() > 5 then
    objects.player1.b:setX(objects.player1.b:getX() -5)
    Punkte = Punkte +1
end
good luck
Matzexxxxx
Prole
Posts: 7
Joined: Tue Mar 06, 2012 6:20 pm

Re: A Little Löve Game

Post by Matzexxxxx »

I Understand ;)
I made it such difficulter than it works :)
But the collisioncallbacks i dotn understand.
In the tutorial a circle falls down onto a static object.
the 4 functions for callbacks

Code: Select all

(function add(a, b, coll)
   
end

function persist(a, b, coll)
   
end

function rem(a, b, coll)
   
end

function result(a, b, coll)
   
end)
in the expample it looks like this:

Code: Select all

text = ""

function add(a, b, coll)
    text = text..a.." collding with "..b.." at an angle of "..coll:getNormal().."\n"
end

function persist(a, b, coll)
    text = text..a.." touching "..b.."\n"
end

function rem(a, b, coll)
    text = text..a.." uncolliding "..b.."\n"
end

function result(a, b, coll)
    text = text..a.." hit "..b.."resulting with "..coll:getNormal().."\n"
end
But From where come the parameter a,b and coll? i cant see them anywhere in the whole script, so it confuses me.

That u dont have to check the site again, i write the full code from the tutorial here:

Code: Select all

function love.load()
world = love.physics.newWorld(-800,-600, 800,600)
    world:setGravity(0,20)
    world:setCallbacks(add, persist, rem, result)
    ball = {}
        ball.b = love.physics.newBody(world, 400,200, 10,0)
        ball.s = love.physics.newCircleShape(ball.b, 0,0, 50)
        ball.s:setData("Ball")
    static = {}
        static.b = love.physics.newBody(world, 400,400, 0,0)
        static.s = love.physics.newRectangleShape(static.b, -100,-25, 200,50, 0)
        static.s:setData("Block")
end

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

function love.draw()
    love.graphics.circle("line", ball.b:getX(),ball.b:getY(), ball.s:getRadius())
    local x1,y1, _,_, x3,y3, _,_ = static.s:getBoundingBox()
    local w = x3-x1
    local h = y3-y1
    love.graphics.rectangle("line",
        static.b:getX()-w/2,static.b:getY(),
        w,h, 0)
    love.graphics.print(text,0,12)
end

--Refer to http://love2d.org/wiki/Contact for more information on collision objects
--'coll' is an object created by the collision
--'a' is the first object in the collision and 'b' is the second

text = ""

function add(a, b, coll)
    text = text..a.." collding with "..b.." at an angle of "..coll:getNormal().."\n"
end

function persist(a, b, coll)
    text = text..a.." touching "..b.."\n"
end

function rem(a, b, coll)
    text = text..a.." uncolliding "..b.."\n"
end

function result(a, b, coll)
    text = text..a.." hit "..b.."resulting with "..coll:getNormal().."\n"
end
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: A Little Löve Game

Post by tentus »

Matzexxxxx wrote: But From where come the parameter a,b and coll? i cant see them anywhere in the whole script, so it confuses me.
They're built into World:setCallbacks(). The parameters aren't explicitly mentioned in the wiki but they're there.
Kurosuke needs beta testers
User avatar
felix24
Party member
Posts: 163
Joined: Tue Jul 26, 2011 4:51 pm
Contact:

Re: A Little Löve Game

Post by felix24 »

a, b and coll are just required parameters for those functions. a and b represent the data of the two shapes colliding. coll is a collision object that is generated with each collision. in order to set what data is passed into the functions via a and b, note the following line in the example:

Code: Select all

ball.s:setData("Ball")
whenever ball.s collides with another shape, the string "Ball" is passed into add, persist and remove. so when the example says:

Code: Select all

text = text..a.." collding with "..b.." at an angle of "..coll:getNormal().."\n"


a and b are the strings that were defined as the shape's data in love.load(). you can set the data as anything you want. it doesn't have to be a string.
Matzexxxxx
Prole
Posts: 7
Joined: Tue Mar 06, 2012 6:20 pm

Re: A Little Löve Game

Post by Matzexxxxx »

Ahh, i understand now.
The difficult part was for me to understand that the Data "block" will be sent to add when it collides with something.
So, if i want that on the collision a body will be destroyed, can i write:

Code: Select all

function add(a, b, coll)
objects.Enemy.b:destroy()

end
It it this object:

Code: Select all

  objects.Enemy = {}
  objects.Enemy.b = love.physics.newBody(world, 200,200,0,0)
  objects.Enemy.s = love.physics.newRectangleShape(objects.Enemy.b, 0, 0 , 5,5, 0)
  objects.Enemy.s:setData("Gegner")
but when i do so, my programm has a error and will be closed, if i destroy the shape too, it dont helps.
Is it correct or what is with it?
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests