Answered need help with a windows forms app game

rowlandsfc

Active member
Joined
Feb 3, 2019
Messages
43
Location
bridgend
Programming Experience
Beginner
so i created a basic game and it has a level that is basically space invaders. i have a class for the bad guys, and a class for the bullets, but sometimes i have an issue in game where the bullet just goes straight through the enemy rather than actually hitting it. this doesnt happen all the time so i know the the bullet class and the contact between bullet and enemy does work bu t just dont understand why sometimes it would go straight through?


bullet collision with enemy:
bullet.RemoveAll(bullets => bullets.isDisposed); // check for collicion between enemies and bullets
            foreach(JetBullet b in bullet)
            {
                b.MoveBullet(this.panel1);
                foreach(EnemyShips enemy in enemies)
                {
                    if (b.d.Bounds.IntersectsWith(enemy.e.Bounds))
                    {
                        b.isDisposed = true;
                        b.d.Dispose();
                        enemy.isDisposed = true;
                        enemy.e.Dispose();
                        enemyNumber--;
                        enemyTimer.Enabled = true;
                        score = score + 5;
                        enemiesKilled = enemiesKilled + 1;
                        lblKilled.Text = "Enemies Kileed: " + enemiesKilled;
                        lblScore.Text = "Score: " + score;
                        enemiesKilled = enemiesKilled + 1;
                    }
                }
            }

thats the code i have for the collision between the 2 objects
 
Your collision detection is based on actual rectangle intersection. What if the bullet movement increment is larger the size of the enemy rectangles?

Let's imagine that your enemy is 10x10 pixels big and is at position (100,100) and has is not moving. Let's say that your bullet 2x2 is has a movement vector of (20, 0) and has an initial position of (15,105). You would expect the bullet to hit in the enemy since the enemy is not moving and the movement vector of the bullet points at the enemy and will go through it. Unfortunately, if we plot the positions of the bullets, we'll not see an intersection:
Code:
(15,105)
(35,105)
(55,105)
(75,105)
(95,105)
(115,105)
Between (95,105) and (115,105), the bullet has essentially teleported itself past the enemy occupying (100,100)-(110,110).

The other thing to consider as well is how you arranged your game loop. A well designed game loop will have you accept all the inputs all at the same time, followed by applying the inputs to movement vectors, followed by updating positions, and then finally doing collision detection. If you are doing things piece meal you could have potentially moved the bullet or the enemy at the wrong time before doing your collision detection.
 
Your collision detection is based on actual rectangle intersection. What if the bullet movement increment is larger the size of the enemy rectangles?

Let's imagine that your enemy is 10x10 pixels big and is at position (100,100) and has is not moving. Let's say that your bullet 2x2 is has a movement vector of (20, 0) and has an initial position of (15,105). You would expect the bullet to hit in the enemy since the enemy is not moving and the movement vector of the bullet points at the enemy and will go through it. Unfortunately, if we plot the positions of the bullets, we'll not see an intersection:
Code:
(15,105)
(35,105)
(55,105)
(75,105)
(95,105)
(115,105)
Between (95,105) and (115,105), the bullet has essentially teleported itself past the enemy occupying (100,100)-(110,110).

The other thing to consider as well is how you arranged your game loop. A well designed game loop will have you accept all the inputs all at the same time, followed by applying the inputs to movement vectors, followed by updating positions, and then finally doing collision detection. If you are doing things piece meal you could have potentially moved the bullet or the enemy at the wrong time before doing your collision detection.
thanks for the reply, sorry im only just learning this stuff, i understand about how it may have missed and that each thing is happening one at a time but not sure how i would go about solving it atm. so would i need to look at how fast my enemies and my bullet moves?
 
Search for "Continuous Collision Detection"?

A brief overview can be found 3/4th of the way down:
 
Search for "Continuous Collision Detection"?

A brief overview can be found 3/4th of the way down:
ok thanks for the link will look into it
 
so would i need to look at how fast my enemies and my bullet moves?
I don't think so...

Could it not be simplified by logging the points per objects positions based on your map sizes resolution? If two objects occupy the same point on the screen, ie a bullet, and a ship, then if both objects share the same point on the map, a collision between the bullet and the ship can make a collision condition true. Simply increase your score, destroy the ship and bullet and dispose of them. You could run a background worker or infinite loop to track the points of each newly spawned object position? Just brainstorming... but that link Skydiver gave you looks like a promising direction, but may require more code than my suggestion!
 
You would also find WPF much more resource efficient than Winforms. And If you are only learning, I recommend you move to WPF, since Winforms is EOL, and quite dated, as there are more modern, robust project types to use instead which would offer better performance. And if you are only learning, consider transitioning to WPF/Xamarin. Using Xamarin also make it easier to adapt your app to run on other platforms should that be something you may want to do later.
 
You would also find WPF much more resource efficient than Winforms. And If you are only learning, I recommend you move to WPF, since Winforms is EOL, and quite dated, as there are more modern, robust project types to use instead which would offer better performance. And if you are only learning, consider transitioning to WPF/Xamarin. Using Xamarin also make it easier to adapt your app to run on other platforms should that be something you may want to do later.
Thanks will take look into, im in college learning it and winforms is what they use
 
That's the trouble with schools today. They teach dated material's you don't need to learn. They should be teaching wpf and not something which is EOL.
 
the assignment we have is a game, and they start you off but giving you instruction on how to create a grid like minesweeper using 400 labels, but since i have added other features etc but also changed the image so its no longer a grid but just a image of space and the main thing i use the labels for now is for random placements of mines, and detecting mines around my ship, im wondering if it would be better for me to get rid of all the labels and just create a class for the mines and have them randomly placed on the panels as pictureboxes?
 
how to create a grid like minesweeper using 400 labels,
That's terrible!!! Even in C/C++ teaching Win32 API that is not the way you are taught how to create Minesweeper. 400 labels means that you have 400 window handles. Window handles are a limited resource. The correct thing to do is to render the grid all within a single custom control/window.

(As an interesting aside, I had the creator of Windows Minesweeper as my manager for about 2 years so we always try to sneak in a Minesweeper reference into meetings to tease him.)

Now to highlight why WPF is so much more efficient that WinForms. In WPF, you would also create 400 UI elements, but the UI elements are just an abstraction, and more importantly, they are not a limited resource. When the screen is drawn by WPF the UI elements contribute toward just rendering a single image on screen using the faster DirectDraw APIs (as opposed to WinForms using the slower GDI and GDI+).
 
Last edited:
That's terrible!!! Even in C/C++ teaching Win32 API that is not the way you are taught how to create Minesweeper. 400 labels means that you have 400 window handles. Window handles are a limited resource.

(As an interesting aside, I had the creator of Windows Minesweeper as my manager for about 2 years so we always try to sneak in a Minesweeper reference into meetings to tease him.)
Ye it's crap loading that one form, I've saved a copy as it's all working, so now I'm gonna try re do it but without all those labels
 
Are you honestly telling us that your school gave you a project and that project had 400 objects to use, or were these 400 objects your idea? Either way, that's a terrible idea. Why not randomly generate them and record their placement position as a point using x/y coordinates?
 
Are you honestly telling us that your school gave you a project and that project had 400 objects to use, or were these 400 objects your idea? Either way, that's a terrible idea. Why not randomly generate them and record their placement position as a point using x/y coordinates?
this was their tutorial, they had us create a program that saved the details and positions of the 400 labels and then we had to copy and paste what was in the text file into the designer of our game
 

Latest posts

Back
Top Bottom