• 10
• 9
• 12
• 14
• 13

# Sticky power up?

This topic is 659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is just sort of a follow up on my previous post about power ups on break out game.

How can you move a ball together with the paddle while the ball is stuck on paddle and retaining its position on where it hits the paddle?

I have a reset code here where It will put the ball on center. If i modify it

 float x = paddle.position.X + ball.position.x;

it just not working.

Say for example I got a sticky power up and then the ball hit right on the paddle. Theory is that I should be able to move the ball stuck on paddle but retaining its position on where its stuck.

any tips? ty

Edited by FirstStep

##### Share on other sites
You cannot add two positions and expect something good will happen. You can add a position and a vector, though. So something like

  float dx = however_you_figure_out_how_much_to_move_the_paddle();

if (ball_is_stuck())
ball.position.x += dx;


##### Share on other sites

You cannot add two positions and expect something good will happen. You can add a position and a vector, though. So something like

  float dx = however_you_figure_out_how_much_to_move_the_paddle();



Im guessing this however() is the speed I am adding to the paddle? Am i correct?

Edited by FirstStep

##### Share on other sites

Either match the ball speed with the paddle speed at all times until it becomes unstuck, or

when the ball becomes stuck, store its position relative to the paddle, and always paint it there (ie stop moving the ball based on speed and instead paint it relative to the paddle).

Edited by Alberth

##### Share on other sites

You cannot add two positions and expect something good will happen. You can add a position and a vector, though. So something like

  float dx = however_you_figure_out_how_much_to_move_the_paddle();



Im guessing this however() is the speed I am adding to the paddle? Am i correct?

It's the speed of the paddle multiplied by the duration of a frame.

##### Share on other sites

when the ball becomes stuck, store its position relative to the paddle, and always paint it there (ie stop moving the ball based on speed and instead paint it relative to the paddle).

Say I hit the right of the paddle (100,100)

if I move the paddle position there must be a new position for the ball now. Although adding the speed works but I am interested on this getting the position relative to something?

Could you elaborate a little more? TY

##### Share on other sites

To explain this, we need the concept "coordinate system", in particular a 2-dimensional cartesian coordinate system.

https://en.wikipedia.org/wiki/Cartesian_coordinate_system

That is the official name for your every day coordinates. (and yes, there are a zillion other coordinate systems, and they can be crazy :) ).

200 and 100 are just numbers, and a pixel only has a colour, so these numbers refer to a system outside this pixel, namely the screen coordinate system. We said that the pixel at the top-left of the screen is (0, 0). The pixel at its right has coordinate (1, 0), the one next to that (2, 0), and so on. Similarly in the vertical direction.

In terms of a coordinate system of the pixels, the origin is at the top-left (ie where (0, 0) is), there are 2 dimensions in the coordinate system (x and y), the positive x is to the right, and the positive y is down.

(Yes, this looks very non-surprising. What I did here is add "coordinate system" to pixel (x,y) positions, which is the fundamental step, as you will see.)

[ The entire post below is about interpretation of these (a, b) pairs. You constantly have to keep in mind in what coordinate system you 'live'. It may be best to annotate that explicitly if you get confused, eg "(a, b)_pixels", "(c, d)_paddle".  You have conversion computations to 'jump' between coordinate system, like a=c+20, b=d+10, or the simpler a=c, b=d but we hardly think of them as "computations". ]

We established meaning of pixel(x, y), let's talk paddles.

Assume the paddle is at (200, 100), what does that mean?

It means you have a coordinate system in 2 dimensions (p and q), where the paddle is 200 units in the p dimension, and 100 units in the q dimension, from the origin of the coordinate system of the paddle.

Until now, there is a pixel coordinate system which is given to us, and we invented a paddle coordinate system with (p, q) coordinates. Nothing is connecting these two coordinate systems so far.

To be able to make changes in the paddle coordinates, and have a known effect at the screen, you have to connect both coordinate systems. If you decide that both coordinate systems are the same (the origins are on top of each other, and units in all dimensions are the same), things are "normal". Paddle at (200, 100) means it is shown at pixel(200, 100). p=p+1 means it moves 1 pixel to the right, and so on. Basically, you just connected both coordinate systems with (a,b)_paddle == (a,b)_pixel.

However, nothing prevents you from making a different connection between both coordinate systems. The simplest change is to move the paddle origin (a translation of the coordinates). Let's define (a+10,b+20)_pixel==(a, b)_paddle. How does it work now?

Paddle is still at (200, 100) in the paddle coordinate system, which now means

(210, 120)_pixel (compute the value of the expressions)

So ball(200, 100) is at pixel(210, 120) in the pixel coordinate system!

In terms of pixels, the paddle moved (10, 20) pixels, yet it's at the same (200, 100) in its paddle coordinates.

The connection between both coordinate system makes the difference.

<wacky stuff>
You can also change the positive direction of the axis, like (a, b)_paddle = (-a, b)_pixel

Change the unit of measurement in a coordinate system like (a, b)_paddle = (2 * a, b)_pixel

For the more mind-twisting options, nothing prevents you from using both coordinates, like

This is often used in games with dimetric perspective (also known as isometric perspective)
</wacky stuff>

Ok, back to your ball problem.

So far, we have a pixel coordinate system, and a paddle coordinate system, and we decided (a, b)_paddle = (a, b)_pixel, or pixel coordinate == paddle coordinate.

Let's add a ball, with positions (u, v). Again we have to decide what these coordinates mean, where is the origin attached to, what are the directions and units of the coordinates.

In the simple case you also connect the ball coordinate system to the pixel coordinates (a, b)_ball == (a, b)_pixel, so pixel coordinate == ball coordinate.

How are ball coordinates and paddle coordinates related now?

(300, 200)_ball

= (300, 200)_pixel   (since (a, b)_ball == (a, b)_pixel)

This was to be expected of course, we put all origins on top of each other, so no wonder that the same (a,b) position has the same meaning in all coordinate systems. The reason that I showed you the above is due to our decisions to attach both the paddle and the ball coordinate system to the pixel coordinate system. To go from ball to paddle, or from paddle to ball, we formally have to go through the pixel coordinate system as intermediate step.

We can also decide to place the orign of the ball coordinates at the origin of the paddle coordinates. To get from ball to paddle position then is a single coordinate system jump, to get from ball to pixels, is then two coordinate system jumps.  To understand how this is different, translate the paddle coordinates again in the pixel coordinate system, and try to connect ball coordinates to paddle coordinates again. (I don't explain it here, the post in long enough already.)

So far, we've put origins of coordinate systems relative to origins of other coordinate systems at a fixed offset, like (a+10,b+20)_pixel==(a, b)_paddle. There is nobody who says you can't use variables for that. Let's say we have (p, q) in the paddle coordinates, and that's where the origin of the ball coordinate is.

That is (a, b)_ball = (p+a, q+b)_paddle.

So ball at (0, 0) where is that in paddle coordinates?

(0, 0)_ball

This is to be expected of course, we defined the origin of the ball coordinates to be at (p, q) in the paddle system, and then asked position of (0, 0), ie the origin of the ball, in terms of paddle coordinates. Obviously, we should end up at (p, q), and indeed that is the case.

So here you have it. ball coordinates relative to paddle coordinates. You attach paddle coordinates to pixel coordinates, and define a variable position in the paddle system as origin of the ball system.

(20, 10)_ball  (some position in the ball coordinate system)

(20+p, 10+q)_pixel (paddle -> pixel jump)

I hope it was a bit closer to understanding than previously.

Edited by Alberth

##### Share on other sites

I am more confuse than ever now.

Say for example i have this

float x = paddle.position.X + paddle.width / 2 - width / 2;   // 100
float y = paddle.position.Y - height ;   // 100
position = new Vector2(x,y); // position(100,100)

I am on the paddle coordinate here I think. I am more confuse on the pixel coordinate. I cant get my head around this.

How Am i going to get the pixel? I am using XNA and I dont know about this pixel coordinate.Really really confuse..

##### Share on other sites

:(

My post discusses positions of single points (which have no size) in coordinate systems. I would suggest you think of zero-size paddles and balls first, before adding further complications by adding sizes, and weird XNA positioning systems (by the looks of your code).

For the purpose of the discussion, let's say the ball should be 10 units above the paddle (remember: 0 size paddle and ball).

Once you understand what coordinate systems exist, and how they relate, we can add more layers of complexity (like non-zero size, XNA weirdnesses, etc), hopefully without getting lost.

A useful strategy with confusing problems is to simplify the problem, and try again. Repeat simplifying and trying, until you understand the problem. Then try adding the parts you dropped while simplifying.

How Am i going to get the pixel? I am using XNA and I dont know about this pixel coordinate.Really really confuse..

What do you mean? If you draw a red pixel at (0, 0), you don't know where it ends up at the screen?

If you draw a green pixel at (1, 0) you don't know where it ends up at the screen?

How do you manage to display anything at the screen then?

In other words, first try to understand pixels on their own, before considering how they relate to paddle coordinates.

Edited by Alberth

##### Share on other sites

:(

My post discusses positions of single points (which have no size) in coordinate systems. I would suggest you think of zero-size paddles and balls first, before adding further complications by adding sizes, and weird XNA positioning systems (by the looks of your code).

For the purpose of the discussion, let's say the ball should be 10 units above the paddle (remember: 0 size paddle and ball).

Once you understand what coordinate systems exist, and how they relate, we can add more layers of complexity (like non-zero size, XNA weirdnesses, etc), hopefully without getting lost.

Hmm No matter how many times I read your post, it still makes a little sense to me.  :(

In terms of pixels, the paddle moved (10, 20) pixels, yet it's at the same (200, 100) in its paddle coordinates.

I was visualising things like these. I imagine a graphing paper. Since you said that a pixel is just a color. But if you add the coordinates it will have its position and will i guess be called pixel coordinate.

But then I thought I was starting to understand it. then You sad I move 10 pixels with the coordinate of 200. That got me thinking, since I cant add a size in a paddle visualizing it as a point might help? So it would be 210(moved).

How are ball coordinates and paddle coordinates related now?

(300, 200)_ball
= (300, 200)_pixel   (since (a, b)_ball == (a, b)_pixel)
= (300, 200)_paddle  (since (a, b)_paddle = (a, b)_pixel).

From what I understand here, since we are using the ball now, I cant visualize it as a zero size object. So in theory its the computation above results in the image below

So ball at (0, 0) where is that in paddle coordinates?

(0, 0)_ball
(p, q)_paddle

So basically it all comes down as this code

float x = paddle.position.x + ball.position.x;
float y = paddle.position.y + ball.position.y;