It is currently Thu Dec 05, 2013 10:48 am

 All times are UTC

 Page 1 of 1 [ 10 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Object Design for space gamePosted: Sun Nov 13, 2011 3:35 am
 Rookie

Joined: Tue Nov 01, 2011 1:07 am
Posts: 3
I'm a hobby programmer and release my games for free to the public. Unfortunatly I lack the OOP design skills a professional would have - perhaps you can help?

Okay lets make a hypothetical set of requirements for a space game

1. The following entities will exist - spaceship, rock, fuel, missile, alienship, sun, black hole
2. The rock can be blasted into smaller rocks
3. The 'fuel' is just like a rock but the space craft can 'tractor beam' it to get more fuel
4. The sun and black hole has a large gravitational field.
5. You die when you hit the sun but you teleport to a random place when you hit the wormhole

Now the proper gravity law will be applied to all objects. Meaning I would like to see spaceship, rock, fuel, missile etc all able to 'slingshot' around the sun/wormhole.

Now at the moment I have the following code (and it's really annoying me!)

- For each spaceship calculate the gravity to each rock, fuel, missile, sun, wormhole, alienship etc and move spaceship
- For each rock calculate the gravity to each spaceship, fuel, missile, sun, workhole, alienship etc and move the rock
- For each missile calulate the gravity... etc etc
- For each sun ..
- For each wormhole
- For each alienship...
etc etc

Now i'm thinking there has to be a easier way to handle the gravity side of things for all these different objects around the place and i'm thinking its a problem in the way ive designed my objects.

How would you design the above entities given then rules i provided to make gravity calulations easier? Aka condense all the 'for each X calculate the gravity of Y' etc

Really I want to be able to have ONE gravity check like so

- For each object that is effected by gravity, calculate to every other object effaced by gravity

Top

 Post subject: Re: Object Design for space gamePosted: Sun Nov 13, 2011 10:44 am
 Obfuscation Ogre

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 298
Location: Here (where else?)
Make a GameObject base class (or super class, or whatever they call it in .net), and store all GameObjects in a list should allow for
Code:
for obj1 in game_objects:
for obj2 in game_objects:
if obj1 == obj2: continue  // skip self
// compute interaction between obj1 and obj2

Wouldn't that work?

Top

 Post subject: Re: Object Design for space gamePosted: Sun Nov 13, 2011 11:07 am
 Rookie

Joined: Tue Nov 01, 2011 1:07 am
Posts: 3
So is there a way to group different objects together in a list? And when you need to, access their properties?

Top

 Post subject: Re: Object Design for space gamePosted: Sun Nov 13, 2011 12:43 pm
 Obfuscation Ogre

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 298
Location: Here (where else?)
I never programmed .NET, but you need to make a list of GameObject references, at least that's how it works in C++ and Java.
As for property access, properties for gravity (probably mass and velocity in the plane) are bes stored in GameObject, so you can access them easily. Otherwise you need a (virtual) method to pull them out of the object.

Top

 Post subject: Re: Object Design for space gamePosted: Sun Nov 13, 2011 4:48 pm
 Dexterous Droid

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3750
Location: South Africa
Quote:
How would you design the above entities given then rules i provided to make gravity calulations easier? Aka condense all the 'for each X calculate the gravity of Y' etc

Really I want to be able to have ONE gravity check like so

I agree with Alberth about the double for-loop and creating a GameObject base class for all game objects. Just two things to keep in mind:

1) there's no point in calculating gravity between ships and rocks, as they'll exert close to 0 gravity on each other. You only need to worry about the massive objects like suns and planets.
2) consider keeping references to the massive objects in a separate array massiveObjects. Then you can do the calculations much more easily and avoid having to loop through N² objects each time you calculate gravity. (Where N is the number of game objects you have)

Quote:
So is there a way to group different objects together in a list? And when you need to, access their properties?

Have you used inheritance / polymorphism before?

Polymorphism basically means you can treat every object that inherits from GameObject as a GameObject, ignoring any other functionality it might have and only using the functionality you know a GameObject has. That means if you keep mass, velocity and position in the GameObject base class, then you can loop through all the ships, rocks, suns and everything else and access the mass, velocity and position without knowing if it's a ship/rock/sun. I'll try explain better if you don't get it

_________________
Whatever the mind can conceive and believe, it can achieve

Top

 Post subject: Re: Object Design for space gamePosted: Sun Nov 13, 2011 10:13 pm
 Rookie

Joined: Tue Nov 01, 2011 1:07 am
Posts: 3
IGTHORN wrote:
2) consider keeping references to the massive objects in a separate array massiveObjects. Then you can do the calculations much more easily and avoid having to loop through N² objects each time you calculate gravity. (Where N is the number of game objects you have)

Unfortunately the game is more of a physics simulation with a game added on top. Each object has a size and density and the mass is calculated from that. It's possible to use the tractor beam to drag a low density "rock" (think of it as a cloud) into another low density rock and have them combine.

It's also possible to shoot high density rocks and have them explode. The total mass of the universe never changes. For example a 10 mass rock may split into a set of 2, 5 and 3 mass rocks. I've actually seen planets form from bunches of rocks collecting together (there is a proceedure that runs that checks to see if rocks have pooled together for a long time and joins them). I've also seen 'ring' planets form with a belt of stuff around them - some unexpected surprises to this dynamic nature.

I do run into two problems though if you could share your thoughts

- The tractor beam at the moment is just a line of fixed length. I would like to make it a 'chain' that drags behind a ship. Aka if you turn i want it to sling shot around you. I've been reading up on spring physics and hope this can do it however if there is a easier way to do this that you can think of please let me know.

- In space things rotate. I have not been able to find any sources (that I can understand) that describe how rotation starts in a object due to gravity. If anyone has any idea how this could be done well (even if its 'fudged') please let me know.

IGTHORN wrote:
2)Have you used inheritance / polymorphism before?

Polymorphism basically means you can treat every object that inherits from GameObject as a GameObject, ignoring any other functionality it might have and only using the functionality you know a GameObject has. That means if you keep mass, velocity and position in the GameObject base class, then you can loop through all the ships, rocks, suns and everything else and access the mass, velocity and position without knowing if it's a ship/rock/sun. I'll try explain better if you don't get it

Wow this is exactly what I need to do. I'm aware of inheritance and use it a lot (it seams to save duplicating code) unfortunately I don't have the knowledge to apply polymorphism. I'll read up on what your describing some more.

If you have the time to give a VB.net or C# example I would be most appreciative. Thank you.

Top

 Post subject: Re: Object Design for space gamePosted: Mon Nov 14, 2011 8:55 am
 Dexterous Droid

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3750
Location: South Africa
Quote:
If you have the time to give a VB.net or C# example I would be most appreciative. Thank you.

Say you have a hierarchy like this:

Then you would have classes looking like this, in C#
Code:
class MovableSprite
{
public float x, y;
public float density;
public float size;
public float velocityX;
public float velocityY;

public void applyMutualGravity(MovableSprite otherBody)
{
float force = density * size * otherBody.density * otherBody.size /
(float)Math.Sqrt((x - otherBody.x) * (x - otherBody.x) + (y - otherBody.y) * (y - otherBody.y));
//etc..
}
}

{
public int lives;
}

//inside Program.cs
private List<MovableSprite> sprites = new List<MovableSprite>();

private void run()
{
for (int i = 0; i < sprites.Count - 1; i++)
{
for (int j = i + 1; j < sprites.Count; j++)
{
sprites[i].applyMutualGravity(sprites[j]);
}
}
}

Not sure about the tractor beam thing, but a spring sounds like a reasonable way to do that.

I think rotation starts due to gravitational tidal forces. If two objects pass each other, the two nearest sides are most closely attracted, and as they move away, that makes them start spinning. There's a reasonable chance I'm wrong about this, hopefully someone else pitches in to confirm / correct me.

edit: Seems I'm wrong about the tidal forces. It comes down to conservation of angular momentum and the way planets and suns are formed. http://www.physicsforums.com/showthread.php?t=163047

_________________
Whatever the mind can conceive and believe, it can achieve

Top

 Post subject: Re: Object Design for space gamePosted: Mon Nov 14, 2011 9:18 am
 Bibliotherapist

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 6868
Location: Wilts, Englandshire
IGTHORN wrote:
I think rotation starts due to gravitational tidal forces. If two objects pass each other, the two nearest sides are most closely attracted, and as they move away, that makes them start spinning. There's a reasonable chance I'm wrong about this, hopefully someone else pitches in to confirm / correct me.

I was thinking about the rotation thing yesterday. I only came up with two situations:

1. Two objects strike each other. A glancing blow will result in the objects 'rolling' off each other and tumbling along their new course.

2. Transfer of inertia. If an object is 'captured' by the gravity of another object, it will enter orbit around it. Once the captured object becomes part of the parent, the inertia is preserved as rotation of the overall mass.

I have no idea how the maths for the above would work, I just had some ideas and thought I'd throw them in the mix.

I'm still thinking about the tidal idea. I like it and it sounds plausible, but I'm not sure if it would have enough effect to induce rotation.

_________________
20 GOTO 10

Top

 Post subject: Re: Object Design for space gamePosted: Tue Dec 20, 2011 5:06 am
 Grand Optimizer

Joined: Mon Jan 17, 2005 6:01 pm
Posts: 356
Quote:
- For each rock calculate the gravity to each spaceship, fuel, missile, sun, workhole, alienship etc and move the rock

The spaceships are exerting gravity on the rocks? That doesn't make sense to me... *scratches head*

Let's think logically about space physics - start at the top and work our way down -which object has the most mass?

The black hole, of course.
Then, the sun
then the rocks, assuming they're huge

IMO, spaceships/"fuel"/missiles/ should not exert gravity as they just dont have the mass, and i would argue that 'wormholes' have no mass at all and therefore no gravity.

So if you want a really dynamic and "living" model, you could step through your physics logic in order:

Black hole in the middle, exerting gravity on the sun. sun orbiting black hole
Sun exerting gravity on the rocks, causing them to orbit the sun
rocks exerting gravity on everything else that's smaller.

Assuming theres enough distance between your celestial objects, and your math/physics calculations are sound, what youl end up with is essentially a living-breathing solar system. You know, minus the black hole. Naturally more than one gravitational pull could apply to any given small object, and so youd have to calculate which pull is stronger based on distance/mass of celestial object.. etc.

Sounds complicated but if you work it out logically and code it in a modular fashion, it should work out fine. If i wasn't on my way to bed and half asleep, id write ya some pseudocode. Maybe tomorrow hehe. hope this helps.

_________________
"None are more hopelessly enslaved than those who falsely believe they are free."
"It is no measure of health to be well adjusted to a profoundly sick society."
"Hope is the first step on the road to dissapointment." -Jonah Orion
http://tankzgame.blogspot.com

Top

 Post subject: Re: Object Design for space gamePosted: Fri Dec 23, 2011 10:35 am
 Bibliotherapist

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 6868
Location: Wilts, Englandshire
As I understand it, every object has a gravitational effect. Some things are too small to exert a meaningful influence, but it is still there. When you drop a ball, the Earth's gravity pulls the ball down, but on a minute scale, the ball pulls also the Earth up. However, for the OP's requirement, most small objects' gravity can be ignored.

I would approach this by setting up a grid of vectors, the density of the grid depends on how much work you want to do and how accurate the simulation needs to be. Each object's gravity field can then be applied in turn to the grid until you have a 'map' of the gravity on the whole playfield. When adjusting your object's velocity, you can simply look at the grid to determine the overall gravitional effect at any given point.

_________________
20 GOTO 10

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 10 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ Forums    Forum Rules and Posting Guidelines Wiki Discussion    Help    Content Issues Game Programming Discussion    C and C++ Game Programming    Java Game Programming    Language Agnostic Programming    .NET Game Programming    VB Game Programming    Mobile Game Programming    Web-Based Game Programming    Other Languages    OpenGL Development    Direct X Development Game Development Discussion    Game Design    Game Media Off-Topic Discussion    Announcements    Off-Topic    Community Projects    News