Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Playing with balls

edited July 2015 Posts: 8

This is the first project I have worked on in Codea. It's a simple project that shows balls and stuff. :>

Tagged:

• Posts: 8

Please show ways to make it better. • edited July 2015 Posts: 5,396

Nice work for a first project

if tx >= obj.x - obj.radius and tx <= obj.x + obj.radius
and ty >= obj.y - obj.radius and ty <= obj.y + obj.radius
and pchange == true then

--try this
--dist is the distance between two vectors (points)
if pchange and vec2(tx,ty):dist(obj)<=obj.radius then

• edited July 2015 Posts: 8

It's actually the 3rd version of the project. The first project only has the creation of balls and the second one is just a fancier version of the first.

I don't understand the code you just posted.
By the way, your ebook helped me a lot Edit:
I understood it but it gives me an error.

Edit2:
Eureka! Apparently, it doesn't work when you just put obj inside.

if pchange and vec2(tx,ty):dist( vec2(obj.x, obj.y) )<=obj.radius then

Just updated the code.

• Posts: 5,396

ah, yes, I overlooked that obj wasn't a vec2

it's worth looking at the other vector functions in Codea, they can be very helpful. If you make your position variables vec2's, then it makes using those functions a little easier (and your code a little tidier)

--if your position is t [=vec2(x,y)], instead of tx,ty
--and your object position is obj.pos instead of obj.x and obj.y
--and both of those are vec2, then you have
if pchange and t:dist(obj.pos) )<=obj.radius then

and your "apply force" line

v:applyForce(vec2((tx-v.x)*(pspeed+1), (ty-v.y)*(pspeed+1)))
--can be written as
v:applyForce((t-v)*(pspeed+1))

(PS thanks for the kind words about the ebook).

• edited July 2015 Posts: 437

Actually

if tx >= obj.x - obj.radius and tx <= obj.x + obj.radius
and ty >= obj.y - obj.radius and ty <= obj.y + obj.radius
and pchange == true then

and

if pchange and vec2(tx,ty):dist(obj)<=obj.radius then

are not equivalent. (But the second is probably what you want anyway.)

• Posts: 8

Thanks @Ignatz, I looked at the touched function and noticed that I could shorten the if statements into one if statement. I also made tx and ty into a vec2 in order to shorten other parts of my code.

@LoopSpace I don't understand why they are not equivalent, could you please explain why

• Posts: 5,396

They are not equivalent because my code assumes your objects are circular, whereas your original code worked for squares.

But I thought you probably wanted circles (and even if you have squares, it is close enough, since we have fat fingers that don't touch accurately).

• Posts: 8

Thanks, I get it now. (Fat fingers ))

I also noticed that instead of putting "== true", you could just write the variable/function. It also works with "== false" by putting "not" before the variable/function.

if var == true then

-- Just write
if var then

if var == false then

-- Just write
if not var then

• Posts: 5,396

That's right.

A very good trick is using or to set defaults for variables, eg in a function like this

function ABC(a,b)
--if a is nil or not provided, use 10
a = a or 10
--if b is nil or not provided, use 0.15
b = b or 0.15
return a,b
end

--then
ABC() --> returns 10, 0.15
ABC(3) --> returns 3, 0.15
ABC(nil,2) --> returns 10, 2

• Posts: 8

@Ignatz Thanks for the info I also thank you very much for your ebooks, I started reading the lua ebook before I even knew about Codea and it helped me a ton. 