Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript What does the x and y value mean here? Can you show in a figure?

shivajikobardan

Legendary Coder
Code:
function checkBoundaryCollision() {
  if (x - ballRadius <= 0 || x + ballRadius >= canvasWidth) {
    dx = -dx;
  }
  if (y - ballRadius <= 0 || y + ballRadius >= canvasHeight) {
    dy = -dy;
  }
}

SrLa34mOMtljsGdmLiNP0S4VdZzhFL92sA9SEM3bf-9q5OdDtPN_J8OAo2SD9R8LiVF4_w4PETvgTC445yoMUPq0uRqp3ogYiu__AIlclRDEf_X110-H3el_OrgPBBfwWu-OgOWaaOVGA5gv62pzDoiMQslngM79oB_Hgmk3H9r2Sk5yoMTIcnPHPvE1Xg


Full code here: https://codepen.io/pelko/pen/gOjXdaq
 
Code:
function checkBoundaryCollision() {
  if (x - ballRadius <= 0 || x + ballRadius >= canvasWidth) {
    dx = -dx;
  }
  if (y - ballRadius <= 0 || y + ballRadius >= canvasHeight) {
    dy = -dy;
  }
}

SrLa34mOMtljsGdmLiNP0S4VdZzhFL92sA9SEM3bf-9q5OdDtPN_J8OAo2SD9R8LiVF4_w4PETvgTC445yoMUPq0uRqp3ogYiu__AIlclRDEf_X110-H3el_OrgPBBfwWu-OgOWaaOVGA5gv62pzDoiMQslngM79oB_Hgmk3H9r2Sk5yoMTIcnPHPvE1Xg


Full code here: https://codepen.io/pelko/pen/gOjXdaq
Hey there,
Given the fact that there is this in the code
1674142950307.png
it seem that ^ at this point x and y contain the starting values of the object. Further down, towards the bottom, you see this function
1674143056463.png
You can see that x and y are being reset here, to whatever their current values are plus whatever dx and dy are. Now, based off of the snippet of code you posted, you are checking whether or not x and y meet some criteria in order to set dx and dy. At this point, one can deduct that dx and dy are the changes in the x direction and the y direction, respectively.
 
To just answer the posted question, x is the horizontal position of the center of the ball, and y the vertical position of the center. relative to the upper left corner of the canvas.
How that relates to your picture, or why you need a picture to clarify this, I have no clue at all.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom