Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9170863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:05:08+00:00 2026-06-17T16:05:08+00:00

First off, I’m new to LINQ, so I don’t really know the ins and

  • 0

First off, I’m new to LINQ, so I don’t really know the ins and outs of it. I’m attempting to use it in some code at the minute, and according to my diagnostics it appears to be about as fast as using a for loop in the same way. However, I’m not sure how well this would scale as the lists which I am working with could increase quite dramatically.

I’m using LINQ as part of a collision detection function (which is still in the works) and I’m using it to cull the list to only the ones that are relevant for the checks.

Here is the LINQ version:

partial class Actor {
    public virtual bool checkActorsForCollision(Vector2 toCheck) {
        Vector2 floored=new Vector2((int)toCheck.X, (int)toCheck.Y);

        if(!causingCollision) // skip if this actor doesn't collide
            return false;

        foreach(
            Actor actor in
            from a in GamePlay.actors
            where a.causingCollision==true&&a.isAlive
            select a
            )
            if( // ignore offscreen collisions, we don't care about them
                (actor.location.X>GamePlay.onScreenMinimum.X)
                &&
                (actor.location.Y>GamePlay.onScreenMinimum.Y)
                &&
                (actor.location.X<GamePlay.onScreenMaximum.X)
                &&
                (actor.location.Y<GamePlay.onScreenMaximum.Y)
                )
                if(actor!=this) { // ignore collisions with self
                    Vector2 actorfloor=new Vector2((int)actor.location.X, (int)actor.location.Y);

                    if((floored.X==actorfloor.X)&&(floored.Y==actorfloor.Y))
                        return true;
                }

        return false;
    }
}

This is my previous method:

partial class Actor {
    public virtual bool checkActorsForCollision(Vector2 toCheck) {
        Vector2 floored=new Vector2((int)toCheck.X, (int)toCheck.Y);

        if(!causingCollision) // skip if this actor doesn't collide
            return false;

        for(int i=0; i<GamePlay.actors.Count; i++)
            if( // ignore offscreen collisions, we don't care about them
                (GamePlay.actors[i].location.X>GamePlay.onScreenMinimum.X)
                &&
                (GamePlay.actors[i].location.Y>GamePlay.onScreenMinimum.Y)
                &&
                (GamePlay.actors[i].location.X<GamePlay.onScreenMaximum.X)
                &&
                (GamePlay.actors[i].location.Y<GamePlay.onScreenMaximum.Y)
                )
                if( // ignore collisions with self
                    (GamePlay.actors[i].isAlive)
                    &&
                    (GamePlay.actors[i]!=this)
                    &&
                    (GamePlay.actors[i].causingCollision)
                    ) {
                    Vector2 actorfloor=
                        new Vector2(
                            (int)GamePlay.actors[i].location.X,
                            (int)GamePlay.actors[i].location.Y
                            );

                    if((floored.X==actorfloor.X)&&(floored.Y==actorfloor.Y))
                        return true;
                }

        return false;
    }
}

At the minute, either run in almost no time (but run numerous times a second), but as the project builds and gets more intricate, this will be dealing with far more objects at once and the code to check for collisions will be more detailed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:05:09+00:00Added an answer on June 17, 2026 at 4:05 pm

    Your code looks pretty good; I’m not a big fan of changing working code, but if you did want to rewrite it to be easier to read, here’s what I would do:

    First, abstract away the predicate “is off the screen”. Perhaps make it a method of GamePlay. This business of checking every time whether the coordinates are in the bounds is (1) an implementation detail, and (2) making your code hard to read. It is possible that in the future you will have some more sophisticated mechanism for deciding whether an object is on the screen or not.

    Second, abstract away the vector flooring operation. Perhaps make it a method of Vector. Note that this method should return a new vector, not mutate the existing vector.

    Third, make an equality operator on vectors.

    Fourth, name the method better. A predicate should have the form “IsFoo” or “HasFoo”. You’ve phrased it as a command, not as a question.

    Fifth, you don’t need a loop at all.

    Sixth, it is strange to say somebool == true. Just say somebool. The former means “if it is true that this bool is true”, which is needlessly complicated.

    Let’s see how this shakes out:

    public virtual bool HasCollisionWithAnyActor(Vector2 toCheck)
    {
        // "Easy out": if this actor does not cause collisions then
        // we know that it is not colliding with any actor.
        if (!causingCollision)
          return false;
    
        Vector2 floored = toCheck.Floor();
    
        var collidingActors = 
          from actor in GamePlay.actors
          where actor != this
          where actor.causingCollision
          where actor.isAlive
          where GamePlay.IsOnScreen(actor.location)
          where actor.location.Floor() == floored
          select actor;
    
        return collidingActors.Any();
    }
    

    Look at how much easier that reads than your version of the method! None of this messing around with X and Y coordinates. Make helper methods do all that scutwork. The code now clearly expresses the semantics: tell me whether there are any collisions with other living, collision-causing actors on the screen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I'm just starting to mess around with LINQ, and I don't really
First off I use this code to make the navigation bar always stay fixed;
First off I don't know much about regex and need to buy a book
First off I am completely new to Javascript but I have some HTML/CSS experience.
First off, I would like to make clear, that I am SUPER NEW TO
First off, my code: @interface Block : NSObject { NSData *data; NSInteger slice_count; }
First off, I'm using Access 2000 and DAO. I have code that executes a
first off, i'm new to Struts and i have been following the tutorial here
first off I'm very new to rails - I'm playing about with a little
First off I'm quite new to objective-c and xcode in general, so it prop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.