We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, im remaking the atari game Asteroids as a beginner coder and im struggling on the rotation bit of the ship. im very new to rotate() and im not to sure how to make my ship rotate left and right. i also want to make the ship move in the direction its pointing using a variable so i can make sure the ship never leaves the screen size. i've tried to use translate() but i've come to relise that i dont know how to track a translation. can i please get some help, thanks.
Answers
This is an empty sketch. Clearly this works.
Now there is a rectangle in it. Still works.
Same rectangle, but now its position is controlled by the variables x and y.
Can we rotate it?
Hm. That's not right. The rectangle has MOVED. We didn't want it to move away from (20,20) - we wanted it to rotate.
But wait, to rotate, it needs a POINT to rotate around! What point is it rotating around?
Let's make it rotate a different amount every frame, so we can see which point it is rotating about.
Ah ha! It is rotating about the top left corner, (0,0)!
What if we MOVE SPACE? That is, what if we relocate our coordinate system so that (0,0) is at a different place?
Ah, so that's what translate() does!
Okay. So. Let's move (0,0) so that it is where our rect should be. Then when we rotate, it will rotate about the point where it is. Of course, since we moved the coordinates of where (0,0) is to where we want the rect to be, we now need to draw the rect at (0,0).
Hm. Closer. We're rotating about what looks like (20,20) now, but that is the corner of the shape, not the center.
Let's draw the rect so that it's center is now over the point where we're rotating.
Does this do what we want?
Yes! The rect now rotates about it's center, no matter where it is located.
Of course, we wanted a spaceship.
So let's use proper variable names, some useful functions, and throw in some keyPressed logic to move the ship...
What? The ship doesn't move right?
Oh yeah, the direction it moves needs to change based on the angle it is rotated.
Clearly this works.
Wait, what? it doesn't?
Sneaky! The ship now starts off rotated a quarter turn to the left, but it's drawn a quarter turn to the right!
Give yourself a gold star if you saw this next step coming.
Gotta figure this as an assignment given that it's about the twentieth time it has been asked.
Maybe someone oughta start teaching tfguy's code (again?)
Hello @TfGuy44
What if I also wanted to move left or right without rotating...
Then you will need to change (or add to, if you also want to keep the rotating) the logic that deals with the key presses.
Remember: x and y (in the Ship class, now) are the position of the ship. It is a simple matter to assign (use the assignment operator) those variables new values if certain conditions (use conditional statements) are met.
@TfGuy44 thanks a lot this should definitly help a ton I knw youve done so much, ive asked around and people tell me i should use a class instead of a PShape. I only use PShape becaue i semi inderstand it. If you could explain the difference in
A PShape is a type of common object that represents a Processing Shape (hence the name).
You can read more about it here: https://processing.org/reference/PShape.html
Or for an even more detailed look, here: http://processing.github.io/processing-javadocs/core/index.html?processing/core/PShape.html
Heck, the source code for the PShape object is even online, here (I think?): https://github.com/processing/processing/blob/master/core/src/processing/core/PShape.java
The point is this, however: A PShape is a common kind of object that you can use. You already use other (even more) common types of objects without thinking about it. Take, for example, int. Or float. Or String. Or maybe PImage. All of these objects are defined somewhere.
This ability - being able to define a new type of object - is one that you also have! You define a new type of object when you write a class for it. The class is like a blueprint that details what fields and methods an instance of that class will have.
The best example of this is one that I have already given - the Ship class, above.
Notice that the Ship class contains some fields (x, y, and a) that it uses to remember things that are useful for the ship to know about itself. It also has some methods (draw(), simulate(), and render()) that you can call that do things on a given ship.
If you really are writing an asteroids game, you will probably need two more classes. The first is a Bullet class, which you will use to track a shot fired by a Ship. The other is an Asteroid class, which tracks an asteroid.
Neither
int
norfloat
are objects in Java! They are directly accessed w/o any memory indirects. \m/There are 8 primitive datatypes in Java. And they're also keywords: :D
http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
@TfGuy44 this worked out for me, i went a head and made a class for the asteroids and i tried to put it as asteroid.draw(); in my void draw(); just like you did for ship_zero.draw();. the thing is that it cant run both ship_zero.draw(); and asteroid.draw(); at the same time. do you know how to make it so it can?
Post your current code if you need more help. Be sure to format it correctly for these forums (Select code and press Ctrl + o).
@TfGuy44 sorry i couldn't get to you yesterday, heres my code:
i had made a class for the asteroid and i plan on making 2 more classes for the smaller asteroids. like i said when i run this you can only see the asteroid and i even switched the asteriod and the ship's .draw and it only showed the ship. my understanding is that it runs one and then throws it away and then runs the next. i want them to both run and i want it to run when i add the other classes, thanks!
Without seeing your bigAsteroid and Ship classes, we have no idea what your code is doing. Post the ENTIRE sketch.
You have too many calls to background()!
There shouldn't be any calls to background inside other classes. All of them should come in the main class, and preferably only one in the start of draw(). Delete line 19 and 66.