C# Platform Collisions Not Working With Multiple Platforms
up vote
0
down vote
favorite
Right now I'm trying to make a simple game editor in C#, however there's a problem with when the user adds more than one platform to the screen:
private void tmrRunGame_Tick(object sender, EventArgs e)
{
foreach(Platform plat in platList)
{
if (plat.getBounds().IntersectsWith(player.getBounds()))
{
tmrGravity.Stop();
isColliding = true;
}
else
{
isColliding = false;
}
}
if(player.getY() < 500 && !isJumping && !isColliding)
{
tmrGravity.Start();
}
else
{
tmrGravity.Stop();
}
}
This code only stops the user from falling through the last created platform, all of the ones before that the user is able to just fall right through. What makes this all the more confusing is that the program is detecting collisions for all of the platforms, but only doing what it's supposed to for one! It's very frustrating and any help is appreciated.
This is how I'm adding platforms if that helps in any way:
private void pbPlatformSelect_MouseClick(object sender, MouseEventArgs e)
{
Platform plat = new Platform(100, 10, 50, 50);
plat.drawTo(this);
platList.Add(plat);
}
c# game-physics collision
add a comment |
up vote
0
down vote
favorite
Right now I'm trying to make a simple game editor in C#, however there's a problem with when the user adds more than one platform to the screen:
private void tmrRunGame_Tick(object sender, EventArgs e)
{
foreach(Platform plat in platList)
{
if (plat.getBounds().IntersectsWith(player.getBounds()))
{
tmrGravity.Stop();
isColliding = true;
}
else
{
isColliding = false;
}
}
if(player.getY() < 500 && !isJumping && !isColliding)
{
tmrGravity.Start();
}
else
{
tmrGravity.Stop();
}
}
This code only stops the user from falling through the last created platform, all of the ones before that the user is able to just fall right through. What makes this all the more confusing is that the program is detecting collisions for all of the platforms, but only doing what it's supposed to for one! It's very frustrating and any help is appreciated.
This is how I'm adding platforms if that helps in any way:
private void pbPlatformSelect_MouseClick(object sender, MouseEventArgs e)
{
Platform plat = new Platform(100, 10, 50, 50);
plat.drawTo(this);
platList.Add(plat);
}
c# game-physics collision
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Right now I'm trying to make a simple game editor in C#, however there's a problem with when the user adds more than one platform to the screen:
private void tmrRunGame_Tick(object sender, EventArgs e)
{
foreach(Platform plat in platList)
{
if (plat.getBounds().IntersectsWith(player.getBounds()))
{
tmrGravity.Stop();
isColliding = true;
}
else
{
isColliding = false;
}
}
if(player.getY() < 500 && !isJumping && !isColliding)
{
tmrGravity.Start();
}
else
{
tmrGravity.Stop();
}
}
This code only stops the user from falling through the last created platform, all of the ones before that the user is able to just fall right through. What makes this all the more confusing is that the program is detecting collisions for all of the platforms, but only doing what it's supposed to for one! It's very frustrating and any help is appreciated.
This is how I'm adding platforms if that helps in any way:
private void pbPlatformSelect_MouseClick(object sender, MouseEventArgs e)
{
Platform plat = new Platform(100, 10, 50, 50);
plat.drawTo(this);
platList.Add(plat);
}
c# game-physics collision
Right now I'm trying to make a simple game editor in C#, however there's a problem with when the user adds more than one platform to the screen:
private void tmrRunGame_Tick(object sender, EventArgs e)
{
foreach(Platform plat in platList)
{
if (plat.getBounds().IntersectsWith(player.getBounds()))
{
tmrGravity.Stop();
isColliding = true;
}
else
{
isColliding = false;
}
}
if(player.getY() < 500 && !isJumping && !isColliding)
{
tmrGravity.Start();
}
else
{
tmrGravity.Stop();
}
}
This code only stops the user from falling through the last created platform, all of the ones before that the user is able to just fall right through. What makes this all the more confusing is that the program is detecting collisions for all of the platforms, but only doing what it's supposed to for one! It's very frustrating and any help is appreciated.
This is how I'm adding platforms if that helps in any way:
private void pbPlatformSelect_MouseClick(object sender, MouseEventArgs e)
{
Platform plat = new Platform(100, 10, 50, 50);
plat.drawTo(this);
platList.Add(plat);
}
c# game-physics collision
c# game-physics collision
edited Nov 21 at 19:58
D Manokhin
602219
602219
asked Nov 21 at 19:56
BenCompSci
527
527
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
Replace foreach
loop with this code:
var playerBounds = player.GetBounds ();
isColliding = platList.Any (plat => plat.GetBounds ().IntersectsWith (playerBounds);
if (isColliding) tmrGravity.Stop ();
if you don't like LINQ, you can change you loop like this:
var playerBounds = player.GetBounds ();
isColliding = false;
foreach (var plat in platList) {
if (plat.GetBounds ().IntersectsWith (playerBounds)) {
isColliding = true;
tmrGravity.Stop ();
break;
}
}
add a comment |
up vote
1
down vote
I think you want to break out of the foreach loop once you determine you have collided with something. If you have 3 platforms, and you collide with the first, isColliding is true, but if it doesn't collide with the second platform, it will switch isColliding to false. In the end, whatever the intersection result of the last platform in the list is, is what isColliding's value will be.
So try putting 'break;' right after 'isColliding = true';
This is also an efficiency improvement because if you have 1,000 platforms and the player collides with the first one, we don't really care about the others (from what I can tell) and we save ourselves 999 iterations of the loop.
Yes that worked, thank you!
– BenCompSci
Nov 21 at 20:08
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Replace foreach
loop with this code:
var playerBounds = player.GetBounds ();
isColliding = platList.Any (plat => plat.GetBounds ().IntersectsWith (playerBounds);
if (isColliding) tmrGravity.Stop ();
if you don't like LINQ, you can change you loop like this:
var playerBounds = player.GetBounds ();
isColliding = false;
foreach (var plat in platList) {
if (plat.GetBounds ().IntersectsWith (playerBounds)) {
isColliding = true;
tmrGravity.Stop ();
break;
}
}
add a comment |
up vote
2
down vote
Replace foreach
loop with this code:
var playerBounds = player.GetBounds ();
isColliding = platList.Any (plat => plat.GetBounds ().IntersectsWith (playerBounds);
if (isColliding) tmrGravity.Stop ();
if you don't like LINQ, you can change you loop like this:
var playerBounds = player.GetBounds ();
isColliding = false;
foreach (var plat in platList) {
if (plat.GetBounds ().IntersectsWith (playerBounds)) {
isColliding = true;
tmrGravity.Stop ();
break;
}
}
add a comment |
up vote
2
down vote
up vote
2
down vote
Replace foreach
loop with this code:
var playerBounds = player.GetBounds ();
isColliding = platList.Any (plat => plat.GetBounds ().IntersectsWith (playerBounds);
if (isColliding) tmrGravity.Stop ();
if you don't like LINQ, you can change you loop like this:
var playerBounds = player.GetBounds ();
isColliding = false;
foreach (var plat in platList) {
if (plat.GetBounds ().IntersectsWith (playerBounds)) {
isColliding = true;
tmrGravity.Stop ();
break;
}
}
Replace foreach
loop with this code:
var playerBounds = player.GetBounds ();
isColliding = platList.Any (plat => plat.GetBounds ().IntersectsWith (playerBounds);
if (isColliding) tmrGravity.Stop ();
if you don't like LINQ, you can change you loop like this:
var playerBounds = player.GetBounds ();
isColliding = false;
foreach (var plat in platList) {
if (plat.GetBounds ().IntersectsWith (playerBounds)) {
isColliding = true;
tmrGravity.Stop ();
break;
}
}
answered Nov 21 at 20:10
trollingchar
1616
1616
add a comment |
add a comment |
up vote
1
down vote
I think you want to break out of the foreach loop once you determine you have collided with something. If you have 3 platforms, and you collide with the first, isColliding is true, but if it doesn't collide with the second platform, it will switch isColliding to false. In the end, whatever the intersection result of the last platform in the list is, is what isColliding's value will be.
So try putting 'break;' right after 'isColliding = true';
This is also an efficiency improvement because if you have 1,000 platforms and the player collides with the first one, we don't really care about the others (from what I can tell) and we save ourselves 999 iterations of the loop.
Yes that worked, thank you!
– BenCompSci
Nov 21 at 20:08
add a comment |
up vote
1
down vote
I think you want to break out of the foreach loop once you determine you have collided with something. If you have 3 platforms, and you collide with the first, isColliding is true, but if it doesn't collide with the second platform, it will switch isColliding to false. In the end, whatever the intersection result of the last platform in the list is, is what isColliding's value will be.
So try putting 'break;' right after 'isColliding = true';
This is also an efficiency improvement because if you have 1,000 platforms and the player collides with the first one, we don't really care about the others (from what I can tell) and we save ourselves 999 iterations of the loop.
Yes that worked, thank you!
– BenCompSci
Nov 21 at 20:08
add a comment |
up vote
1
down vote
up vote
1
down vote
I think you want to break out of the foreach loop once you determine you have collided with something. If you have 3 platforms, and you collide with the first, isColliding is true, but if it doesn't collide with the second platform, it will switch isColliding to false. In the end, whatever the intersection result of the last platform in the list is, is what isColliding's value will be.
So try putting 'break;' right after 'isColliding = true';
This is also an efficiency improvement because if you have 1,000 platforms and the player collides with the first one, we don't really care about the others (from what I can tell) and we save ourselves 999 iterations of the loop.
I think you want to break out of the foreach loop once you determine you have collided with something. If you have 3 platforms, and you collide with the first, isColliding is true, but if it doesn't collide with the second platform, it will switch isColliding to false. In the end, whatever the intersection result of the last platform in the list is, is what isColliding's value will be.
So try putting 'break;' right after 'isColliding = true';
This is also an efficiency improvement because if you have 1,000 platforms and the player collides with the first one, we don't really care about the others (from what I can tell) and we save ourselves 999 iterations of the loop.
edited Nov 21 at 20:09
answered Nov 21 at 20:05
Ben Krueger
426517
426517
Yes that worked, thank you!
– BenCompSci
Nov 21 at 20:08
add a comment |
Yes that worked, thank you!
– BenCompSci
Nov 21 at 20:08
Yes that worked, thank you!
– BenCompSci
Nov 21 at 20:08
Yes that worked, thank you!
– BenCompSci
Nov 21 at 20:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419630%2fc-sharp-platform-collisions-not-working-with-multiple-platforms%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown