Little Help Fellow Programmers ?

Miscellaneous. No spam or advertisements, constructive discussion encouraged.
User avatar
fr0stbyte124
Developer
Posts:727
Joined:Fri Dec 07, 2012 3:39 am
Affiliation:Aye-Aye
Re: Little Help Fellow Programmers ?

Post by fr0stbyte124 » Sun May 05, 2013 6:44 am

3 weeks ought to be plenty of time if your other work doesn't cut into it too much.
If you have a machine you can install XNA on, I would look into that. Since you would only be using a small part of it, there shouldn't be a ton to learn, and it is definitely built for games.

http://www.xnadevelopment.com/tutorials.shtml

You can also set resources to be included in the output folder, so you can then take the project to a machine that doesn't have the framework.
I think that's the case, anyway. I've never used XNA either.

User avatar
Iv121
Vice Admiral
Vice Admiral
Posts:2414
Joined:Fri Dec 07, 2012 3:40 pm
Affiliation:UTN
Location:-> HERE <-

Re: Little Help Fellow Programmers ?

Post by Iv121 » Sun May 05, 2013 6:50 am

Also there might be problems using other libraries and such because I'm not sure what the ministry of education allows to use ...
They're watching ... Image

"I am forbidden tag" -CvN

User avatar
fr0stbyte124
Developer
Posts:727
Joined:Fri Dec 07, 2012 3:39 am
Affiliation:Aye-Aye

Re: Little Help Fellow Programmers ?

Post by fr0stbyte124 » Sun May 05, 2013 7:29 pm

That sounds crazy that they would care, but I don't know how your government works. Anyway, just ask your instructor what you can use. Better yet ask what people have used in the past and if he has any samples of sprite games. Unless you are going above and beyond the call of duty, someone has surely ran into this before.

User avatar
Iv121
Vice Admiral
Vice Admiral
Posts:2414
Joined:Fri Dec 07, 2012 3:40 pm
Affiliation:UTN
Location:-> HERE <-

Re: Little Help Fellow Programmers ?

Post by Iv121 » Sun May 05, 2013 11:35 pm

Well I am pretty much. I mean the closest someone is to what I try to do is a pac-man which is probably not as complicated as my stuff is ... but unlike mine it is mostly operational :[.
I am currently trying to make the graphics work without threads.
They're watching ... Image

"I am forbidden tag" -CvN

User avatar
Iv121
Vice Admiral
Vice Admiral
Posts:2414
Joined:Fri Dec 07, 2012 3:40 pm
Affiliation:UTN
Location:-> HERE <-

Re: Little Help Fellow Programmers ?

Post by Iv121 » Tue May 07, 2013 9:58 am

Alright we have progress and maybe rather dumb question. I made the screen move with the separate tile drawing (storing them onto a buffer and all that stuff) and also made the tanks move around (finally).

The problem I have is actually a bug related to their movement.

Basically in order to move a tank I need to delete its image off the form (redrawing the background) and paint it elsewhere, all made in a timer ticking every 10 milliseconds. In the spirit of "never move the whole thing together", in order to clear the tank I use background.Clone () and take only the specific area behind the tank and redraw it. The problem concerns calculating the location I need to clone. Each time I tried to move a tank out of the bunds of the current screen it crashed on "out of memory" on the clone function. I’m quite certain it is because there is actually no background to clone there ( background now stores only what we can see right ?) . I disallowed the player to move the tank out of the screen but even once I move the screen and the tank is visible again it crashes with the calculations I made (tried several ways) . I need to calculate the location that I need to clone.

That is what we have:
Spoiler:
Image
The big black square is the map we have. The map is a matrix of tiles called map [,]. The map stores the textures and stuff needed to draw the background and does not exist as a bitmap. On the map we have the background - a specific area of the map that is currently visible to the user. That area where you see the map on the screen is called Rectangle screenRec. The screenRec's co-ordinates are static, it has a constant width and height and it starts at (PanelMenu1.Width , 0) . Now on the edge of the background we have a Point called CurrentAnchor. It stores the top left corner of the background IN TILES and not in pixels and moves together with the background (as seen on the blue rectangle).

The location of the moved tank is stored on a rectangle called activeRec. the tank is always drawn on it and its co-ordinates are relative to the beginning of the screen (0,0) and not to the map. I move activeRec in order to redraw the tank. Now I need to use those to find the correct location on the background if I move it. I know I could possibly calculate it but for some reason I can't get it right ! :[ .
They're watching ... Image

"I am forbidden tag" -CvN

User avatar
fr0stbyte124
Developer
Posts:727
Joined:Fri Dec 07, 2012 3:39 am
Affiliation:Aye-Aye

Re: Little Help Fellow Programmers ?

Post by fr0stbyte124 » Tue May 07, 2013 12:49 pm

I don't really get what activeRec is, here. Is it an image buffer? It sort of sounds like it is the tank sprite, but I am not really sure.

If the tank is in world coordinates, then activeRec.position = tank.position - currentAnchor. It's complicated a little by the fact you are using tile coordinates. Do your tanks move in tile increments or pixel increments? What about your map scrolling?

If you need sub-tile resolution, I would recommend using float positions, with 1 = tile width. As long as your tiles are a power of 2 pixels, you won't have to worry about precision errors.

User avatar
fr0stbyte124
Developer
Posts:727
Joined:Fri Dec 07, 2012 3:39 am
Affiliation:Aye-Aye

Re: Little Help Fellow Programmers ?

Post by fr0stbyte124 » Tue May 07, 2013 1:49 pm

Alright, going off what we discussed in the chat box, your tank game is essentially a board game. Turn based, only one piece is moving at any given time, and pieces snap to tiles except while moving. So long as that is the case, your job is simple. I am also assuming they move along adjacent tiles, too.

In that case, give each tank an internal coordinate system in tiles, but make it a float type. While it is in transit, it can have fractional positions. During your form tick() event, redraw the map tile for the tile your tank is currently assigned to and the tiles it is crossing over. Once those are redrawn, recalculate the tanks position based on
(starting position + (current time - movement start time)/tankspeed)
with tank speed being in tiles per time increment.

For drawing, coordinates would just be (TankPosition - ScreenAnchor) * 64, or whatever the resolution of each tile is. You can make your screen anchor floating point as well to get smooth scrolling with the same method.

User avatar
Iv121
Vice Admiral
Vice Admiral
Posts:2414
Joined:Fri Dec 07, 2012 3:40 pm
Affiliation:UTN
Location:-> HERE <-

Re: Little Help Fellow Programmers ?

Post by Iv121 » Thu May 09, 2013 10:26 am

Saw your name online again so I'll leave you this, it's probably the bug that makes you fall asleep on the keyboard though... Anyway as I worked on making my tanks move I figured out that the screen RTS like movement was a result of a bug :[ . The reason it didn't lag when I moved the whole tank layer is because I didn't move the whole tank layer and forgot to reset its numbers. Now I have to make it work similar to the background just unlike the background it doesn't work .

Code: Select all

        private void timerScreen_Tick(object sender, EventArgs e)  // responsible for moving the screen, ticks ever 10 milliseconds
        {
            if (mouseHoverSide[0]) // if direction is up
            {
                currentAnchor.Y ++; // move the map down
                if (currentAnchor.Y > 0) // if out of bounds reset to bounds
                    currentAnchor.Y = 0;

                for (int i = - currentAnchor.X ; i < - currentAnchor.X + ScreenRec.Width / 64; i++)
                {
                    for (int k = -currentAnchor.Y; k < -currentAnchor.Y + ScreenRec.Height / 64; k++) // scan the tiles currently visible on the screen
                    {
                        bgGraphics.DrawImage(textures[map[i, k].Texture],
                                            (i + currentAnchor.X) * 64, (k + currentAnchor.Y) * 64, 64, 64);  // draw the texture of the current tile
                        if (map[i, k].CurrentTank == null) // if no tank on tile
                        {
                            tlGraphics.Clip = new Region(new Rectangle((i + currentAnchor.X) * 64, (k + currentAnchor.Y) * 64, 64, 64)); // clean the tile
                            tlGraphics.Clear(Color.FromArgb(0, 255, 255, 255));
                        }
                        else
                            tlGraphics.DrawImage(textures[map[i, k].CurrentTank.TextureId],
                                                (i + currentAnchor.X) * 64, (k + currentAnchor.Y) * 64, 64, 64); // paint the tank if exists
                    }
                }
                g.DrawImage(background, ScreenRec); // paint the buffers at screen location
                g.DrawImage(tankLayer, ScreenRec);
                g.DrawImage(foreground, ScreenRec);
there is the same piece of code for every other direction. CurrentAnchor is actually a negative value just to let you know, remained from prev calculations and also because in order to make the map go up we need to move it down in reality. I assume the location calculation is correct because the background is drawn correctly.
They're watching ... Image

"I am forbidden tag" -CvN

User avatar
fr0stbyte124
Developer
Posts:727
Joined:Fri Dec 07, 2012 3:39 am
Affiliation:Aye-Aye

Re: Little Help Fellow Programmers ?

Post by fr0stbyte124 » Thu May 09, 2013 1:12 pm

I can't really tell from the code what is or isn't working about it. Can you explain a bit more about what is or isn't happening?

Design-wise, I would put the draw screen code in a new method so you have less copy-pasted stuff.0
Also, store all the lines with arithmetic as constants, like
var tile = map[i, k];
int tileX = (i + currentAnchor.X) * 64;
int tileY = (k + currentAnchor.Y) * 64;

It will make the routine easier to read.

User avatar
Iv121
Vice Admiral
Vice Admiral
Posts:2414
Joined:Fri Dec 07, 2012 3:40 pm
Affiliation:UTN
Location:-> HERE <-

Re: Little Help Fellow Programmers ?

Post by Iv121 » Thu May 09, 2013 10:40 pm

The problem is that the tanks themselves are not drawn on the screen
They're watching ... Image

"I am forbidden tag" -CvN

User avatar
fr0stbyte124
Developer
Posts:727
Joined:Fri Dec 07, 2012 3:39 am
Affiliation:Aye-Aye

Re: Little Help Fellow Programmers ?

Post by fr0stbyte124 » Fri May 10, 2013 3:41 am

Yeah, there's definitely not enough context there to tell where the problem lies. Try drawing only the tank layer, and double check that the numbers it is producing are correct.

User avatar
Iv121
Vice Admiral
Vice Admiral
Posts:2414
Joined:Fri Dec 07, 2012 3:40 pm
Affiliation:UTN
Location:-> HERE <-

Re: Little Help Fellow Programmers ?

Post by Iv121 » Fri May 10, 2013 4:41 am

I know it lacks context, if I had this context the problem would be gone already :[ . I will try a few things out and maybe send you the project file of that stuff. That is this sort of things that delayed my project by 2 months -.- .
They're watching ... Image

"I am forbidden tag" -CvN

User avatar
Iv121
Vice Admiral
Vice Admiral
Posts:2414
Joined:Fri Dec 07, 2012 3:40 pm
Affiliation:UTN
Location:-> HERE <-

Re: Little Help Fellow Programmers ?

Post by Iv121 » Mon May 13, 2013 11:29 am

Progress ! Now the movement and the screen are finally working as intended after solving quite a few bugs ... which brings me to the fun stuff - LoS (line of sight). Well I had an idea for this in mind but it turned out to be too heavy on the comp and didn't work well. I need an efficient way to scan the tiles around a tank and see if the tank sees them. Here are the rules of LoS that I tried to make fitting for my prev system so you can modify them:

The tank can see a distance of X tiles (4 for this example) around it - that means moving 4 tiles around like this:
Spoiler:
Image
The result is obviously a diamond-like shape.

next walls block vision like this:
Spoiler:
Image
with the first block behind the wall is invisible and the following ones become visible in straight diagonal (Didn't want to go too complicated with angles and stuff).
Those all were made for the old thing I tried which didn't work but feel free to change things to your liking *Deletes program makes 3D * .
They're watching ... Image

"I am forbidden tag" -CvN

User avatar
fr0stbyte124
Developer
Posts:727
Joined:Fri Dec 07, 2012 3:39 am
Affiliation:Aye-Aye

Re: Little Help Fellow Programmers ?

Post by fr0stbyte124 » Mon May 13, 2013 12:30 pm

Raycasting would be the easiest. With a set angular resolution, start with a point and an xy vector. You can either dynamically calculate this with trig functions, or pre-compute the full list. The vector doesn't need to be normalized.

Start with a point P and vector D, (assuming positive D.x and D.y, and coordinates are 1 = tile)
stepsX = (Math.Ceiling(P.x) - P.x) / D.x; //number of arbitrary steps to reach next X
stepsY = ((Math.Ceiling(P.y) - P.y) / D.y; //number of arbitrary steps to reach next Y

Whichever is smaller is the first intercept you are going to cross. Have a condition for both, as well as one for the rare condition where stepsX == stepsY. Let's say it stepsX was smaller:
P_new.x = P.x + (D.x * stepsX);
P_new.y = P.y + (D.y * stepsX);
target_tile.x++;
viewDistance++;

Keep going until you go off the map or your viewDistance maxes out.
By changing the number of rays casted, you can change the performance of the routine. One nice thing about this method is you get back points where the ray intersects the grid. You can use this to draw polygons on the screen to visually see what the raycasting is sending back.

There's a much more efficient line generating method called the Bresenham Line algorithm, which you can look up, but the way I showed is easier to understand and debug, so start with that.

User avatar
Iv121
Vice Admiral
Vice Admiral
Posts:2414
Joined:Fri Dec 07, 2012 3:40 pm
Affiliation:UTN
Location:-> HERE <-

Re: Little Help Fellow Programmers ?

Post by Iv121 » Mon May 13, 2013 12:56 pm

Yea you pretty much *Deleted program made 3D* for the noob I am :tongue: , this is gonna impress the examiner for sure ...

The vector points at the tile I'm currently checking ?
They're watching ... Image

"I am forbidden tag" -CvN

Post Reply