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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:02:41+00:00 2026-05-25T11:02:41+00:00

I’m working on a simple 2D game (birds eye view). I have projectiles, which

  • 0

I’m working on a simple 2D game (birds eye view). I have projectiles, which are circles. There are barriers around the game level, which are all rectangles (axis aligned). I’d like the projectiles to bounce off the barriers when they collide. I’m not sure how to implement this in my game loop. Guessing it’d be something like this:

void gameLoop() {

    Projectile p = ..;

    p.updateLocation(p.getVelocity(), p.getDirection());

    Barrier intersected = p.intersects(barriers);
    if (intersected != null) {
        // We hit a barrier after moving on this time tick.
        // Figure out where our reflected location off the 
        // barrier should be now.
        Point ptNew = intersected.reflect(p.getLastPoint(), p.getVelocity(),
            p.getDirection());

        // Our new location after getting bounced off the barrier.
        ptNew.setLocation(ptNew);
    }
}

So after we move the projectile, we can check if we’re intersecting (or completely inside) one of the barriers. If so, do a computation to figure out where our reflected position should be off the barrier given our starting point and velocity/direction.

Thanks

———- Update ————————

A little more specific – given Erik’s comments, I do need to make sure the projectiles bounce properly, I can’t have them pass through a barrier if their velocity happens to be so fast they go right through on a single game loop iteration.

In that case, I guess I need to test a starting point, velocity, direction, against all barriers (possibly repeatedly) until no more intersections for the velocity are possible. Something like:

void gameLoop() {
    Projectile p = ...;

    Barrier barrier = moveProjectile(p, barriers);
    while (barrier != null && p.stillHasVelocityThisFrame()) {
        barrier = moveProjectile(p, barriers);
    }

    // at this point, the projectile is done moving / bouncing around.
}

void moveProjectile(Projectile projectile,
                    List<Barrier> barriers)
{
    for (Barrier barrier : barriers) {
        // tbd
        Barrier intersected = test2dVectorAgainstRectangle(
            projectile.getPosition(),
            projectile.get2dVector());
        // the above would test the projectile's vector
        // to see if it intersects any barrier, reflect
        // off of it if necessary, modify the projectile's
        // position, and reduce the available distance it
        // can still travel for this frame.
        if (intersected) {
            return intersected;
        }
    }

    // no intersections.
    return null;
}

Yeah this is already getting kind of tricky.

Thanks

  • 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-05-25T11:02:41+00:00Added an answer on May 25, 2026 at 11:02 am

    You want to use vector math for your reflections and collision checks. It’s pretty straight forward to detect if you were on one side of a line (edge of world rectangle) on 1 tic and on the other side of that line next. The same check will also give you the point of collision with just a touch more math. Then you have a point from which to do a reflection, again using vectors. Plotting collisions with other objects is a bit tricker, but still can be done. Make a list of all collisionswithin a loop and try and find the first one that would have occured along their movement paths – again reusing some dot product magic. Or, if you detect multiple collisions, you can always back up each object to it’s position the tic before and apply collisions from there.

    One thing you never want to do is allow an object to stay inside another object. It can cause them to get stuck on each other and act very erraticly – depending on how you do your collision code.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.