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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:59:22+00:00 2026-06-07T10:59:22+00:00

I have an array of objects that are all ‘rectangles’. I also have a

  • 0

I have an array of objects that are all ‘rectangles’. I also have a circle that is the object. The equation I use for gravity is:

newYPos = oldYPos + prevFallingSpeed + gravity

Basically, I am adding the rate of gravity to the number of pixels the circle ‘fell’ in the previous frame and then adding that to the position of the circle in the last frame.

I am detecting if any part of the ball is inside of any of the objects using this code:

for(var i = 0; i < objects.length; i++){
    if(ball.x > objects[i].x - ball.r && ball.y > objects[i].y - ball.r && ball.x < ball.r + objects[i].x + objects[i].w && ball.y < ball.r + objects[i].y + objects[i].h){
        alert('test');
        gSy = (-1 * gSy);
    }
}

The code checks if the circle’s coordinates plus or minus the radius is greater than the top/left positions of the walls of the box and less than the right/bottom positions of the walls of the box.

The ball is inside the object at one point, but I never get an alert. I’ve tried everything I can think of. Hopefully I’m just making some dumb mistake I can’t see…

Here is a jsfiddle if you are up for messing with my code or don’t understand the variables:

http://jsfiddle.net/JGKx5/

  • 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-07T10:59:23+00:00Added an answer on June 7, 2026 at 10:59 am

    The small problem:

    You have four objects.

    • Two of them (numbers 1 and 3) are tall and thin, and off to the left or right. The ball never goes near them.
    • One of them (number 2) is short and wide, and at y-coordinates smaller than the ball ever attains.
    • The other one (number 0) is short and wide, and its y-coordinates are ones that a real physical ball would pass through — but because your ball moves in discrete steps your script never actually sees it do so. (It goes from y=580.4 to y=601.2.)

    The big problem:

    In the jsfiddle, all your comparisons in the collision test appear to be exactly the wrong way around :-). (Which is odd since the ones in the code here are the right way around.)

    With both of these changed (I made the ball move by 0.1*gSy instead of by gSy, and flipped all the comparison operators), collisions are detected.

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

Sidebar

Related Questions

Hello I was trying to remove objects from object array that I have and
I have an array of java objects. Each object stores two longs that define
I have an array of stdClass objects that are very simple, every object looks
I have an array that created 5 objects. Each object has two strings and
I have an array of objects that have QTTime structs as attributes. The objects
I have a function that takes an array of objects and I need to
I have a figure below: And my question is that the objects in Array
I have a mutable array that has about 100 objects in it and each
I have a form that generates an array of attribute objects. The attribute objects
I have a mutable array that is retained and storing several objects. At some

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.