Community RSS Feed
http://community.wolfram.com
RSS Feed for Wolfram Community showing any discussions in tag Recreation sorted by active[GIF] Moving bars illusion
http://community.wolfram.com/groups/-/m/t/1127522
![enter image description here][1]
These bars are moving at a constant speed together, but it looks as if they move to the right in an alternating fashionâ€¦
Code:
size={sizex,sizey}={600,400};
n=16;
bars=sizex Subdivide[-1/2,1/2,2n-1];
width=N[bars[[2]]-bars[[1]]];
bars=Rectangle[{#1,-sizey/2},{#2,sizey/2}]&@@@Partition[bars,2];
cols={Lighter[Yellow,1/5],Darker[Blue,1/5]};
heights=N[sizey Subdivide[-1/2,1/2,Length[cols]+1][[2;;-2]]];
ClearAll[CreateScene]
CreateScene[\[Alpha]_]:=Module[{recs},
recs=MapThread[{#1,Rectangle[{\[Alpha] sizex-2 width,#2-width/2},{\[Alpha] sizex+2 width,#2+width/2}]}&,{cols,heights}];
Rasterize[Graphics[{bars,recs},PlotRange->({{-1,1},{-1,1}}size/2),PlotRangePadding->0],"Image",RasterSize->size,ImageSize->size]
]
Manipulate[CreateScene[a], {a, -0.6, 0.6}]
enjoy!
[1]: http://community.wolfram.com//c/portal/getImageAttachment?filename=movingbars.gif&userId=73716Sander Huisman2017-06-24T15:45:59ZFlappy Bird in a Mathematica notebook
http://community.wolfram.com/groups/-/m/t/1127985
I was asked to give a presentation about something fun I do with Mathematica. It struck me that I make a lot of interfaces, so why not make a game? As a first attempt I decided to port the game Flappy Bird to run in a Mathematica notebook. This is really a purely academic exercise, as I didn't stop to ask myself "should I do this?", but rather "how well would this run?".
You can find the final project on my GitHub account: [Flappy Bird in Mathematica][1]
I looked up a version of the game ([flappybird.io][2]) as I had actually never played the game before. I screen captured the pipes and other sprites and stripped off the backgrounds with `RemoveBackground`. The version I was taking as inspiration did not have sounds, so I added sounds that I found via google searches. I have no idea if they are correct.
I followed some ideas from an earlier Wolfram Technology Talk about [making Space Invaders in a Mathematica notebook][3], such as using a `ScheduledTask` to update the frames. I originally included hit detection using `RegionIntersection` but found that that was my main bottleneck. I coded my own compiled version using the distance from a rectangle to a point (it was two-orders of magnitude faster). I use compiled functions wherever possible.
You load the game's source code with `<<FlappyBird.wl` and then load the game with `playFlappyBird[]`.
Here's a short video of the final result:
![enter image description here][4]
[1]: https://github.com/KMDaily/FlappyBird_Mathematica
[2]: http://flappybird.io/
[3]: http://www.wolfram.com/broadcast/video.php?c=104&v=41
[4]: http://community.wolfram.com//c/portal/getImageAttachment?filename=FlappyGif.gif&userId=829295Kevin Daily2017-06-26T03:33:15ZMotion of a classical particle in a box (2 D and 3 D)
http://community.wolfram.com/groups/-/m/t/1127402
# https://wolfr.am/mAuOX0XK
This repository was made for the Homework Assignment for Wolfram Summer School 2017.
The "FunwithPhysicsin2D.nb" file in this repository contains code for implementing
the steps described in this readme file.
## Author: Bhubanjyoti Bhattacharya
## Date: June 21,2017
## Motion of a classical particle in a box (2 dimensions)
Here we will describe the motion of a classical particle inside a box with hard walls.
The particle will be represented by a single unit of a 2D (or 3D) raster. The interactions
with the walls will be considered elastic, i.e. such interactions simply reverse the direction
of motion perpendicular to the wall.
### The first step is to create a box and a point particle with given coordinates within the box.
We first create a 2-dimensional raster with one of the elements highlighted using a different color:
```
mybox[{mx_Integer, ny_Integer}, {px_Integer, qy_Integer}] :=
Graphics[Raster[
ReplacePart[
ConstantArray[{1, 1, 1}, {ny, mx}], {qy, px} -> {1, 0, 0}]],
Frame -> True, FrameTicks -> None];
mybox[{20, 10}, {5, 5}]
```
![basic_raster][1]
### The second step is to animate this box
(Note that the .nb file in this repository has more steps (more detailed description of the process I followed))
(Note also that we will use discrete time steps to describe the motion, to use this in conjunction with the Raster function)
```
mytimeAnimatedbox[{mx_Integer, ny_Integer}, {x0_Integer,
y0_Integer}, {vx_Integer, vy_Integer}] :=
Animate[mybox[{mx, ny}, {x0 + vx t, y0 + vy t}], {t, 0,
Min[(mx - x0)/vx, (ny - y0)/vy], 1}, AnimationRunning -> False];
mytimeAnimatedbox[{20, 10}, {5, 5}, {1, 1}]
```
![animated_figure][2]
Above we made a 20 x 10 raster in 2 dimensions. The particle is started with coordinates (5,5).
The new function takes values (vx,vy) which describe the speed of the particle in x and y directions respectively.
### The third step is to figure out what happens after collisions with a wall.
The particle's motion in 2D can be broken down into two independent motions in the x and y directions.
In our simple case these two motions are similar to each other. We can therefore describe both motions
with the same function.
Here we will try to figure out the function that describes the position of the particle 'n' time steps after it starts.
The idea is simple as follows:
* If incrementing the position by any number of time steps does not result in the particle hitting the wall boundaries,
then the particle's position follows the simple rule $x = x_0 + v_x t$
* If the particle hits a wall, we assume that the collision is elastic, so its velocity perpendicular to the wall
simply changes sign.
The above two rules can be implemented using the following function:
```
posn[pos_Integer, v_Integer, posmax_Integer, t_Integer] :=
1 + If[EvenQ[Floor[(pos + v (t - 1))/(posmax - 1)]],
Mod[pos + v (t - 1),
posmax -
1], (posmax - 1) Floor[(pos + v (t - 1))/(posmax - 1) + 1] -
pos - v (t - 1)];
```
In order to understand the above function we can plot it as a function of time steps. Below
we plot it for the first 50 time steps:
```
ListLinePlot[Table[{n, posn[5, 1, 10, n]}, {n, 0, 50}]]
```
![position_vs_time_plot][3]
### The final step is to put all of this together to actually obtain the result
The code that creates the two-dimensional box for us is as follows:
```
myFinalAnimatedbox[{m_Integer, n_Integer}, {x_Integer,
y_Integer}, {vx_Integer, vy_Integer}, tt_Integer] :=
Animate[mybox[{m, n}, {posn[x, vx, m, t], posn[y, vy, n, t]}], {t,
0, tt, 1}, AnimationRate -> 20];
```
![final_animated_figure][4]
## We will extend this construction to 3 dimensions
### Instead of using a 2D Raster we use a 3D Raster
Using the same techniques as before we can construct the function that generates a free particle in a 3D box:
```
my3DFinalbox[{m_Integer, n_Integer, o_Integer}, {x_Integer, y_Integer,
z_Integer}, {vx_Integer, vy_Integer, vz_Integer}, tt_Integer] :=
Animate[my3Dbox[{m, n, o}, {posn[x, vx, m, t], posn[y, vy, n, t],
posn[z, vz, o, t]}], {t, 0, tt, 1}, AnimationRate -> 5,
AnimationRunning -> True];
my3DFinalbox[{10, 10, 10}, {5, 5, 3}, {1, 1, 1}, 100]
```
![3D_animated_figure][5]
More cool examples in 3D are here: https://wolfr.am/mARXbPv7
Edited to remove padding in the figures for the collisions to look closer to real, using:
``` PlotRangePadding -> None```
[1]: http://community.wolfram.com//c/portal/getImageAttachment?filename=5693Fig1.png&userId=1081732
[2]: http://community.wolfram.com//c/portal/getImageAttachment?filename=5742Fig2.gif&userId=1081732
[3]: http://community.wolfram.com//c/portal/getImageAttachment?filename=Fig3.png&userId=1081732
[4]: http://community.wolfram.com//c/portal/getImageAttachment?filename=1865Fig4.gif&userId=1081732
[5]: http://community.wolfram.com//c/portal/getImageAttachment?filename=8935Fig5.gif&userId=1081732Bhubanjyoti Bhattacharya2017-06-24T00:46:58Z