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 8759391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:47:29+00:00 2026-06-13T14:47:29+00:00

I’m trying to understand how this code works for collision detection. I can tell

  • 0

I’m trying to understand how this code works for collision detection. I can tell the goal is a bounding box, and that we are testing each possible point for the entity, but I am uncertain of the purpose of the signed shift operator in this instance. In fact I don’t even understand why it would ever be useful, just what it does. Can anyone elaborate?

protected boolean move2(int xa, int ya) {
    if (xa != 0 && ya != 0) throw new IllegalArgumentException("Move2 can only move along one axis at a time!");

    int xto0 = ((x) - xr) >> 4;
    int yto0 = ((y) - yr) >> 4;
    int xto1 = ((x) + xr) >> 4;
    int yto1 = ((y) + yr) >> 4;

    int xt0 = ((x + xa) - xr) >> 4;
    int yt0 = ((y + ya) - yr) >> 4;
    int xt1 = ((x + xa) + xr) >> 4;
    int yt1 = ((y + ya) + yr) >> 4;
    boolean blocked = false;
    for (int yt = yt0; yt <= yt1; yt++)
        for (int xt = xt0; xt <= xt1; xt++) {
            if (xt >= xto0 && xt <= xto1 && yt >= yto0 && yt <= yto1) continue;
            level.getTile(xt, yt).bumpedInto(level, xt, yt, this);
            if (!level.getTile(xt, yt).mayPass(level, xt, yt, this)) {
                blocked = true;
                return false;
            }
        }
    if (blocked) return false;

    List<Entity> wasInside = level.getEntities(x - xr, y - yr, x + xr, y + yr);
    List<Entity> isInside = level.getEntities(x + xa - xr, y + ya - yr, x + xa + xr, y + ya + yr);
    for (int i = 0; i < isInside.size(); i++) {
        Entity e = isInside.get(i);
        if (e == this) continue;

        e.touchedBy(this);
    }
    isInside.removeAll(wasInside);
    for (int i = 0; i < isInside.size(); i++) {
        Entity e = isInside.get(i);
        if (e == this) continue;

        if (e.blocks(this)) {
            return false;
        }
    }

    x += xa;
    y += ya;
    return true;
}

It may be worth noting that the Entity knows its exact x and y position by pixel, but a tile doesn’t know its position at all. The world has an array of tiles, but only knows its tile position… so when it comes time to do collision detection the function must have to determine which tile position to get from the player position.

The full source is available here: http://www.ludumdare.com/compo/ludum-dare-22/?action=preview&uid=398

Note that tiles are 16×16

  • 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-13T14:47:30+00:00Added an answer on June 13, 2026 at 2:47 pm

    Dividing by a power of two is often expressed as a right-shift n bits where n is the power.

    Used to be when writing C or assembly you did that because it was MUCH faster than doing an actual divide. Left shift is the same as multiplying by the equivalent power of two and is also MUCH faster than hardware multiply. Nowadays most compilers will special-case this and emit shifts instead of multiply/divide for powers of 2.

    Of course if your tile size is not a power of 2 you have to divide instead.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.