# \[Project ended\] game problem moving character with mouse, ball going through the character

**URL:** <https://discourse.processing.org/t/project-ended-game-problem-moving-character-with-mouse-ball-going-through-the-character/9808>\
**Category:** Coding Questions\
**Created:** [March 31, 2019, 9:04am UTC](https://discourse.processing.org/t/project-ended-game-problem-moving-character-with-mouse-ball-going-through-the-character/9808 "2019-03-31T09:04:22Z")\
**Posts on this page:** 1\
**Showing post:** 17

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 5, 2019, 1:01pm UTC](https://discourse.processing.org/t/project-ended-game-problem-moving-character-with-mouse-ball-going-through-the-character/9808/17 "2019-04-05T13:01:56Z")

</div>

> [@anon26872549](#):
>
> PVector ballPos;

```auto
float ballX, ballY;

```

you never had PVector in your code,  
you can use, OK, but why you use if you not like??

* * *

somewhere you are supposed to use  
hatX = mouseX -45 ; // middle HAT

to limit move must check:

> **[constrain() / Reference](https://processing.org/reference/constrain_.html)**
>
> Constrains a value to not exceed a maximum and minimum value.

hatX = constrain(mouseX,45,width-45) - 45;

for the draw of all that rectangles use  
hatX and not mouseX to make that limit happen,  
sorry need to move all rect again.

```auto
void drawPuppy() {
  hatX = constrain(mouseX, 45, width-45) - 45;
  hatY = 400;
  fill(0);
  rect(hatX, hatY, 90, 10); //HAT
  fill(255, 224, 189); //head
  rect(hatX+15, hatY+10, 60, 40);
  fill(210, 105, 30);
  rect(hatX+25, hatY+20, 15, 5); //left eye
  rect(hatX+25+20, hatY+20, 15, 5); //right eye
  fill(0);
  ellipse(hatX+42, hatY+36, 10, 2); //mouth 
  fill(255, 0, 0);
  rect(hatX+25, hatY+50, 40, 20); //body
  fill(0);
  rect(hatX+35, hatY+70, 5, 10); //legs        
  rect(hatX+35+15, hatY+70, 5, 10);
}

```

also the “ball hit hat” logic i would change  
( still your pvector )

```auto

boolean ballHitsHat() {
  // Rectangle and circle collision detection
// if (ballPos.y + ballRadius > hatY && ballPos.x + ballRadius > hatX && ballPos.x + ballRadius < hatX + hatW) {
  if (ballPos.y < hatY && ballPos.y + ballRadius > hatY && ballPos.x > hatX && ballPos.x < hatX + hatW) {
    ballPos.y = hatY-ballRadius; // anti rebounce
    return true;
  }
  return false;
}

```

so we are not able to deal with a hit on the hat corner,  
( if ball center outside hatX ( + hatW ) )  
but that would require real circle // rect collision logic!!!

---

_[View the full topic](https://discourse.processing.org/t/project-ended-game-problem-moving-character-with-mouse-ball-going-through-the-character/9808)._
