0

What I want to do is bounce an entity off of a paddle and have it react to the speed of the paddle as well.

So like in the old break out games, the faster you move the paddle the more obtuse the angle.

What I have thus far is this;

var playerPos = player.pos.x - paddle.pos.x;
var relativePos = Math.floor(player.pos.x - this.pos.x + (player.size.x / 2));
var angle = relativePos * (Math.PI / paddle.size.x);
var newVel = Math.cos(angle);

player.vel.x = newVel;

My math is not what it used to be.

edit

Player is an entity that has properties such as size.x and size.y, pos.x and pos.y, velocity.x and velocity.y. the this object has identical properties and is the paddle.

player is the ball if you will.

so you move the paddle and it's position in the y dimension is set and you move it in the x.

griegs
  • 22,624
  • 33
  • 128
  • 205
  • Just a blind guess: `player.vel.x = newVel * paddle.vel.x`. To make tinkering easier, could you host a live demo on http://jsfiddle.net/? – Blender Aug 21 '12 at 00:32
  • I can't host a live demo as the source for ImpactJs needs to be bought and i dont think the publisher would approve :) – griegs Aug 21 '12 at 00:34
  • The old break out games also set speed according to where the entity hits the paddle. So if the paddle is still and it hits the far right it shoots out far right. Would you want to incorporate this into your equation as well? – Evan Kennedy Aug 21 '12 at 00:37
  • @EvanKennedy, yes please – griegs Aug 21 '12 at 00:38
  • I'm trying to understand your code with the limited context given. First, what do the variables `player` and `this` mean in the given context. – Evan Kennedy Aug 21 '12 at 00:46
  • _"the more obscure the angle"_ - Do you mean the more _obtuse_ the angle? Or something else entirely? – nnnnnn Aug 21 '12 at 01:06
  • @nnnnnn, obtuse. i did say my maths wasn't what it was :) – griegs Aug 21 '12 at 01:19
  • So `paddle` and `this` are one in the same, correct? – Evan Kennedy Aug 21 '12 at 01:20

1 Answers1

1

You didn't mention this in your post, but I'm assuming you want to not change the velocity if it didn't hit the paddle, so let's make an if function that wraps our entire velocity-changing code:

if(paddle.x + paddle.size > player.x && player.x + player.size > paddle.s){
    //Code to be ran to change velocity
}

This only works if the player is square at the bottom. If you want to just go off of the middle of the player instead of the edges run this code instead:

if(paddle.x + paddle.size > player.x + (player.size/2) && player.x + (player.size/2) > paddle.x){
    //Code to be ran to change velocity
}

Now that we know that the player actually hit the paddle, let's adjust the velocity for where it hit the paddle. You could do this many different ways (and please tell me if you want it differently), but I would do this by creating a velocity based on the position it hit the paddle and add the current velocity to it. I am going to have the range span from -5 to 5.

player.vel.x += (player.x + (player.size/2) - paddle.x + (paddle.size/2)) /
                (paddle.size/2) * 5;

The first line calculates where the position of the player is in relation to the paddle, and the second line puts that into a ratio between -1 and 1 and multiplies it by 5. All of this is added to the current velocity. The variable 5 should be changed to your liking.

The last thing to do is adjust for the paddle velocity. This should be really easy as long as you aren't going to make the paddle have less or more of an effect on the overall velocity.

player.vel.x += paddle.vel.x;

Quite simple code there is all you really need.

So to put it all together:

if(paddle.x + paddle.size > player.x + (player.size/2) && player.x + (player.size/2) > paddle.x){
    player.vel.x += (player.x + (player.size/2) - paddle.x + (paddle.size/2)) /
                    (paddle.size/2) * 5 + paddle.vel.x;
}

Very simple code and no trigonometry needed. (yay!)

If you would like for this to be changed I can help as I actually do know math very well, I just wanted to simplify it because I'm not sure if it really needs to be more advanced.

Evan Kennedy
  • 3,975
  • 1
  • 25
  • 31
  • Thanks for this but what about the angle? So when my entity hits the paddle say, I need it to reflect according to where on the paddle it hit. So my players 'y' would need to reflect that – griegs Aug 21 '12 at 03:49