{"id":15070,"date":"2022-10-10T07:41:01","date_gmt":"2022-10-10T02:11:01","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/"},"modified":"2022-10-10T07:41:01","modified_gmt":"2022-10-10T02:11:01","slug":"solved-ball-sometimes-bounces-sometimes-doesnt","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/","title":{"rendered":"[Solved] Ball sometimes bounces sometimes doesn&#8217;t"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-32748251\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"32748251\" data-parentid=\"32746683\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The code that checks bouncing is off by a bit. First of all,  <code>ball.getCenterX()+ball.getRadius()<\/code> is effectively the right side of the ball, and that&#8217;s the only part that you&#8217;re comparing to the paddle&#8217;s location. That means that the effective paddle location is off by an amount equal to the radius of the ball. You can see it in your image &#8211; the ball only &#8220;bounces&#8221; once the far right side of the ball is aligned with the paddle. <\/p>\n<p>Secondly, even after changing that part, your code will still fail in certain conditions. Imagine, for example, that the ball is directly under paddle but has not hit the wall yet. Presumably you want the ball to continue moving downward until it hits the wall, then bounce back upwards. However, with the code you have right now, the ball will immediately start moving upwards again as soon as the paddle is above the ball &#8211; according to your boolean conditions, the ball is below the paddle and the x-coordinate is between the paddle&#8217;s edges, so it should &#8220;bounce&#8221;. <\/p>\n<p>I&#8217;m assuming you&#8217;re a beginner and probably don&#8217;t want to get into a lot of math, so the simplest way to resolve this problem is to use AABBs (axis-aligned bounding boxes) and test for collision using that logic. Here&#8217;s an example: <\/p>\n<pre><code>\/\/ I'm creating a bunch of variables here because I don't know\n\/\/ what properties your objects have available.\nint ballLeftSide     = ball.getCenterX() - ball.getRadius();\nint ballRightSide    = ball.getCenterX() + ball.getRadius();\nint ballTopSide      = ball.getCenterY() - ball.getRadius();\nint ballBottomSide   = ball.getCenterY() + ball.getRadius();\nint paddleLeftSide   = paddle.getTopLeftX();\nint paddleRightSide  = paddle.getTopLeftX() + paddle.getWidth();\nint paddleTopSide    = paddle.getTopLeftY();\nint paddleBottomSide = paddle.getTopLeftY() + paddle.getHeight();\n\nbool ballOverlapsPaddle = ((ballLeftSide &lt; paddleRightSide) &amp;&amp;\n                           (ballRightSide &gt; paddleLeftSide) &amp;&amp;\n                           (ballTopSide &lt; paddleBottomSide) &amp;&amp;\n                           (ballBottomSide &gt; paddleTopSide))\n\n\/\/ Only change ball direction if the ball hasn't already passed\n\/\/ the paddle. In this case, \"passed\" means the ball's center is\n\/\/ further down than the bottom of the paddle.\nif ((ballOverlapsPaddle) &amp;&amp; (ball.getCenter() &lt; paddleBottomSide))\n    ball.setDirectionY(UPWARDS);\n<\/code><\/pre>\n<p>That boolean condition that checks for overlaps uses a very simple form of the Separating Axis Theorem (SAT) using axis-aligned bounding boxes (AABBs). Basically, it checks whether two rectangular shapes are colliding. Since one of your shapes is a ball, this method treats it as if it had a rectangular bounding box around it when it does this check. <\/p>\n<p>Now, although this solution is very simple, it does bring up some complications.  Since your ball is obviously NOT a rectangle, its effective bounding box is going to extend a bit beyond the actual shape of the ball. That means that you&#8217;ll have &#8220;collisions&#8221; even when there don&#8217;t visually appear to be any, as shown in the example below.<\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-Ball-sometimes-bounces-sometimes-doesnt.png\"><img decoding=\"async\" src=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-Ball-sometimes-bounces-sometimes-doesnt.png\" alt='False positive \"collision\"'><\/a><\/p>\n<p>There are of course methods to get around that issue, but that gets a little too detailed and techy for a Q+A format. If you&#8217;d like to look into it yourself, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.metanetsoftware.com\/technique\/tutorialA.html\">this is the tutorial<\/a> I used when I first learned about collision detection between shapes. <\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Ball sometimes bounces sometimes doesn&#8217;t <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The code that checks bouncing is off by a bit. First of all, ball.getCenterX()+ball.getRadius() is effectively the right side of the ball, and that&#8217;s the only part that you&#8217;re comparing to the paddle&#8217;s location. That means that the effective paddle location is off by an amount equal to the radius of the ball. You &#8230; <a title=\"[Solved] Ball sometimes bounces sometimes doesn&#8217;t\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/\" aria-label=\"More on [Solved] Ball sometimes bounces sometimes doesn&#8217;t\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[1346,3918,3917,323],"class_list":["post-15070","post","type-post","status-publish","format-standard","hentry","category-solved","tag-animation","tag-bounce","tag-collision-detection","tag-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Ball sometimes bounces sometimes doesn&#039;t - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Ball sometimes bounces sometimes doesn&#039;t - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The code that checks bouncing is off by a bit. First of all, ball.getCenterX()+ball.getRadius() is effectively the right side of the ball, and that&#8217;s the only part that you&#8217;re comparing to the paddle&#8217;s location. That means that the effective paddle location is off by an amount equal to the radius of the ball. You ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-10T02:11:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-Ball-sometimes-bounces-sometimes-doesnt.png\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Ball sometimes bounces sometimes doesn&#8217;t\",\"datePublished\":\"2022-10-10T02:11:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/\"},\"wordCount\":408,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-Ball-sometimes-bounces-sometimes-doesnt.png\",\"keywords\":[\"animation\",\"bounce\",\"collision-detection\",\"java\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/\",\"name\":\"[Solved] Ball sometimes bounces sometimes doesn't - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-Ball-sometimes-bounces-sometimes-doesnt.png\",\"datePublished\":\"2022-10-10T02:11:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#primaryimage\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-Ball-sometimes-bounces-sometimes-doesnt.png\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-Ball-sometimes-bounces-sometimes-doesnt.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Ball sometimes bounces sometimes doesn&#8217;t\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Ball sometimes bounces sometimes doesn't - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Ball sometimes bounces sometimes doesn't - JassWeb","og_description":"[ad_1] The code that checks bouncing is off by a bit. First of all, ball.getCenterX()+ball.getRadius() is effectively the right side of the ball, and that&#8217;s the only part that you&#8217;re comparing to the paddle&#8217;s location. That means that the effective paddle location is off by an amount equal to the radius of the ball. You ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/","og_site_name":"JassWeb","article_published_time":"2022-10-10T02:11:01+00:00","og_image":[{"url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-Ball-sometimes-bounces-sometimes-doesnt.png","type":"","width":"","height":""}],"author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Ball sometimes bounces sometimes doesn&#8217;t","datePublished":"2022-10-10T02:11:01+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/"},"wordCount":408,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-Ball-sometimes-bounces-sometimes-doesnt.png","keywords":["animation","bounce","collision-detection","java"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/","url":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/","name":"[Solved] Ball sometimes bounces sometimes doesn't - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#primaryimage"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-Ball-sometimes-bounces-sometimes-doesnt.png","datePublished":"2022-10-10T02:11:01+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#primaryimage","url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-Ball-sometimes-bounces-sometimes-doesnt.png","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/10\/Solved-Ball-sometimes-bounces-sometimes-doesnt.png"},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-ball-sometimes-bounces-sometimes-doesnt\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Ball sometimes bounces sometimes doesn&#8217;t"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/15070","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=15070"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/15070\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=15070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=15070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=15070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}