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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:22:16+00:00 2026-06-13T19:22:16+00:00

public void checkForCollision () { int headX = cells[0].x; int headY = cells[0].y; int

  • 0
public void checkForCollision () {
          
  int headX = cells[0].x; 
  int headY = cells[0].y;
  int noOfParts = nPoints;
  
  for(int i = 1; i <noOfParts;i++)
  {
     int tempX = cells[i].x;
     int tempY = cells[i].y;
     
     
      if(tempX == headX && tempY == headY){
          JOptionPane.showMessageDialog(null,"Head hit body");
          //EndGameCollectScore etc.
        }
  }
       
} 

EDIT: ‘Cells[]’ is an array of type Point AND noOfParts is just how many segments the snake has

main Question

With the above code I’m trying to compare tempX to headX but i would like to have a sort of margin for error e.g. +-5 but am unsure how to accomplish this, my reasoning behind this is i’m thinking maybe the x and Y variables might be a few digits apart so if i have the radius of one of the segment of the snake (explanation of ‘snake’ in Alternate below) then if i’m right and the values are a tiny bit off it should still come back positive.

OR

Alternative

if anyone can suggest a better way for doing this? Basically it’s for a Snake game and headX and headY is the head of the snake and the remaining X and Y variables in Cells is the body, and I’m attempting to compare if the head hits the body.

I tested it and it seemed to work but after i tested it again it seems it will only pick up the collision if i make the snake double back on itself for a few squares. e.g. IF i cross the body perpendicular it will not detect the collision.

Also i am fairly certain that this method is called after each block the snake moves.

Cheers,
Shane.

P.S Running of very little sleep and way too much sugar in my blood, If you need further clarification because the above doesn’t make alot of sense let me know.

  • 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-13T19:22:17+00:00Added an answer on June 13, 2026 at 7:22 pm
    int eps = 5;
    if (Math.abs(tempX - headX) <= eps && Math.abs(tempY - headY) <= eps) { 
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public void Insert(int value) { if (value < Data) { if (LeftNode == null)
public void Find() { String Value = ; System.out.println(Search Name); Value = Input.next(); int
public void setRange(int which) { if (datelimitsset == 1) { if (startPicker.Value >= endPicker.Value
public void Draw(SpriteBatch theSpriteBatch) { Random rand = new Random(); for(int y = 0;
public void writeToParcel(Parcel parcel, int flags) throws IOException { doSomethingThatThrowsIOException(); } Quoth the compiler:
public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); switch (reqCode)
public void handleParsedCommand(String[] commandArr) { if(commandArr[0].equalsIgnoreCase(message)) { int target = Integer.parseInt(commandArr[1]); String message =
public void ScoreFirstBall(int pinsKnockedDown) { if (IsStrike(Frame, pinsKnockedDown)) { Score = X; ScoreMessage =
public void onListItemClick(ListView parent, View v, int position, long id) { super.onListItemClick(parent, v, position,
public void onItemClick(AdapterView<?> a, View v, int position, long id) { AlertDialog.Builder adb =

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.