This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2009-09-25 14:45:24

scratch333
Scratcher
Registered: 2009-09-25
Posts: 3

ping pong game reflection equation

Hello
I am a new scratcher , the ping pong game I found here is "4 pong" which have some tricky kind of equation for reflecting the ball when it hits the paddle ...

I am considering to use the true equations .. like I mean:

1- sin[0] in the deflection equation when the ball hits this side of the paddle
2- sin[1] in th deflection equation when the ball hits that side of the paddle
3- sin[2] in the deflection equation when the ball hits that side of paddle ... and so on .


scratcher333

Offline

 

#2 2009-09-29 21:55:36

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: ping pong game reflection equation

If you want something that is simpl(er) and still does its job fairly well, I would use the script for the ball:

When start clicked
forever if touching paddle or if touching edge
rotate direction (whichever) direction - 180 degrees
move 5 steps

And here it is in pictures:
<when green flag clicked>
<forever>
<if><< <touching[ paddle <or> <touching[ edge >>
<turn cw( (( <direction> <-> 180 )) )degrees>
<move( 5 )steps>

I hope I could help!  big_smile

Offline

 

#3 2009-09-29 22:21:27

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: ping pong game reflection equation

That doesn't necessarily do it correctly Shadow, that just makes it move the opposite way, not bounce.

Offline

 

#4 2009-09-29 22:35:39

greenflash
Scratcher
Registered: 2009-05-27
Posts: 1000+

Re: ping pong game reflection equation

Magnie wrote:

That doesn't necessarily do it correctly Shadow, that just makes it move the opposite way, not bounce.

It bounces correctly if bouncing head on, but not at an angle. Bouncing is an interesing problem, I think we could use some bouncing blocks.  smile

Last edited by greenflash (2009-09-29 22:36:24)


http://i48.tinypic.com/2wrkirk.pnghttp://i46.tinypic.com/6r5zk7.pnghttp://i45.tinypic.com/2vtxr1t.png

Offline

 

#5 2009-09-29 22:37:05

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: ping pong game reflection equation

[Bounce if touching |sprite/edge|]

Offline

 

#6 2009-09-29 22:40:24

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: ping pong game reflection equation

How about not using trig at all?

Have 2 variables xVel and yVel

If the ball bounces on the left or right side multiply xVel by -1
If the ball bounces on the top or bottom side multiply yVel by -1

And make the ball move using xVel and yVel

Scratch lacks some trig functions so that makes things like this more difficult.

Last edited by archmage (2009-09-29 22:41:17)


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#7 2009-09-30 12:25:04

justtestingstickman
Scratcher
Registered: 2009-07-04
Posts: 100+

Re: ping pong game reflection equation

I'm looking for this too...


http://img509.imageshack.us/img509/2126/smalladvert.pnghttp://img175.imageshack.us/img175/8867/advertisementfor3sprite.pnghttp://img696.imageshack.us/img696/6061/failc.png
This signature has too many lines.

Offline

 

#8 2009-09-30 15:08:08

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: ping pong game reflection equation

I just posted something you can use.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#9 2009-10-01 18:41:47

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: ping pong game reflection equation

Magnie wrote:

That doesn't necessarily do it correctly Shadow, that just makes it move the opposite way, not bounce.

I know it is not perfect, as I explained in my post. But that is the general method used in Pong programs.

Offline

 

#10 2009-10-01 19:18:39

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: ping pong game reflection equation

Shadow, your way is totally incorrect. You can't have it just turn around.

My method however, works.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#11 2009-10-01 21:46:06

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: ping pong game reflection equation

Hmm...

[blocks]
<when green flag clicked>
<forever>
<if><touching[ Paddle ]>
<turn cw( 180 )degrees>
<if><( <direction> <>> # )>
<turn cw( 45 )degrees>
<end>
<if>( <direction> <<> # )>
<turn cw( 45 )degrees>
<end>
<end>
[/blocks]

Does that work some what? I just made it up in a flash, Ima go make a reflection project...

Offline

 

#12 2009-10-01 21:56:42

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: ping pong game reflection equation

Here it is! http://scratch.mit.edu/projects/Magnie/701486 Press space to change the sprite that the ball bounces off of, different bouncing with different angles!

Offline

 

#13 2009-10-01 22:22:22

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: ping pong game reflection equation

archmage wrote:

Shadow, your way is totally incorrect. You can't have it just turn around.

My method however, works.

I made a mistake. Sorry.

After the move () steps you have to have a block like this
turn (pick random ( + ) to ( - ) degrees (typically 20 works best).

Offline

 

#14 2009-10-01 22:29:14

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: ping pong game reflection equation

Random choosing won't work properly, just goes in random directions after touching something.

Offline

 

#15 2009-10-02 08:22:03

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: ping pong game reflection equation

It does in fact, work reasonably well. And it is not completely random either. Check the sample pong project.

Offline

 

#16 2009-10-02 09:03:58

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: ping pong game reflection equation

It is completely random. It turns in a randomly generated direction.

Also, the sample pong project is not coded for accuracy so they did made the code basic n easy .

Last edited by archmage (2009-10-02 09:05:49)


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#17 2009-10-02 19:15:04

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: ping pong game reflection equation

archmage wrote:

It is completely random. It turns in a randomly generated direction.

Also, the sample pong project is not coded for accuracy so they did made the code basic n easy .

You know you don't have to argue with everything I say. It gets irritating. And considering this is a new user who has no experience (or at least little) experience with things like this, I would go with mine.  big_smile

Offline

 

#18 2009-10-02 19:25:54

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: ping pong game reflection equation

If you don't want me to correct you then don't be wrong.

The OP wanted accuracy and you didn't deliver.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#19 2009-10-03 00:26:50

floppy_gunk
Scratcher
Registered: 2008-11-14
Posts: 500+

Re: ping pong game reflection equation

I would try this:
[blocks]<when green flag clicked>
<forever>
<move( 5 )steps>
<if><touching[ paddle
<set{ olddirection }to( <direction>
<point towards( paddle
<turn cw( (( 180 <+> <{ olddirection }> )) )degrees>
<end>
<end>[/blocks]

Last edited by floppy_gunk (2009-10-03 00:28:19)


http://img163.imageshack.us/img163/1917/2856lg.jpg Get it now!  smile

Offline

 

#20 2009-10-03 08:40:20

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: ping pong game reflection equation

I tried yours , Floppy, but it doesn't work to well.  hmm  Go check mine out. ^.^ Better but not perfect...

Offline

 

#21 2009-10-08 17:17:14

Locomule
Scratcher
Registered: 2009-08-24
Posts: 500+

Re: ping pong game reflection equation

http://scratch.mit.edu/projects/Locomule/709464

Probably not the best solution but one I came up with just for fun. If you want to do 'true' angle based reflections, here is an idea off the top of my head...

Assuming your paddles are on the left and right sides of the screen and moving only vertically, when ball and paddle collide subtract ball y from paddle y, multiply that by a small amount (this will allow you to fine tune the bounce amount later), inverse that and add it back to the normal 90º angle already figured.
Sounds good anyway  smile  The cool thing is instead of like some Pong games I've played where you could only reflect the ball off your paddle at a few angles, this should give you a huge, smooth range of bounce angles. You may have to do something different for the ends of the paddle and maybe make a dead zone (regular reflection) in the middle depending on how much you want to fine tune the paddle angle sweep.
Peace


aka Pain from DragonSpires, Delrith Online, BotBattle, Urban Dead etc etc lol

Offline

 

Board footer