Physion

  • Increase font size
  • Default font size
  • Decrease font size
Welcome, Guest
Username Password: Remember me

Bean machine generator (scripted scene)
(1 viewing) (1) Guest

TOPIC: Bean machine generator (scripted scene)

Re: Bean machine generator (scripted scene) 4 years, 9 months ago #778

  • Lynxx
  • OFFLINE
  • Gold Boarder
  • Java/C#-Developer
  • Posts: 223
ckstewar wrote:
This may be a dumb question, but how do you get all the balls back up to the top to rerun the process? I want to use this as the core for demonstrating statistics and I need to change color of balls, and other things like have two balls of different size hit and bounce off each other at the apex, have a small magnet at the apex that will deflect a certain color ball, etc. I just don't see controls for starting this or changing conditions. Please write me at This e-mail address is being protected from spambots. You need JavaScript enabled to view it .

You need to extend the script, your not very detailed here how to change colors and what sizes and so on .. but here's a example how to access the balls, if the script is running and you have clicked in the animation screen you can press R to reset the balls to their original position, i accomplished this by adding some code:
// create balls
for(var i=1; i<=bcount; i++)
{
for(var n=-10; n<=10; n++)
{
dx = 2*(rb+z);
xx = dx*n + n*(i-1)*z/2;
yy = y0 + r0*2 + 2*i*(rb+z) + dx*Math.abs(n)*sina/cosa;
var circle = world.createCircle(xx, yy, 0.0, rb);
circle.ShowLine = false;
circle.Friction = 0.1;
circle.Restitution = ball_restitution;
}
}
 

changed to:
// create balls
var balls = new Array();
for(var i=1; i<=bcount; i++)
{
for(var n=-10; n<=10; n++)
{
dx = 2*(rb+z);
xx = dx*n + n*(i-1)*z/2;
yy = y0 + r0*2 + 2*i*(rb+z) + dx*Math.abs(n)*sina/cosa;
var circle = world.createCircle(xx, yy, 0.0, rb);
circle.ShowLine = false;
circle.Friction = 0.1;
circle.Restitution = ball_restitution;
circle.StartX = xx;
circle.StartY = yy;
balls.push(circle);
}
}
 


and:
 
// create ground
makeStatic(world.createRectangle(0.0, -bh/2, 0.0, 2*halfW + 2*kxr + 2*r0, bh));
 

changed to:
 
// create ground
ground = world.createRectangle(0.0, -bh/2, 0.0, 2*halfW + 2*kxr + 2*r0, bh);
makeStatic(ground);
ground.OnKeyPress = 'if (key == Qt.Key_R) { for (var i=0;i<balls.length;i++) { with(balls[i]) { X = StartX; Y = StartY; LinearVelocityX = 0; LinearVelocityY = 0; } } }';
 

File Attachment:

File Name: Bean_Machine_WITH_RESET.script
File Size: 3886
Infinity is vast, especially near end.
bye, Lynxx

Re: Bean machine generator (scripted scene) 4 years, 9 months ago #779

  • Lynxx
  • OFFLINE
  • Gold Boarder
  • Java/C#-Developer
  • Posts: 223
Here is an example which also colors the balls:
 
// create balls
var balls = new Array();
var colors = new Array();
colors.push("red");
colors.push("yellow");
colors.push("aqua");
colors.push("lime");
colors.push("teal");
colors.push("orange");
colors.push("fuchsia");
colors.push("blue");
colors.push("sienna");
colors.push("silver");
 
for(var i=1; i<=bcount; i++)
{
for(var n=-10; n<=10; n++)
{
dx = 2*(rb+z);
xx = dx*n + n*(i-1)*z/2;
yy = y0 + r0*2 + 2*i*(rb+z) + dx*Math.abs(n)*sina/cosa;
var circle = world.createCircle(xx, yy, 0.0, rb);
circle.ShowLine = false;
circle.Friction = 0.1;
circle.Restitution = ball_restitution;
circle.StartX = xx;
circle.StartY = yy;
circle.Brush = new QColor(colors[i-1]);
balls.push(circle);
}
}


You could either use the predefnied color-names (take a look at Colors.script previews all colors from colornames) or use hex-color-encoding in the format #rrggbb, for example #ff0000 = red.

File Attachment:

File Name: Bean_Machine_WITH_RESET_COLORED.script
File Size: 4190


BTW: You should NEVER write your email-adress like this in a public forum, you will get a lot of spam soon. Use ckstewarATsbcglobal.net or something like that.
Infinity is vast, especially near end.
bye, Lynxx
Last Edit: 4 years, 9 months ago by Lynxx.

Re: Bean machine generator (scripted scene) 4 years, 9 months ago #780

  • Lynxx
  • OFFLINE
  • Gold Boarder
  • Java/C#-Developer
  • Posts: 223
I've got a slight idea what you might want to do ..
This version allows 3 keypresses:
// Key C = Color balls depending on their column
// Key R = Reset balls to their startposition
// Key B = Color and Reset the balls

Also the keypressing is done in a function, which greatly increases readability.
ground.OnKeyPress = 'keypressed(key)';
 
// Key C = Color balls depending on their column
// Key R = Reset balls to their startposition
// Key B = Color and Reset the balls
function keypressed(key) {
if (key == Qt.Key_C) {
for (var i=0;i<balls.length;i++) {
with(balls[i]) {
var j=0;
for(; j<=columns.length; j++) {
if (X<columns[j]) break;
}
Brush = new QColor(colors[j-1]);
}
}
}
else if (key == Qt.Key_R) {
for (var i=0;i<balls.length;i++) {
with(balls[i]) {
X = StartX; Y = StartY; LinearVelocityX = 0; LinearVelocityY = 0;
}
}
}
else if (key == Qt.Key_B) {
for (var i=0;i<balls.length;i++) {
with(balls[i]) {
var j=0;
for(; j<=columns.length; j++) {
if (X<columns[j]) break;
}
Brush = new QColor(colors[j-1]);
X = StartX; Y = StartY; LinearVelocityX = 0; LinearVelocityY = 0;
}
}
}
}


File Attachment:

File Name: Bean_Machine_WITH_RESET_COLORED2.script
File Size: 5144
Infinity is vast, especially near end.
bye, Lynxx
Last Edit: 4 years, 9 months ago by Lynxx.
Moderators: Glenn555
Time to create page: 0.18 seconds