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

  • Home
  • SEARCH
  • 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 9266595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:21:21+00:00 2026-06-18T14:21:21+00:00

I’m making a 2D game, nothing fancy, with objects (called entities from now on)

  • 0

I’m making a 2D game, nothing fancy, with objects (called entities from now on) that should collide with eachother and terrain. Now, detecting collision is not the problem, the problem is when the entities collide with the terrain.

I have a simple .png, upon loading the game I go through all pixels of this image and add all black ones to a list, these are the pixels that is collidable.

Collision-code from Entity-class:

    int ceiling = Integer.MAX_VALUE;
    int floor = 0;
    int rightPoint = 0;
    int leftPoint = Integer.MAX_VALUE;

    for(Point p : main.terrain.points)
        if(getHypoRect().contains(p))
        {
            if(p.x > rightPoint && right)
                rightPoint = p.x;
            if(p.x < leftPoint && left)
                leftPoint = p.x;
            if(p.y < ceiling && up)
                ceiling = p.y;
            if(p.y > floor && down)
                floor = p.y;
        }
    if(rightPoint != 0)
        xCoord = rightPoint - sprite.getImage().getWidth();
    else if(right)
        xCoord+=speed;

    if(leftPoint != Integer.MAX_VALUE)
        xCoord = leftPoint;
    else if(left)
        xCoord-=speed;

    if(ceiling != Integer.MAX_VALUE)
        yCoord = ceiling;
    else if(up)
        yCoord-=speed;

    if(floor != 0)
        yCoord = floor - sprite.getImage().getHeight();
    else if(down)
        yCoord+=speed;

In the for-loop I go through all points (black pixels) in the terrain, then check if the Entity’s collisionbox contains them (speed is added in this method, or rather “hypothetically” added, that’s why it has the weird name).
Now, if the x-value of the point is greater than the “rightpoint”, and if the Entity is walking to the right, it’s overwritten. The same happens with leftPoint (“leftest” point), ceiling and floor.
Then I check if any of these points have been changed, if they have that means we collided with the terrain so we bounce them back. For example, say the Entity is walking to the right and we collided with the terrain, rightPoint have been written with the “rightest” point we collided with so then we set the coordinate so that the Entity is right beside the point that it collided with.

If one would only go at one direction at a time, this would work perfectly, but “weird” things happen when you take multiple inputs into account.

Example: the Entity walks, flat to the floor, towards a ledge on the right, when the Entity is halfway over the ledge, it wants to go down, so both right and down are pressed, what my code will do is check the inputs, put the Entity where it wants to be and then bounce it back if it hit terrain, so it will see that I press right and bounce the Entity back to the end of the ledge.

This is all my own idea of how terraincollision could work, I couldn’t find anything online except how to check if a collision took place, but nothing that covers this.
Any idea what could make my solution work, or a pointer to how terraincollision should be done?

  • 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-18T14:21:22+00:00Added an answer on June 18, 2026 at 2:21 pm

    Your bouncing back idea maybe problematic. Try instead to ignore moves that would collide with the terrain:
    If the player moves down and right, then first check if down would collide, if yes, then ignore that movement, and check for right, if right is possible do right movement, else block, too.

    • 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
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.