This site requires JavaScript, please enable it in your browser!
Greenfoot back
aidanbb
aidanbb wrote ...

2018/4/9

Forces on Objects

aidanbb aidanbb

2018/4/9

#
How do I add a force that will push all objects in the world to the right when I press a certain key on the keyboard? I have already set up an off statement but I don’t know what code to put in it to make the objects get pushed to the right. Preferably I need to use vectors. Thanks
danpost danpost

2018/4/9

#
aidanbb wrote...
How do I add a force that will push all objects in the world to the right when I press a certain key on the keyboard? I have already set up an off statement but I don’t know what code to put in it to make the objects get pushed to the right. Preferably I need to use vectors. Thanks
Is it an assignment that requires you to use vectors? or is that just something you think will be needed? If only pushing horizontally to the right, I do not see any need to use vectors.
aidanbb aidanbb

2018/4/9

#
It is an assignment that requires vectors
danpost danpost

2018/4/9

#
aidanbb wrote...
It is an assignment that requires vectors
Are you given any classes or methods to use? or are you on your own on the whole thing? What classes are you working with? Show something to go with.
aidanbb aidanbb

2018/4/10

#
In the Greenfoot Text book it is part of the Newton’s lab scenario. We essentially have to do it all on our own and are using a body class. Here is the question: Add some keyboard control. For example, pressing the right arrow key could add a small force to the right to all Body objects.
danpost danpost

2018/4/10

#
aidanbb wrote...
Add some keyboard control. For example, pressing the right arrow key could add a small force to the right to all Body objects.
Okay. What exactly is the behavior you are wanting (as obviously the specifics are up to you). Like, do you want that small force to continuously be adding to the movement of the bodies while the key is down? or, do you want just one small force added when the key goes down and removed when the key is released? Did you write any "code" down on paper and if so, what? Have you tried any actual coding, and if so what? If not, what do you think would be involved in coding what you want?
aidanbb aidanbb

2018/4/10

#
I’ve tried writing code with an If statement that uses checkKeypress so that when the right arrow key is down a force is added. The issue I’m having is that I don’t know how to write the code within the If statement. I’ve tried using dx and dy like from the book but I can’t figure it out. Preferably I would like a small force to be continuously added to the objects when the key is pressed.
danpost danpost

2018/4/10

#
aidanbb wrote...
I’ve tried writing code with an If statement that uses checkKeypress so that when the right arrow key is down a force is added.
You mean isKeyDown -- don't you? (or maybe that is a method written directly in the Body class).
The issue I’m having is that I don’t know how to write the code within the If statement. I’ve tried using dx and dy like from the book but I can’t figure it out. Preferably I would like a small force to be continuously added to the objects when the key is pressed.
The dx and dy were showing you the parts of a vector and not literally used in code; but values for those vector attributes were. All you need to do is, if the key is found pressed, create a new vector with a small positive value for dx for the horizontal increase in speed toward the right (zero value for dy -- no vertical change). Then have this new vector added to the movement of the actor.
aidanbb aidanbb

2018/4/10

#
Can I get an example...I'm really struggling with vectors...
aidanbb aidanbb

2018/4/10

#
More specifically, how do I add the vector to the movement of the actor? Is this bit right?? public void checkKeypress() { if (Greenfoot.isKeyDown("right")) { new Vector(-3, 0.0); }
danpost danpost

2018/4/11

#
Negative 3 would be to the left. Also, 3 is a huge force -- especially if you will be adding it constantly until the key is released. Try something like:
1
adddForce(new Vector(0.03, 0.0));
aidanbb aidanbb

2018/4/11

#
Thanks
You need to login to post a reply.