Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: Loulou 54 on July 23, 2012, 07:25:10 pm

Title: Pool Nspire
Post by: Loulou 54 on July 23, 2012, 07:25:10 pm
Hi all !  :D

I've already thought long ago about making a billiard game.. But that seemed too complicated (balls collisions, ...) and TI Basic on TI 89 would have been too slow.. (I didn't know C at this time ! :P )

But I remembered this idee some days ago and tried with Lua. ;)

I already managed to do the physics which seem quite real !  ;D(http://www.omnimaga.org/Themes/default/images/gpbp_arrow_up.gif)

(Moreover my code worked at the first try !! AMAZING :P )

I'm now making the gameplay and so on.. Actually, I think there is a lot of work if I want to make a complete game !  ^-^

A screen ?  ;)

EDIT :
- It's a 256 color GIF, so... x)
- There are only 7 balls right now and no hole, but it's coming, obviously ! :P

EDIT 2 : a newer version (but still a bad gif sorry ! :P) with 3D ! ;)
(http://www.omnimaga.org/index.php?action=dlattach;topic=14019.0;attach=13381;image)
Title: Re: Pool Nspire
Post by: Loulou 54 on July 23, 2012, 07:30:34 pm
First LabyRoll', then Pool, and next... (http://www.youtube.com/watch?v=oHg5SJYRHA0) :)
Title: Re: Pool Nspire
Post by: Eiyeron on July 23, 2012, 07:38:35 pm
Please could you helpme understand the physics o theballs?
Title: Re: Pool Nspire
Post by: cyanophycean314 on July 23, 2012, 08:07:28 pm
That looks really cool, especially the animation of hitting it. Great physics btw!  :D
Title: Re: Pool Nspire
Post by: TIfanx1999 on July 23, 2012, 09:51:44 pm
Looks quite nice so far Loulou! =)
Title: Re: Pool Nspire
Post by: Adriweb on July 24, 2012, 12:17:08 am
Nice !

Levak and I were actually creating one using physics too x)
(started like 3 days ago)


Edit : 3.2's Physics Engine.
Title: Re: Pool Nspire
Post by: 3rik on July 24, 2012, 12:21:33 am
This seems really nice. I look forward to seeing how it turns out.  :)
Title: Re: Pool Nspire
Post by: Hayleia on July 24, 2012, 02:11:51 am
That sounds really nice already :D
But does that use the physics library of the OS 3.2 ?
Title: Re: Pool Nspire
Post by: Reo on July 24, 2012, 03:12:30 am
Judging by the non-smoothed font, this must be running on 3.1. It looks great.
Title: Re: Pool Nspire
Post by: parserp on July 24, 2012, 11:45:02 am
Dude, this is one friggin epic game. :D
Can't wait for more progress!
Title: Re: Pool Nspire
Post by: DJ Omnimaga on July 24, 2012, 01:36:19 pm
Wow looks great :D. Do you re-use some code from Labyroll, such as the 360° movement and stuff? Can't wait to try it :)
Title: Re: Pool Nspire
Post by: Nick on July 24, 2012, 01:43:37 pm
ok, i might be a little (or a lot) slow, but is this made using the physics engine from 3.2? or did you write your own part of colliding code?
Title: Re: Pool Nspire
Post by: Loulou 54 on July 24, 2012, 02:51:54 pm
That sounds really nice already :D
But does that use the physics library of the OS 3.2 ?

No I made all by myself and so it can work on any "Lua supporting OS" ! :)

Nice !

Levak and I were actually creating one using physics too x)
(started like 3 days ago)


Edit : 3.2's Physics Engine.

Really ? ^^
Yes the 3.2's Physics Engine could be usefull for such a project ! :)

Please could you helpme understand the physics o theballs?

Collisions with walls aren't very difficult, but between balls it's kinda complicated..
Each ball is characterized by its coordinates and velocity. (no use to consider any acceleration) So I have a table like this which contains these informations for each ball :
balls={{x1,y1,vx1,vy1},{x2,y2,vx2,vy2},...,{xn,yn,vxn,vyn}}

First of all, you have to consider all couple of balls which could collide and test if they collide. The number of couple is thereby "2 among n". (nCr(n,2), binomial coefficiant)

For example the list of collision for 4 balls is :
{{1,2},{1,3},{1,4},{2,3},{2,4},{3,4}}  (6 collision possibilities = nCr(4,2))

Now, lets consider two balls wich could collide :
ball1 with x1,y1,vx1,vy1
ball2 with x2,y2,vx2,vy2
Their radius is r.

They collide if (x2-x1)^2+(y2-y1)^2 < (2*r)^2.

If they do, we have to calculate the new velocity of each ball.

By velocity subtraction, we can bring us to a more simple problem with :
- the ball 2 moving at the velocity {vx2-vx1,vy2-vy1}
- the ball 1 wich is immobile.

Now, after thinking, that's how I modeled the behavior of a moving ball hitting an other wich is motionless :

- the direction of the new velocity of the ball 1 is collinear with the vector defined by the center of gravity of each ball. (we will call this vector g2g1)
That's it :
(http://upload.wikimedia.org/wikipedia/commons/3/3f/Billard_eclatement.png)
from wikipédia

About the norm of the new velocity of the ball 1 (v1) : if v2 is the velocity of the moving ball (ball 2) and a is the angle between the direction of the vector g2g1 and the vector v :
v1 = v2*cos(a)

Thus, for example, if you hit the ball in the center, g2g1 and v are colinear and cos(a)=1 and the entire velocity of ball 2 is transmitted to the ball 1.
If they touch softly when g2g1 and v are almost perpendicular, ball 1 won't move much.

So we have defined the new velocity of ball 1 and  to have the velocity of ball 2 (the moving ball) you just have to subtract the velocity transmitted to the ball 1.
v2=v2-v2*cos(a)
(I haven't considered loses of power in ball-ball collisions since the coefficient of restitution between two balls is pretty high, and I don't want to do useless calculations..) However I have considered friction with table and a restitution of 75% in a ball-wall collision. ;)

So if we want these results in the case where ball 1 and ball 2 have their own velocity, we can have this result :

v1=v1+norm_v*cos(a)
v2=v2-norm_v*cos(a)
where v1 and v2 are vectors and norm_v = |v2-v1|

and as we want this on x/y, we have :

vx1=vx1+norm_v*cos(a)*cos(b)
vy1=vy1+norm_v*cos(a)*sin(b)
where b is the angle between Ox axis and the vector v1, which is colinear, as we said before, with the vector g2g1.
vx2=vx2-norm_v*cos(a)*cos(b)
vy2=vy2-norm_v*cos(a)*sin(b)

But how to calculate a and b angles ? (actually, cos(a), cos(b) and sin(b))
With the scalar product and so on we can have very good simplifications ! (no cos, no sin, no norm_v ;) )

And here is how looks the program for this part :

Code: [Select]
gx=boul[combi[i][2]][1]-boul[combi[i][1]][1]
gy=boul[combi[i][2]][2]-boul[combi[i][1]][2]
norm_g=gx^2 + gy^2

if math.floor(math.sqrt(norm_g)) < 2*r then
vx=boul[combi[i][2]][3]-boul[combi[i][1]][3]
vy=boul[combi[i][2]][4]-boul[combi[i][1]][4]
scal=gx*vx+gy*vy

boul[combi[i][1]][3]=boul[combi[i][1]][3]+math.floor(scal*gx/norm_g) -- vx + norm_v*cos((g,v))*cos((Ox,g))
boul[combi[i][1]][4]=boul[combi[i][1]][4]+math.floor(scal*gy/norm_g) -- vy + norm_v*cos((g,v))*sin((Ox,g))

boul[combi[i][2]][3]=boul[combi[i][2]][3]-math.floor(scal*gx/norm_g)
boul[combi[i][2]][4]=boul[combi[i][2]][4]-math.floor(scal*gy/norm_g)

end

It's a bit confuse but I hope you got some things.. ^^

So this is how I modelized collisions ! I don't know if it's right but it seems to be not bad at all ! :P
Title: Re: Pool Nspire
Post by: Loulou 54 on July 24, 2012, 03:00:50 pm
And thanks for compliments !  :thumbsup:
Title: Re: Pool Nspire
Post by: apcalc on July 24, 2012, 07:15:01 pm
Wow, looks great!  Nice work! :)
Title: Re: Pool Nspire
Post by: Hayleia on July 25, 2012, 01:29:25 am
That sounds really nice already :D
But does that use the physics library of the OS 3.2 ?

No I made all by myself and so it can work on any "Lua supporting OS" ! :)
Great :D
I was afraid I would not be able to play this when it comes out.
Title: Re: Pool Nspire
Post by: Nick on July 25, 2012, 01:31:15 pm
That sounds really nice already :D
But does that use the physics library of the OS 3.2 ?

No I made all by myself and so it can work on any "Lua supporting OS" ! :)

that's indeed wonderful! nice to see this running, and it seems to be working perfectly :) great
Title: Re: Pool Nspire
Post by: Loulou 54 on August 08, 2012, 10:35:42 am
Hello there !

I'm still working on my pool game and here's a new screen in the attachment ! ;)



As you can see on the shitty screen, I've decided to make a 3D view from the cloth, as if you were the white ball itself !  ;D(http://www.omnimaga.org/Themes/default/images/gpbp_arrow_up.gif)

I've made the 3D engine myself because RayCasting would have been to slow and not really appropriated.. Indeed I tried to make a light 3D engine as physics calculations are already important.

To do list :
- Implementing the rules
- Managing two players
- Artificial Intelligence !
- Menu and all that graphic and GUI stuff..

Maybe much later :
- Other billiard game as french billiard or "9-ball" (you know, where you must enter the balls in the right order, finishing with the ninth).

As you can see there are many possibilities but still much work !  ;D

NOTE : I've attached the .tns file, so try it ! (I have to find a better program for color screenshot !)  ^-^
Title: Re: Pool Nspire
Post by: critor on August 08, 2012, 10:53:35 am
Oh, great new view :)


When do we get a tns file ? ;)
Title: Re: Pool Nspire
Post by: critor on August 08, 2012, 11:18:11 am
NOTE : I've attached the .tns file, so try it ! (I have to find a better program for color screenshot !)  ^-^

Thanks for the edit, but are you sure about that?

I can see no attached file except the screen capture in your last and first posts...
Title: Re: Pool Nspire
Post by: p2 on August 08, 2012, 11:26:26 am
Hello there !

I'm still working on my pool game and here's a new screen in the attachment ! ;)



As you can see on the shitty screen, I've decided to make a 3D view from the cloth, as if you were the white ball itself !  ;D(http://www.omnimaga.org/Themes/default/images/gpbp_arrow_up.gif)

I've made the 3D engine myself because RayCasting would have been to slow and not really appropriated.. Indeed I tried to make a light 3D engine as physics calculations are already important.

To do list :
- Implementing the rules
- Managing two players
- Artificial Intelligence !
- Menu and all that graphic and GUI stuff..

Maybe much later :
- Other billiard game as french billiard or "9-ball" (you know, where you must enter the balls in the right order, finishing with the ninth).

As you can see there are many possibilities but still much work !  ;D

NOTE : I've attached the .tns file, so try it ! (I have to find a better program for color screenshot !)  ^-^

In that screenshot you sometimes see a green ball in front of the others! exactly has the same color as the table!
Is that a bug in game or in screenshotprogram?

but looks nice! ;)
Title: Re: Pool Nspire
Post by: Wayne on August 08, 2012, 01:21:50 pm
Nice :thumbsup:
But where is the download link ;)
Title: Re: Pool Nspire
Post by: Loulou 54 on August 09, 2012, 09:43:46 am
Oh yes I'm dumb I've forgotten it ! xD
I've edited my post. ;)

Billard v0.5 (http://www.omnimaga.org/index.php?action=dlattach;topic=14019.0;attach=13382)

P2 : yes it's a bug in the screenshot program.. x)

What do you use critor or others ?

CalcCapture is good but only for black/white..

EDIT : commands :
to aim : with the mouse or with arrows : (left/right=accurate ; up/down=faster)
to shoot : hold the click button or press enter to control the power and release it or press enter again to fire ! ;)
Title: Re: Pool Nspire
Post by: Deep Toaster on August 09, 2012, 10:24:57 am
That looks awesome. I love seeing games with realistic physics :D Can you control how hard you shoot?

EDIT: 3D what?!
Title: Re: Pool Nspire
Post by: Loulou 54 on August 09, 2012, 10:33:21 am
That looks awesome. I love seeing games with realistic physics :D Can you control how hard you shoot?

EDIT: 3D what?!

Yes obviously ! ;)

Yes 3D !! (not perfect but still..) Try it ! :P
Title: Re: Pool Nspire
Post by: Adriweb on August 09, 2012, 11:28:49 am
Very nice (just tested)

Also, the code looks weirdly indented (if at all ?), so here is a version with a properly indented and formatted code :
http://paste.bwns.be/p/f22b29b93

It runs pretty smooth :)
It really makes me want to continue the physics-based one to compare the performance !
Title: Re: Pool Nspire
Post by: Loulou 54 on August 09, 2012, 12:36:40 pm
Yes I know ^^ I don't use much indentation, but sometimes I use it to bring out things.. So yes it's weirdly indented ! x)

How did you get the source from my .tns ? :)
Title: Re: Pool Nspire
Post by: Wayne on August 09, 2012, 01:11:12 pm
Awesome
Do you add a highscore or a 2 player mode?
Title: Re: Pool Nspire
Post by: someone on August 09, 2012, 03:37:35 pm
How did you get the source from my .tns ? :)
You can now see the LUA code of the files if you use the latest version for the computer (Guess any version is fine). Is under the menu Insert -> Script Editor -> Edit Script

Do you add a highscore or a 2 player mode?
I don't know about the high-score, but 2 player is in his todo list
To do list :
- Implementing the rules
- Managing two players
- Artificial Intelligence !
- Menu and all that graphic and GUI stuff..
Title: Re: Pool Nspire
Post by: Loulou 54 on August 09, 2012, 04:12:41 pm
Thanks someone, I still haven't updated anything in 3.2.. x) I will do that soon for the student software. :)

Yes indeed two player mode is coming.

But about scores, how would it work, Wayne ? :)
Title: Re: Pool Nspire
Post by: Wayne on August 09, 2012, 04:49:20 pm
Do you mean coding or how to count. I have no idea of coding :-[

Edit: The only way i can help is to test your game and give suggestions for new features ;)
Title: Re: Pool Nspire
Post by: Loulou 54 on August 10, 2012, 12:25:34 pm
I meant how to count, yes. :)

Ok thanks I'll post the future versions here anyway. ;)
Title: Re: Pool Nspire
Post by: xacto on August 10, 2012, 09:59:17 pm
This looks really cool. And it works great too. :D
Title: Re: Pool Nspire
Post by: DJ Omnimaga on August 11, 2012, 01:01:36 am
I saw this on TI-Planet and this looks great. I need to try it since in the animated screenshots the colors sometimes disappear due to low color depth from CalcCapture.
Title: Re: Pool Nspire
Post by: Jonius7 on September 10, 2012, 01:53:41 am
Oh wow looks like you know your stuff. I've looked at your explanation on the first page about the physics. It boggles me (for now) but great to see some nice Lua development. Is it ready for a ticalc.org release?
Title: Re: Pool Nspire
Post by: Loulou 54 on October 06, 2012, 11:54:04 am
Hello Jonius ! :)

Thank you. :) Yeah the collisions were kinda complex, and trying to explain this in english increases the difficulty.. :P

No I don't think it's ready for a ticalc release. And moreover I'm still not registered on ti-calc ! :P But you can still try it out ! It's already playable, with 3D view ! But no rules, no AI.

The project is standby currently because of school.. But since the last release, I have implemented the rules and managed to do an AI partially working ! :D
Indeed the AI is already able to aim for a right ball according to the rules, and to enter it even using "combos" of n balls !  (using a recursive function and a sort of dichotomy to detect potential balls on the way.. :P )

So the project will be continued obviously, but not right away.

(Moreover I should correct ABA Logique Nspire to get it compatible with 3.2 ! :/ )
Title: Re: Pool Nspire
Post by: Loulou 54 on July 19, 2013, 07:25:54 pm
Hi,

I post here a reply to a user who asked me about the progress on the game. As there is an attached file and as I explain some stuffs, I put the answer here.
So you can watch my very early version of AI working on this file ! ;)


Quote from: binly
Hi, your nSpire pool game is a really great game that I love, and since you haven't posted about it in a while I was wondering if there have been any updates to it? I love the game and an update with AI or any other improvements would be great!


Quote from: Loulou 54
Hello !

Thank you for this feedback ! :)
Yes I had to stop the development of this game because of my very busy school year. But now that I'm recently on hollidays (ans that next year should be less time consuming ;) ) I should continue some calculator project :
* first I plan to finish the release of ABA Logique Nspire (http://tiplanet.org/forum/archives_voir.php?id=3761) for the OS 3.2 that I started last november ! (this a program about boolean algebra)
* then I will continue Pool Nspire ! I stopped progressing since end of august. I was indeed making an AI ! And improving the physics to make it more accurate.

So if you want, I can give you an unpublished version where the AI is playing against itself ! :)
The rules are even implemented !
In this version I was also trying out a new version of the physics engine but there are impressive bugs when many balls hit at the same time, as it often does at the beginning... ^^ But this engine is more accurate so the AI is better with this one ! ;) I'll have to improve it though.

Currently the AI can enter a ball according the rules, using combos of n balls ! This work already pretty well ! :)
But when the AI can't enter a ball this way, (it can't think with bounces on the bords yet) so when it doesn't know what to do, it just shoot very hard in a random direction ! :P
(so the AI can sometime be very strong and very stupid the next move ^^ but this will be improved obviously)

So yeah, have fun, even if you can only stare at the game on this version ! :P

PS : don't forget to try [tab] button ! ;)
(and [r] = reset the game)