Help improve my code ? (HardonCollider & Middleclass inside)

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.
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Help improve my code ? (HardonCollider & Middleclass inside)

Post by Lynesth »

Hi people, I just want to show you something I've started (it'll help me make a game later, but for now I'm just playin' around with stuff and all).

I'd like you, if you want to, to check it out and tell me want you think, what could be improved, etc...
I know I'll have to use classes later for all my objects but I don't really know how to right know (haven't check how it works with middleclass yet).

I'm using HardonCollider (if you have an idea to improve what I did with it, like the 2 collider:update(dt)) to manage collisions.

You can use arrows to move the player (the circle with Technocat's Falldown's ball.png :p)

Anyway, thanks in advance and excuse my poor english ;)
Attachments
mystuff.love
(24.12 KiB) Downloaded 193 times
Last edited by Lynesth on Mon Jun 04, 2012 3:22 am, edited 1 time in total.
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Help improving my code ?

Post by Roland_Yonaba »

Lynesth wrote: I'd like you, if you want to, to check it out and tell me want you think, what could be improved, etc...
Fair enough.
Lynesth wrote: I'm using HardonCollider (if you have an idea to improve what I did with it, like the 2 collider:update(dt)) to manage collisions.
As I'm not familiar with HardonCollider, I may say silly things. Someone will hopefully correct me.
Well, first of all, why do you *have* to update your collider twice ? What's the unexpected behaviour that result when calling it once ?
Or I may have missed something ?

Lynesth wrote: I know I'll have to use classes later for all my objects but I don't really know how to right know (haven't check how it works with middleclass yet).
Skimming through your code, I think it wouldn't be hard to refactor your code to OOP. It'll just need some organization.
You can create a class for the player, a class for all others shapes that will interact with the player. Maybe a World class to register everything inside and provide shorcut callbacks to update all entities in that world.
Moving this to middleClass woudn't be that complicated, the readme tells everything you need to know by itself.
Or, you might want to try Lua Class System... :crazy:
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Help improving my code ?

Post by Lynesth »

Thanks for your answers.

When I talk about class, I was in fact talking about making a class for world objects, dynamic objects and player. So yeah I might give it a shot sometime soon.

Concerning HardonCollider collisions, it ends up pretty weird when the player stands on a dynamic (like the box in my example) if I don't update it twice. Like it's resolving only one collision per update which isn't enough when there's too much collisions going on. Or maybe that could be my computer... I don't know really. Maybe that Vrld will answer me on that specific point :)

Thanks again for your time, I'll get a look at LCS, I promise ;)
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Help improving my code ?

Post by Roland_Yonaba »

Well, this does not seems to be related to the library....According to what I can understand from that exceprt, HC internally iterates through all registered shapes, resolving untested collisions in a single loop.

Code: Select all

--Excerpt from HardOnCollider/init.lua
function HC:update(dt)
	-- collect colliding shapes
	local tested, colliding = {}, {}
	for shape in self:activeShapes() do
		for other in shape:neighbors() do
			local id = collision_id(self, shape,other)
			if not tested[id] then
				if not (self._ghost_shapes[other] or self:share_group(shape, other)) then
					local collide, sx,sy = shape:collidesWith(other)
					if collide then
						colliding[id] = {shape, other, sx, sy}
					end
					tested[id] = true
				end
			end
		end
	end

	-- call colliding callbacks on colliding shapes
	for id,info in pairs(colliding) do
		self._colliding_last_frame[id] = nil
		self.on_collide( dt, unpack(info) )
	end

	-- call stop callback on shapes that do not collide anymore
	for _,info in pairs(self._colliding_last_frame) do
		self.on_stop( dt, unpack(info) )
	end

	self._colliding_last_frame = colliding
end
The problem may be related to your code.
Well, testing it again, I removed the second update call, and yet it runs fine.
What's exactly the weird behaviour you are tallking about ?
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Help improving my code ?

Post by Lynesth »

After checking out, I realised that this glitch would not happen anymore. So well, I don't know what caused that really but yeah, it could have been something in my code that sucked and that I fixed without noticing.
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Help improving my code ?

Post by Qcode »

I, also haven't used Hardon collider, but have you considered having the falling speed be set to an increasing amount, and then your Y can be added or subtracted with that amount? That way any of your items would have more realistic falling, rather than the set speed that they have now.
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Help improving my code ?

Post by Lynesth »

Qcode wrote:I, also haven't used Hardon collider, but have you considered having the falling speed be set to an increasing amount, and then your Y can be added or subtracted with that amount? That way any of your items would have more realistic falling, rather than the set speed that they have now.
That's what I am already doing actually. I just set a max value to that y velocity. But it does increase with time. Or I didn't understand what you told me :p
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Help improving my code ?

Post by Qcode »

Oh it just seemed like quite a slow value for it to be falling at, In my opinion.
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Help improving my code ?

Post by Lynesth »

Qcode wrote:Oh it just seemed like quite a slow value for it to be falling at, In my opinion.
I may adjust this later.

Anyway, I'm trying to make my code a bit more concised (is that the word ?) by using Middleclass (sorry Roland ^^) but I'm getting an error, about trying to index a field a nil value or whatever that could mean. I tried many things but didn't get it to work. You'll find my .love attached but I'm not sure you'd need it.

Here's what's in my main.lua :

Code: Select all

love.load()

    require("middleclass")
    require("dynamics")
    circle = CircleDynamic(400, 100, 25)
    box = RectDynamic(500, 300, 50, 50)
    player = CircleDynamic(100, 100, 20, true, 'ball.png')
    ...
end

love.update(dt)

    player:movement(dt)
    circle:movement(dt)
    box:movement(dt)
    ...
end
And here is my dynamics.lua :

Code: Select all

Dynamic = class('Dynamic')
function Dynamic:initialize(shape, x, y, image)
    self.shape = shape
    if image then self.image = love.graphics.newImage(image) end
    self.x = x
    self.y = y
    self.velocity = {x=0, y=0, a=0}
    self.mtv = {x=0, y=0}
    self.onGround = false
    self.onBox = false
    self.boxVelocity = {x=0, y=0}
    self.isJumping = false
    self.typ = 'dynamic'
end


function Dynamic:movement(dt) -- Function to move dynamic and player objects

    if self.form == 'circle' then
        if self.onGround or self.onBox then -- If a circle is on ground or on another dynamic,     makes it rotate
            self.velocity.a = self.velocity.x * (0.5-dt)
        end
    end

    if not self.onGround and not self.onBox then
        self.velocity.x = self.velocity.x * (1-dt) -- In air friction
    else
        --self.velocity.x = self.velocity.x * (1-dt) -- Slippery ground friction
        self.velocity.x = self.velocity.x * (1-dt*10) -- Normal friction
        self.velocity.y = 0
    end

    self.velocity.y = self.velocity.y + (1000 * dt) -- Gravity

    if self.velocity.y > 500 then -- Set max velocity down
        self.velocity.y = 500
    end
    if self.velocity.x > 0 and self.velocity.x < 1 then -- Useless ?
        self.velocity.x = 0
    end
    if self.velocity.x > -1 and self.velocity.x < 0 then -- Useless ?
        self.velocity.x = 0
    end

    if self.typ == 'player' then -- Makes player move with keyboard
        if love.keyboard.isDown('right') then -- Makes self move right
            self.velocity.x = 200
        end
        if love.keyboard.isDown('left') then -- Makes self move left
            self.velocity.x = -200
        end
    end

    self.x, self.y = self:center() -- Get self current position

    if self.y >= 600 then -- Makes self unable to go offscreen
        self:moveTo(self.x, 0)
    end
    if self.x > 800 then -- Makes self unable to go offscreen
        self:moveTo(0, self.y)
    elseif self.x < 0 then -- Makes self unable to go offscreen
        self:moveTo(800, self.y)
    end

    self:move((self.velocity.x + self.boxVelocity.x) * dt, self.velocity.y * dt) -- Moves

    if self.form == 'circle' then
        self:rotate((self.velocity.a/(20*20*math.pi))*dt*100) -- Rotates the self if it's a circle
    end

end

CircleDynamic = class('CircleDynamic', Dynamic)

function CircleDynamic:initialize(x, y, r, player, image)
    shape = collider:addCircle(x, y, r)
    Dynamic.initialize(shape, x, y, image)
    if player then self.typ = 'player' end
    self.form = 'circle'
end

RectDynamic = class('RectDynamic', Dynamic)

function RectDynamic:initialize(x, y, sx, sy, player, image)
    shape = collider:addRectangle(x, y, sx, sy)
    Dynamic.initialize(shape, x, y, image)
    if player then self.typ = 'player' end
    self.form = 'rectangle'
end
Here is the error I get : "Attempt to index field 'velocity' (a nil value)"
Related to this line :

Code: Select all

self.velocity.x = self.velocity.x * (1-dt) -- In air friction
in Dynamic:movement(dt) function in dynamics.lua.


Thanks in advance for any help I'll get ;)
Attachments
tagada.love
(26.57 KiB) Downloaded 155 times
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Help improving my code ?

Post by Lynesth »

Double posting to let you guys know that I found what I did wrong...

I messed up things between dots and colon.... Thanks anyway :p

I know have another issue. It is actually possible to do something like that :

Code: Select all

myvar = a
myvar.x = 0
myvar.y = 0
But using Middleclass, how can I achieve the same thing ?
If I do something like that :

Code: Select all

function MyUbberVariable:initialize(a, x, y)
    self = a
    self.x = x
    self.y = y
end


---Calling it that way in love.load() :

myvar = MyUbberVariable(a, x, y)
It just doesn't work. Any idea ?


If it doesn't work then I have another question. Suppose I have this class :

Code: Select all

function MyUbberVariable:initialize(a, x, y)
    self.a = a
    self.x = x
    self.y = y
end
I call it like this then : "myvar = MyUbberVariable(a, x, y)". Suppose then that I know that "somevar = myvar.a". How can I retrieve "myvar.x" from "somevar" ?

Thanks again for your answers.
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests