# ball wall collision tutorial

Hi all,

just want to know if this is slightly the way to program ball/wall collision. Is there some collision DAT tuts online ?

def start():
ballxmin = op(‘pos_ball’)[‘0’, ‘P(0)’]
ballxmax = op(‘pos_ball’)[‘361’, ‘P(0)’]
ballymin = op(‘pos_ball’)[‘0’, ‘P(1)’]
ballymax = op(‘pos_ball’)[‘361’, ‘P(1)’]

``````wallxmin = op('walls')['0', 'p(0)']
wallxmax = op('walls')['1983', 'p(0)']
wallymin = op('walls')['0', 'p(1)']
wallymax = op('walls')['1983', 'p(1)']

p1xmin = op('pos_p1')['0', 'P(0)']
p1xmax = op('pos_p1')['23', 'P(0)']
p1ymin = op('pos_p1')['0', 'P(1)']
p1ymax = op('pos_p1')['23', 'P(1)']

p2xmin = op('pos_p2')['0', 'P(0)']
p2xmax = op('pos_p2')['23', 'P(0)']
p2ymin = op('pos_p2')['0', 'P(1)']
p2ymax = op('pos_p2')['23', 'P(1)']

if 	ballxmin >= wallxmin AND ballxmax <= wallxmax:

n = op('text')
n.write('hit')
else:
n = op('text')
n.write('nope')
return
``````

just have this feeling am doing it wrong …

Can you upload a file so I can get some context?

I’ll also mention you should avoid calling op() more times in a script that necessary. Try to cache the op reference in variables and use those multiple times in the script vs. re-searching for the node on every line.

aye… really love to gain more knowledge about DATs and Python…reading this book during building “Learn Python the hard way” ^^

will up the file
img.zip (1.29 MB)
glr_airplay.55.toe (10.3 KB)

Will take a look at this in the next day or two. Thanks

ok thx Elburz

First thing I noticed, line 34 of execute1, you had AND as all caps. Python is case-sensitive, so you’d have to put if ballxmin >= wallxmin and ballxmax <= wallxmax:

Then, the indentation is not correct where your else statement is. Right after the above if statement, you have to change it to:

else:
n = op(‘text’)
n.write(‘nope’)

Those two will fix the issues in your execute script. But they won’t do much, just some side notes.

That stuff aside, if you want to create a bouncing ball mechanic, in my mind it would be much easier to completely control the ball via a python script that runs every frame.

For this you’d use an Execute DAT and set it to run on Frame Start. Inside of that, you would write some basic code that would deal with moving the ball incrementally every frame, then doing a check to see if at that frame, the ball hits the wall, and if so to negate the result.

This code can be found here:

natureofcode.com/book/chapter-1-vectors

Extremely good book that covers a lot. The first example is about a bouncing ball and how to use vectors. For your use you should be able to get away with taking the code out of the void draw() function of example 1.1 and putting that in your execute dat. If you want to keep going ahead and learning vectors, you’re better off, but you could probably get away without them. Of course you’ll have to change that to Python, but if you’re learning Python it will be a good exercise!

Once you have your ball bouncing around the arena with the lfos you’ve added, then you can go one step further and add more if statements to detect if the ball has hit the paddles, and then to again negate the result.

Make sense?

wow thx Elburz…am on it

this is damn tough … execute DAT is mumbling about a missing function frameStart…but have it…played around a bit with this:

ball_go_x = op(‘ball_go’)[‘x’]
ball_go_y = op(‘ball_go’)[‘y’]
x = op(‘pos_ball’)[‘180’, ‘P(0)’]
y = op(‘pos_ball’)[‘180’, ‘P(1)’]
xspeed = 1
yspeed = 3.3
width = 1.5
height = 1

def frameStart(frame):

x = x + xspeed;
y = y + yspeed;

``````if x > width or x < 0:
xspeed = xspeed * -1

if y > height or y < 0:
yspeed = yspeed * -1

ball_go_x.write('ball_go')['x'] = x
ball_go_y.write('ball_go')['y'] = y

return
``````

frameStart()

trying the get it simple and started …problem i have is the width and height …as the center of my grid is 0 …should move the 0 position to the left top part of my grid.
glr_airplay.62.toe (9.8 KB)

Keep at it. Few things:

Python is an indented language, so indentation is extremely important. You have:

def frameStart(frame):

x = x + xspeed;
y = y + yspeed;

But since those bottom two lines are a part of that function, they should be indented like:

def frameStart(frame):
x = x + xspeed;
y = y + yspeed;

Secondly, you don’t need ; after lines of code. So change these:
x = x + xspeed;
y = y + yspeed;

To:
x = x + xspeed
y = y + yspeed

Thirdly you dont have to run frameStart(), that happens behind the scenes in the DAT, so you can delete that last line.

That should get your errors out of the way.

thx Elburz for your quick response

left the whole core project behind and opened a new .toe file so i can focus on the script …if that works i can then build the rest around. Reading you message i changed the code and got this

python.3.toe (3.67 KB)

Is this better ?

cheers Ian

forgot columns and changed write method proper i think:

ball_go_x = op(‘ball_go’)[‘x’, 1]
ball_go_y = op(‘ball_go’)[‘y’, 1]
xspeed = 1
yspeed = 3.3
width = 100
height = 100

def frameStart(frame):
x = x + xspeed
y = y + yspeed
if x > width or x < 0:
xspeed = xspeed * -1
if y > height or y < 0:
yspeed = yspeed * -1
ball_go_x.write(x)
ball_go_y.write(y)
return

ok dont have the problem anymore but still no change in X and Y in the table

attacht the toe file python.6.toe (3.68 KB)