Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Space Game, combination tutorial / help needed

Author
Message
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 9th Jun 2009 01:16 Edited at: 9th Jun 2009 01:18
Hello all, I have just begun ( quite literally ) writing a two dimensional space adventure game.

I know I'll need help with it, but I also think that some of the methods I use will be helpful to those who are just starting out.

Therefore, if in accordance with the forum rules, I am going to slowly fill this thread with my progress on the game and include all the code used. Feel free to use any of the code, and feel especially free to criticize or suggest improvements. I think that this would be a great way for everyone to learn a bit ^^.

I would have posted this in the WIP forums, but since I want to start this from almost the very beginning, I decided it would fit better here. Once the game is sufficiently well on it's way, I will ask the thread to be moved, or create a new thread in the WIP forum.

Anyway, I'll get this started with what I have so far. Again, please suggest improvements if you have them.

Here is the basic design doc so everyone has an idea of what I'm going for.

Quote: "
Game Design Doc for Project Eres


Tags:

2d, Space Opera, Freeform, Newtonian, Exploration, Battles, Trading, Conquest

Premise:

You shove off into space in a brand new [rookie ship] with nothing but the clothes on your back and a few credits. You are free to do as you choose, become a lethal pirate, peaceful trader, smuggler, or military captain, grunt, or try them all!

Definite Features:

- Newtonian physics, you continue moving in the direction you started, frictionless movement and torque. Weapons should have recoil, except for light based laser weapons.

- Large explorable universe, everything should be at realistic distances from eachother. Several methods of faster-than-light travel should be available to minimize travelling time.

- Time compression for long journeys

- Convincing AI. There should be a number of different sub-AI's per each main type. e.g. not all smugglers should have the exact same programming. On a side note, smugglers should not be advertised as such.

- NO ship to ship collisions, as it is a 2d game and collisions would be far more frequent than would be realistic. The same goes for any sort of space debris.

- Player should be able to modify his vessel with different equipment. Each ship should have a number of 'hardpoints' for different types of equipment, similar to EVE-Online.

- NPC ships should be UNIQUE, all NPC's should have a name and a variety of different fittings for their ships. AKA if Ship Chassis 'Raven' is fitted with two blasters and a missile launcher by default, some NPC's should have 3 blasters and no missile launcher, some should have the default, and some should have all missile launchers.

- If the player dies, he is dead for good. Saving is enabled, but the save file will be deleted if the player is killed. Notice that being killed is different than simply losing your ship.

- Player should be able to eject from his ship to save himself. However, certain opponents may 'pod-kill' the player's escape pod, permanately killing him.

- Player should be able to hire escorts, give them orders, and even tell them to travel long distances alone.

- It should be possible to loot or capture disabled ships, or destroy them and all crew. Upon the successful capture of a ship, options will be given to either add it to your fleet or take command of it yourself. Should the player choose to take command, his previous ship will be added to fleet.

- Player can store items in hangers at various stations, and pay NPCs to transport them. The NPCs might get attacked by pirates, so player should also be able to pay for protection or protect them himself.

- Wormholes / Blackholes should exist. If the player enters or is drawn into one, results should vary. He may be transported to a specific point on the map, destroyed, damaged, or any number of wierd outcomes. Parallel dimensions where everything is different? An entirely different universe?

Possible Features:

- Support for boarding ships?

- Randomly generated universe?

- Moving planets?

- Destructible planets / stations?

- Exiting ship to walk around on ground?
"


Now that you have the basic idea, here is all the code I have so far. It made up of 8 source files so far.

Here is our Main.cpp


Here is my BasicClasses.h file, which has some objects that the game will use frequently.



Next is our Utilities.h file. It's mostly empty right now.



Now the SpaceshipClass.h. This class will have a lot of work done on it, but this is some of the stuff I threw into it for now.



The Setup.h file, which I use to initialize things.



The Graphics.h file, which I'll use to draw things.


Collision.h has some simple functions that I use in a lot of projects. It's not perfect but it works ok.



GameMechanics.h will grow quite a bit as well. Right now it just calls the Update routine on our test ship.



To compile you'll need a 'TestShip.png" file. I've attached the image for your use, it's just a random spaceship sprite I found on google. Once the engine comes along more I'll make real spaceships that look nicer.



And that's all of it! If you compile, you'll get a spaceship sitting in the center of the screen, which doesn't move, or do much of anything really. Hopefully we'll get it moving in the next post.

I look forward to seeing your opinions about my coding style and the like ^^.

~Phos

Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 9th Jun 2009 02:14
I've made a bit of progress ( that was quick o.0 ). The player can now control the spaceship with the arrow keys, and there is a planet! Also, I've created a 2d camera which allows the game to scroll.

I'm just going to attach a zip file of the project so far instead of putting it all in code tags because it's much easier. That way the media is included as well.

Here is a shot of what we have so far:


By the way, the code is not nearly as commented as it should be, I'm going to fix that now.

Next up, a neat parallax star field effect so that we'll have a frame of reference for movement even when there are no planets or ships nearby.

Attachments

Login to view attachments
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 9th Jun 2009 06:38


This is coming along much faster than I was anticipating. Mayhaps a mod could move me into WIP?

The download for the newest version is attached. More comments have been added, along with a parallax starfield, suns ( yes I know they are the same, but I want stars to be whizzing past like dust, whereas suns will be more landmarkish ), and multiple planets.

Currently 100 suns are positioned randomly, and then 500 planets are distributed amongst them randomly.

Tell me what you think?

Attachments

Login to view attachments
ABXG
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Canada
Posted: 9th Jun 2009 08:27
Pretty nice project you got going there. I would make one suggest, place the planets and set up an orbit for them that is realistic. I would also give each planet and sun a "gravity value" so that you can realistically mimic gravity when floating in space.

------------------------------------
Currently 1500+ lines of code into an "over-the-shoulder" action RPG with combat based on rag-doll physics.
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 9th Jun 2009 09:19
Ooer, that would be nifty. I shall do that! I'll upload more changes within a few hours, or possibly tomorrow.

Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 10th Jun 2009 07:00
Hi guys, having a bit of a problem. I wanted to change the way drawing works, and instead just have a Draw() command within each class.

However, I get an error when using this class:



It gives me an 'Access violation reading location' error and points at the dbSprite command. Am I not allowed to create sprites within a class?

Thanks

kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 10th Jun 2009 07:24 Edited at: 10th Jun 2009 07:25
I think where you have:


It may need to be:


Or get rid of the .x and .y and turn them both into just PosY & PosY

EDIT: Alternitavly inside here:


You could/would declare the int's

Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 10th Jun 2009 07:48
Hmmm, I don't think the Pos.x and Pos.y are the problem. PointObject is a custom class I wrote which is defined as such:



The .x and .y are already declared as floats, so I don't think it'd do much good to re-declare them as ints. I know floats are compatible with sprites as I have used them before, the problem only arose when I added the dbSprite command into the constructor.

kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 10th Jun 2009 08:05
hmm well yea thats not the issue at all...
As for the sprites i use the command in a couple of my classes.

Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 10th Jun 2009 10:31
Weird, oh well I figured out a way to get around it for the time being.

Anyways, at this point we have a basic engine for a 2d space adventure. There is no shooting or even other ships yet, but I think this is where I'll leave off.

I'll be posting the continuation of this game in the WIP board for those of you who are interested, but I don't think I'll continue to supply full source code, as I feel this is gathering more potential than I had anticipated.

For the final open-source version however, you can fly around in the galaxy, there are multiple solar systems, each with orbiting planets around a sun.

There is a radar which you can toggle between solar system mode and galaxy mode with '1' and '2', to help you find your way around. The radar is also draggable and resizeable! Neat huh?

Full source is attached, feel free to use it in any way you please, though I'd appreciate credit and possibly notification just so I know where my code ends up ^^. Also please contact me if you use it in anything commercial ( Not because I want your moneys just because it'd be nifty to know :p )

Hope this was helpful to someone



Attachments

Login to view attachments
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 10th Jun 2009 11:25
I'll more than likely be taking a look at the code you used to resize the radar =O

SunDawg
19
Years of Service
User Offline
Joined: 21st Dec 2004
Location: Massachusetts
Posted: 10th Jun 2009 13:43
Just a note about gravity: The force of attraction between two bodies is found by (Force)=(Gravitational Constant)*(Mass of one object)*(Mass of another object)*(Distance Between them)^-2.
The gravitational constant is about 6.67(10)^-11, and generally referred to as G.

Therefore, because Force=(Mass)*(Acceleration), if you used the mass of your planet or sun in the about equation as one value and the mass of your ship as the other, you could find the force of attraction between them. Then, by dividing that force by the mass of the ship, you'll find the acceleration of the ship toward the planet.

Finally, knowing the acceleration, you could use one of a couple of equations to find the current velocity of your ship. (Velocity)=(Initial Velocity)+(acceleration)*(change in time since last calculation). And with that information, you can find the ship's displacement with the equation (Change in position)=(initial velocity+final velocity)*(change in time since last calculation)/2.

Hope that helps a little!

Final note:
Mass is in kg
Force is in N
Velocity is in m/s
Accel is in m/s^2


My site, for various stuff that I make.
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 10th Jun 2009 18:38
Ooh, thanks ^^. I'll definitely use this. I was going to have to hit up my physics-endowed brother but you have done the job :p

Now my planets can orbit realistically instead of just rotating like clock hands! Also I love the idea of getting dragged towards a sun and melting away. The distance shown in the screenshot would probably be fatal.

I'll have to see how this will affect my framerate. I plan to have close to 100 solar systems, all with their own orbiting bodies, and I want them to all be orbiting all the time, so it may be a bit hard on the CPU. Especially if all spaceships are being attracted to planets/suns as well, I'll have to limit forces of attraction to within each solar system.

ABXG
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Canada
Posted: 10th Jun 2009 21:20
If you wanted to use the above defined gravity equations to simulate the movement of your planets you would have to remember that an object orbiting a celestial body has two components (in 2D space), a force that acts towards the body (gravity) and a tangential force that acts in a direction tangent to the "orbital path".

The inward force is always pulling the object closer to the celestial body while the tangential force is providing enough acceleration as to give the object a constant distance (assuming a "perfect orbit") from the body.

The equations to do this correctly can get fairly complex, so I would suggest that you just program a path for each planet. If however, you wanted to do a more complex simulation of gravity for your orbiting planet I could help you with some of the math.

------------------------------------
Currently 1500+ lines of code into an "over-the-shoulder" action RPG with combat based on rag-doll physics.
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 10th Jun 2009 21:56
Hey quick question, when you say this:

Quote: "And with that information, you can find the ship's displacement with the equation (Change in position)=(initial velocity+final velocity)*(change in time since last calculation)/2."


What does the displacement do?

ABXG
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Canada
Posted: 11th Jun 2009 01:38
The ship's displacememnt would be a vector from a fixed position to it's current position.

For your purpose you could just use a distance, which as the length of the the displacement vector.

* Note, i, j, k are engineering terms. Their values represent a distance along their respective axis. 'i' is the 'x-axis', 'j' is the 'y-axis' and 'k' is the 'z-axis'.

For example, a displacement would be:
(i + j + k)m
ie (1.0i + 2.0j + 3.0k),

The distance would be:
sqrt ( (i^2) + (j^2) + (k^2) )
ie sqrt ( (1.0^2) + (2.0^2) + (3.0^2) )

Because you are working in a 2d environment you would only use the 'i' and 'j' elements.

To actually find a displacement vector you would subtract your objects starting position from it's current position, like this:



------------------------------------
Currently 1500+ lines of code into an "over-the-shoulder" action RPG with combat based on rag-doll physics.
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 11th Jun 2009 01:55
Gotcha, so at the moment I'm using this code to derive the force and acceleration.



GravitationalForce( ) is defined as



Then I use this:



And TestShip.Accelerate( ) goes like this:



Vector.x and Vector.y are simply added to the ship's current position.

This seems to produce a realistic effect, the ship is pulled towards the sun and gets catapulted around it quite quickly. However, it is way faster than I'd like, probably because the ship isn't at a realistic distance from the sun to begin with.

I think I'll either make the distances actually realistic, or just multiply the acceleration by a fraction to slow things down.

Login to post a reply

Server time is: 2024-11-05 23:08:51
Your offset time is: 2024-11-05 23:08:51