obj doesnt get variable - object

I've got a processing program that is supposed to display 100 rings each one a bit bigger than the previous. when i run my program it shows 1 ring that is the biggest. i this case 100px from the middle.
here's my code:
Car[] myCar = new Car[100];
void setup(){
size(500,500);
noFill();
for (int i = 0; i != myCar.length; i=i+1) {
myCar[i] = new Car(i);
}
}
void draw(){
for (int i = 0; i != myCar.length; i=i+1) {
myCar[i].drive();
myCar[i].display();
}
}
class Car{
int c;
Car(int c_){
c = c_;
}
void drive(){
c = c + 2;
}
void display(){
background(255);
noFill();
stroke(10);
ellipseMode(CENTER);
ellipse(width/2,height/2,1+c,1+c);
}
}
my question is: why is only the biggest ring displayed?
it could be that
for (int i = 0; i != myCar.length; i=i+1) {
myCar[i] = new Car(i);
}
somehow doesn't give the variable "i" to my constructor.
or that I'm just dumb. I don't know.
Thanks in advance.

You don"t see them because when you call myCar[i].display(), that function clears everything that is already on the screen and sets background(255) and then goes on to draw a ring. This keeps happening and you only see one ring that is growing bigger and bigger every frame.
Just remove background(255) from the display() function and make it the first line of draw() and you will have your desired results.
Not always but for most cases, background() is called in either setup() or draw() because what it does is fill each pixel of the canvas with a color, think about that.

Related

Calling a String in Processing

I'm new to processing/java/code but was hoping for some help with my sketch.
I am trying to create an ink-looking sketch, with letters/characters displayed, then faded out instead of particles themselves. Inspired by https://openprocessing.org/sketch/1576908 I've run into errors with the entire particle constructor with an error on the line void update(p):
//update the velocity and location of particle
void update(p){
this.acceleration.add(createVector((noise(this.location.x)*2-1), (noise(this.location.y)*2-1)));
this.velocity.add(this.acceleration);
this.acceleration.set(0,0);
this.location.add(this.velocity);
this.alpha -= this.rate ;
// here is the recursion condition
if(this.alpha<=this.palpha*0.25 && this.palpha>10) {
p.push(new particle(this.location.x, this.location.y, this.rate*0.25, this.palpha*0.5));
}
}
Here is my full code
Thank you!
String[] particles = {"a", "b", "c", "d"} ; //string of particles
int velocity;
int acceleration;
int location;
int alpha;
int p;
void setup() {
size(600, 600);
background(255);
}
void draw() {
if(mousePressed) {
// spawn a new particle and add it to the array
particles.push(text(particles, mouseX, mouseY, 75));
textSize(random(20, 40));
}
// update and show the particles
for(int i=particles.length-2; i>=0; i--) {
particles[i].update(particles);
particles[i].show();
if(particles[i].alpha<=2) particles.splice(i, 5); // remove the dead particle
}
}
//particle class
class particle{
//constructor called when creating an instance of this class
// x & y are the location, r is the rate of decay, a is the starting alpha value
particle(float x, float y, float r, float a){
this.location = createVector(x,y) ;
this.velocity = createVector(random(-1,1),random(-1,1));
this.acceleration = createVector();
this.alpha = this.palpha=a ;
this.amp=4; // size of the particle
this.rate = r;
}
//update the velocity and location of particle
void update(p){
this.acceleration.add(createVector((noise(this.location.x)*2-1), (noise(this.location.y)*2-1)));
this.velocity.add(this.acceleration);
this.acceleration.set(0,0);
this.location.add(this.velocity);
this.alpha -= this.rate ;
// here is the recursion condition
if(this.alpha<=this.palpha*0.25 && this.palpha>10) {
p.push(new particle(this.location.x, this.location.y, this.rate*0.25, this.palpha*0.5));
}
}
//show the particles
void show(){
noStroke() ;
fill(0,35,25, this.alpha) ;
ellipse(this.location.x, this.location.y, this.amp);
}
} // end particle class```
You have at least two separate questions here:
how to port the p5.js sketch to Processing ?
how to add text for each particle ?
In the future I recommend breaking the problem down to simpler/shorter problems that can be tackle independently.
How let's look at the syntax errors Processing presents:
1.
particles.push(text(particles, mouseX, mouseY, 75));
errors with
The function "text()" expects parameters like: "text(int, float, float, float)"
The issue here is slightly masked. It looks like instead of calling text(yourTextString, yourTextX, yourTextY); you have different parameters. In reality there are two issues here:
push() is JavaScript Array's function. You need to use an ArrayList and its add() method instead in Processing (Java).
currently the particle class doesn't handle text. You can add a String text property which you can supply with a modified contructor: Particle(float x, float y, float r, float a, String text) (and you'd assign the constructor argument to the instance property (e.g. this.text = textl)
createVector exists in p5.js. In Processing you can switch this to new PVector(). Additionally you need to declare the variables initialised in the constructor as part of the class (e.g. location, velocity, acceleration, alpha, palpha, amp, rate).
ellipse(this.location.x, this.location.y, this.amp); is missing the last argument: ellipse(this.location.x, this.location.y, this.amp, this.amp);
This is a modified version of your code with the above notes applied:
// original sketch by OpenProcessing user Prasad
// https://openprocessing.org/sketch/1576908
String[] particlesText = {"a", "b", "c", "d"} ; //string of text
// array of particles
ArrayList<Particle> particles = new ArrayList<Particle>();
int velocity;
int acceleration;
int location;
int alpha;
int p;
void setup() {
size(600, 600);
background(255);
}
void draw() {
if(mousePressed) {
// spawn a new particle and add it to the array
// use % to loop over text (e.g .a,b,c,d,a...etc)
int textIndex = particles.size() % particlesText.length;
// grab the text from the array
String text = particlesText[textIndex];
// add a new particle providing text as well
particles.add(new Particle((float)mouseX, (float)mouseY,5.0, 75.0, text));
textSize(random(20, 40));
}
// update and show the particles
for(int i=particles.size()-2; i>=0; i--) {
Particle particle = particles.get(i);
particle.update(particles);
particle.show();
if(particle.alpha<=2) particles.remove(i); // remove the dead particle
}
}
//particle class
class Particle{
PVector location;
PVector velocity;
PVector acceleration;
float alpha;
float palpha;
float amp;
float rate;
String text = "";
//constructor called when creating an instance of this class
// x & y are the location, r is the rate of decay, a is the starting alpha value
Particle(float x, float y, float r, float a, String text){
this.location = new PVector(x,y) ;
this.velocity = new PVector(random(-1,1),random(-1,1));
this.acceleration = new PVector();
this.alpha = this.palpha=a ;
this.amp=4; // size of the particle
this.rate = r;
this.text = text;
}
//update the velocity and location of particle
void update(ArrayList<Particle> p){
this.acceleration.add(new PVector((noise(this.location.x)*2-1), (noise(this.location.y)*2-1)));
this.velocity.add(this.acceleration);
this.acceleration.set(0,0);
this.location.add(this.velocity);
this.alpha -= this.rate ;
// here is the recursion condition
if(this.alpha<=this.palpha*0.25 && this.palpha>10) {
p.add(new Particle(this.location.x, this.location.y, this.rate*0.25, this.palpha*0.5, this.text));
}
}
//show the particles
void show(){
noStroke() ;
fill(0,35,25, this.alpha);
//render the ellipse
ellipse(this.location.x, this.location.y, this.amp, this.amp);
// render the text
textSize(this.amp * 6);
text(this.text, this.location.x, this.location.y);
}
} // end particle class
(Note that you can choose not to render the ellipses and you can tweak the text size to something that makes more sense aesthetically. Also, when you're using other people's code, always credit them.)

Create Uniform Buffers in Vulkan

i hava a problem with drawing meshes in Vulkan.
I want to bind a UniformBufferObject in the following form to a Object.
void mainLoop() {
..
vulkanDrawing.Draw();
plane.UpdateUniformBuffers();
..
}
To get the currentImage, I created a method SetCurrentImage(uint32_t currentImage).
SetCurrentImage is set from VulkanDrawing::Draw() Method.
This current image is used in the UpdateUniformBuffers().
I get only a black screen if I run this application.
Since, I want to see a square.
In the past, I called the UpdateUniformBuffers Method with an imageIndex parameter in VulkanDrawing::Draw().
I think it could be a problem with the fences or semaphores. But I don't know how I shall fix it.
Does I use eventually a wrong Architecture?
I have attached important Methods:
void CVulkanDrawing::Draw()
{
vkWaitForFences(m_LogicalDevice.getDevice(), 1, &inFlightFences[currentFrame], VK_TRUE, std::numeric_limits<uint64_t>::max());
vkResetFences(m_LogicalDevice.getDevice(), 1,inFlightFences[currentFrame]);
uint32_t imageIndex;
vkAcquireNextImageKHR(m_LogicalDevice.getDevice(), m_Presentation.GetSwapChain(), std::numeric_limits<uint64_t>::max(), imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
for(unsigned int i = 0; i < m_VulkanMesh.size(); i++)
{
//m_VulkanMesh.at(i).UpdateUniformBuffers(imageIndex);
m_VulkanMesh.at(i).SetCurrentImage(imageIndex);
}
VkSubmitInfo submitInfo = {};
...
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
}
void CVulkanMesh::UpdateUniformBuffers()
{
...
vkMapMemory(m_LogicalDevice.getDevice(), uniformBuffersMemory[this->m_CurrentImage], 0, sizeof(ubo), 0, &data);
memcpy(data, &ubo, sizeof(ubo));
vkUnmapMemory(m_LogicalDevice.getDevice(), uniformBuffersMemory[this->m_CurrentImage]);
}
void CVulkanMesh::SetCurrentImage(uint32_t currentImage)
{
this->m_CurrentImage = currentImage;
}
I have additionally created a branch named: https://github.com/dekorlp/VulkanWrapper/tree/VulkanTest
I hope you can help me :)
Best regards
Pixma

Exception thrown at 0x00000000 in ConsoleApplication1.exe: 0xC0000005: Access violation executing location 0x00000000

After people told me to shorten the program I did it and here is the shortened version of the program with the same error as stated above.It only appears after a few moments into the program.If i hit continue the program works fine.However see the movement function?It does't work.The sprite refuses to move in any direction.However if i give a very large floating value in the move,then the sprite is displaced from it's position when i start the program and it stays there in that position with no further movement.For example if i write sprite.move(400.f,400.f) the sprite moves from (0,0) to (400,400) and stays there.It doesn't move any more.
Here's the shortened version of the code:
#include"SFML\Graphics.hpp"
#include<iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "CHECK",sf::Style::Default);
std::cout << "WORKS";
sf::Texture text;
text.loadFromFile("bahamut.png");
sf::Sprite sprite;
sf::Clock frap;
sprite.setTexture(text);
while (window.isOpen())
{
float fps = frap.restart().asSeconds();
sf::Vector2f movements;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
{
movements.y = 0;
movements.x = -1 * fps;
}
else
{if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
{
movements.y = 0;
movements.x = 1 * fps;
}
else
{ if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
{
movements.y = 1 * fps;
movements.x = 0;
}
else
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
{
movements.y = -1 * fps;
movements.x = 0;
}
else
{
movements.x = 0;
movements.y = 0;
}
}
}
}
sprite.move(movements);
window.clear();
window.draw(sprite);
window.display();
}
return 0;
}
I improved upon the code and it still produces the same results and error.
On using the dissassembler i saw the crash occurs at
00B37AEE cmp esi,esp
in window.display().
when i create a function and use it to display the sprite,the movement occurs but witthout the unction nada
Your logic says your movement is 0/0 if W is not pressed. The else of the W pressed block overrides all prior settings. And moving the sprite should happen before you display.
I cannot see a reason for the null pointer exception, but that is what the debugger is for. Next time this happens, debug.
Oh and it's int main(), not void. I know the compiler tolerates this error, but it's still an error and undefined behavior.

In thread, when loop too much times, Invoke waste a lot of time

for (int i = 0; i < 100,000; i++)
{
threadEvent.Invoke(i, new EventArgs());// tell processbar value
}
threadEvent += new EventHandler(method_threadEvent);
void method_threadEvent(object sender, EventArgs e)
{
int nowValue = Convert.ToInt32(sender);
nowValueDelegate now = new nowValueDelegate(setNow);
this.Invoke(now, nowValue);
}
private void setNow(int nowValue)
{
this.progressBar1.Value = nowValue;
}
private delegate void nowValueDelegate(int nowValue);
in the loop i do nothing, but it also waste alot of time !
why threadEvent.Invoke spend so much time ?
Invoking is an expensive operation, because it has to cross thread boundaries.
It's best to reduce the amount of invokes, by for instance only updating the progress bar for each percentage of work you do, rather than for each iteration of the loop. That way, only 100 updates need to be processed, rather than one for each iteration.
First thing you need to do is to calculate or estimate the current progress.
For a typical loop
for (int i = 0; i < someValue; ++i)
{
... // Work here
}
A good estimate of progress is (i / someValue) * 100, which gives the percentage of the loop that has been completed. To update the progress to the UI thread only when the next percentage has been reached you could do something in the line of:
int percentCompleted = 0;
threadEvent.Invoke(percentCompleted, new EventArgs()); // Initial progressbar value
for (int i = 0; i < someValue; ++i)
{
int newlyCompleted = (i / someValue) * 100;
if (newlyCompleted > percentCompleted)
threadEvent.Invoke(percentCompleted, new EventArgs());
percentCompleted = newlyCompleted;
... // Work here
}
Now finally, you could use BeginInvoke instead of Invoke to make sure the worker thread doesn't wait for the threadEvent to complete (PostMessage behaviour). This works well here because there is no return value from threadEvent that you need.

ObservableList producing errors

I made this program that produces 6 random numbers between 1 and 41. In the UI i have 6 textfields and a button. If i press the button the 6 numbers are being displayed in the textfields. I have 1 function(theNumbers()) fired when the button is pressed, the first function makes an ObservableList with the numbers 1-41 and the second function picks 6 numbers randomly.
The problem that happens is that after pressing the button a few times there is an ArrayIndexOutofBound Exception: -1 thrown, usually after (or a few times after) the number 0 is produced (which isnt even in the list) also sometimes a IndexOutOfBoundsException: Index: 4, Size: 4 exception is thrown(the number is random btw). Also it seems to be able to produce the same number twice or numbers bigger then 41.(seen 151)
This is my code:
First function:
ObservableList<Integer> list1 = FXCollections.observableArrayList();
private void createList(ObservableList<Integer> list1) {
this.list1 = list1;
int numbers = 41;
for (int i = 1; i <= numbers; i++) {
list1.add(i);
}
}
Second function:
private void theNumbers() {
createList(list1);
ObservableList<Integer> finalNums = FXCollections.observableArrayList();
try {
for (int i = 0; i < 6; i++) {
FXCollections.shuffle(list1);
int lucky = new Random().nextInt(list1.size());
finalNums.add(lucky);
list1.remove(list1.indexOf(lucky));
}
} catch (Exception ex) {
throw ex;
}
FXCollections.sort(finalNums);
textField1.setText(finalNums.get(0).toString());
textField2.setText(finalNums.get(1).toString());
textField3.setText(finalNums.get(2).toString());
textField4.setText(finalNums.get(3).toString());
textField5.setText(finalNums.get(4).toString());
textField6.setText(finalNums.get(5).toString());
list1.clear();
finalNums.clear();
}
Any clues to why it does this? Could it be my Acer Aspire One Netbook messing up? To my knowledge these errors shouldn't be happening.
Your code tries to remove the exact same number twice ore more in one pass of the for construct. For example you cannot remove the number "2" more than once from the list. The indexOf(2) method will return with -1 because it cannot find the value 2 in the list anymore.
So you might check if a number still exists in list1 before you try to remove from there.
private void theNumbers() {
createList(list1);
ObservableList<Integer> finalNums = FXCollections.observableArrayList();
try {
for (int i = 0; i < 6; i++) {
FXCollections.shuffle(list1);
int lucky;
do
{
lucky= new Random().nextInt(list1.size());
}while(list1.indexOf(lucky) == -1);
finalNums.add(lucky);
list1.remove(list1.indexOf(lucky));
}
} catch (Exception ex) {
throw ex;
}
That should solve your problem.
try using
int lucky = new Random().nextInt(list1.size()-1);

Resources