[update,but not working]sun and earth,use middleclass

Show off your games, demos and other (playable) creations.
Post Reply
xss6985
Prole
Posts: 12
Joined: Wed Jun 15, 2011 5:22 am

[update,but not working]sun and earth,use middleclass

Post by xss6985 »

thank you for your suggest.now I changed the code,make the sun is an instance of Star,but it does not working.
the new code is in sun2.love
in sun3.love ,I use another oo lib,but there is also some wrong.
Attachments
sun3.love
use anoter oo lib
(1.14 KiB) Downloaded 812 times
sun2.love
(2.79 KiB) Downloaded 297 times
sun.love
(2.78 KiB) Downloaded 326 times
Last edited by xss6985 on Sat Oct 19, 2013 11:15 am, edited 2 times in total.
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: sun and earth,use middleclass

Post by Germanunkol »

Nice little demo!
Do you have any plans for it, or was it just for testing out middleclass?
As a side note: I found your usage of middleclass a little... unconventional. You're creating an Object called "star", but the only instance of it is "Earth" (correct me if I'm wrong). This seems weird, because "Earth" is not a "star", and the star you draw is not an object, just a circle. So you're only ever using one class and one object... using a class-library seems overkill for it.

Instead, for an application like this, I would create an Object called "Body" (which can be a star or planet or moon or whatever) and then create a sub-class (child-class, which inherits from "Body") called "Orbiter" or similar. The initializing function of the Orbiter would take another object as a parameter, which it orbits around, and has a "update" or "move" function which will make it move further around this object. Other than that, Orbiter and Body classes would be the same.

Once that's set up, you should easily be able to do:

-- in love.load:
star = Body:new()
planet1 = Orbiter:new(star)
planet2 = Orbiter:new(star)
moon = Orbiter:new(planet1)

-- in love.update:
planet1:update(dt)
planet2:update(dt)
moon:update(dt)

-- in love.draw:
star:draw()
planet1:draw()
planet2:draw()
moon:draw()

To convince you, here's some benefits:
  • Theoretically, you can easily implement any number of planets, moons, asteroids etc using this function. You could have stars orbiting each other, or asteroids orbiting asteroids.
  • Your main file would be very, very clean.
  • You would be using inheritance of middleclass - and thus learning about one of the strongest mechanisms of object-oriented programming
  • It would be easy to implement different orbit-speeds, sizes, orbit-radii etc for the objects (just pass them to the initializing function and use them in the update function)
  • You can move your sun to any place on the map/screen and the orbiting objects would just follow along.
I guess that would be my approach.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
Kyle
Party member
Posts: 146
Joined: Sat Mar 16, 2013 9:46 pm

Re: sun and earth,use middleclass

Post by Kyle »

I personally have Entity objects with a node-tree sort of thing where each one has a parent and a variable number of children. They all have an "orbit" member, which takes an Orbit object. Each update, it calls its own Orbit:Update(dt) and Orbit:Apply(self).
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: sun and earth,use middleclass

Post by kikito »

This is a nice start. Here are some constructive comments:
  • Get rid of the 's' global variable and use 'dt' instead: a=dt/180*math.pi. You might need to increase the speed of the planet after this change.
  • By the way, a should be a local variable: local a=dt/180*math.pi
  • Adding more planets with different sizes, colors and orbits will make it look better.
  • As others have said, the Earth should not be an instance of a class called 'Star', since we al know it is not. 'Planet' would have been a better name for that class. But if you want to use it also for the Sun and maybe the Moon, I'd use 'CelestialBody' or 'AstronomicalBody', which encompasses stars, planets and moons, as well as other things like comets and asteroids.
  • You should definitively build the sun the same way you have built the Earth - as an instance of a class.
Finally, if you fill adventurous, I would suggest adding a method called ':addChild' to your Celestial/AstronomicalBody class, so you can say things like 'The Earth orbits around the Sun / the Moon orbits around the Earth). This would require having a list of children inside each astronomical body (initially empty). If you modify the :update method to also update the children, you can just call Sun:update(dt) and that will update the Earth, the planets and their moons, as well as other asteroids or comets, if you choose to include them. You might have to do some math for the orbits.
When I write def I mean function.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [update,but not working]sun and earth,use middleclass

Post by kikito »

Hi again, I'm adding a new post instead of editing my last one so you can see this answer more easily.

There were several problems in star2. I've fixed those problems on the attached one.
  • The indentation was lost at places. Always try to keep the indentation right, it helps finding bugs.
  • I made color part of the constructor, so you don't need to write two lines to build a star.
  • You were using 'Star' instead of 'self' in your methods. So instead of self.x or self.father, you were saying Star.x or Star.father. I've fixed that.
  • I have added an 'angle' property (self.angle) and simplified the use of dt that you were doing. I am also using radians instead of degrees (no need to multiply by 180 and divide by math.pi)
  • I have removed the galaxy parent, which was a number, and replaced it with nil. Checking if some star has a parent can now be done with if self.parent then ....
  • I have removed all the global variables and replaced then by local variables. Star is returned by require 'Star', and sun and earth are local vars inside main.lua.
  • Finally, I made some small fixes on the orbit calculation / drawing.
  • Oh, I also added a Moon :)
Attachments
star2-fixed.love
(3.34 KiB) Downloaded 280 times
When I write def I mean function.
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: [update,but not working]sun and earth,use middleclass

Post by Helvecta »

kikito wrote:You were using 'Star' instead of 'self' in your methods. So instead of self.x or self.father, you were saying Star.x or Star.father. I've fixed that.
Just curious, why does it matter if you use self instead of Star? If the function's called Star:update, self will just be Star anyways.. right? :?
"Bump." -CMFIend420
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [update,but not working]sun and earth,use middleclass

Post by Robin »

Helvecta wrote:Just curious, why does it matter if you use self instead of Star? If the function's called Star:update, self will just be Star anyways.. right? :?

Code: Select all

self.x = 300 -- this star's x position is 300
Star.x = 300 -- every star's x position is 300 (except for those where the self.x was changed)
Help us help you: attach a .love.
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: [update,but not working]sun and earth,use middleclass

Post by Helvecta »

Oh, okay, so another less efficient way of doing the same thing as in kikito's .love would be:

(main.lua)

Code: Select all

Star:new("sun", 50,0,0,{255,255,0},nil)
(Star.lua)

Code: Select all

Star:initialize(def,r2,r1,speed,color,father)
Star[def] = {}
Star[def].r1 = r1 --orbit radius
...
?
"Bump." -CMFIend420
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [update,but not working]sun and earth,use middleclass

Post by kikito »

Helvecta wrote:Oh, okay, so another less efficient way of doing the same thing as in kikito's .love would be:

(main.lua)

Code: Select all

Star:new("sun", 50,0,0,{255,255,0},nil)
(Star.lua)

Code: Select all

Star:initialize(def,r2,r1,speed,color,father)
Star[def] = {}
Star[def].r1 = r1 --orbit radius
...
?
Your way might work in some cases, depending on what you write and how careful you are. But it would be like using a knife to drive screws instead of using a proper screwdriver. It will easily break later on for circumstances you still don't understand yet (if you are curious: inheritance will not work, and you will not be able to use polymorphic methods).

I strongly recommend you to simply use self instead, as Robin and I advised.
When I write def I mean function.
xss6985
Prole
Posts: 12
Joined: Wed Jun 15, 2011 5:22 am

Re: [update,but not working]sun and earth,use middleclass

Post by xss6985 »

thank you for helping fixed my code. It seems that I have a lot to learn about lua and middleclass.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests