We are about to switch to a new forum software. Until then we have removed the registration on this forum.
in my game when an asteroid hits the ship the ship will die. the problem is i dont know how to make the ship and asteroid collide when the x and y of both objects are in separate classes. should i make the variables global? also i have a minor thing, when you make a object out of a class you use something like this right? (assuming asteroid is a class and it is made to do something)
Asteroid drawAsteroid;
void setup() {
drawAsteroid = new Asteroid();
}
lets say i wanted two asteroids that had different x values (lets say the different x values were already coded, just focus on the two asteroids)i would do something like this right?
Asteroid drawAsteroid1;
Asteroid drawAsteroid2;
void setup(){
drawAsteroid1 = new Asteroid();
drawAsteroid2 = new Asteroid();
}
Well i could just make a new asteroid every time i wanted 1 more, is there a simpler way to create more (like if i had to make 1000000000 asteroids and was on a time schedule meaning i couldnt code 1 million asteroids manually) how would i do that?
Thanks - cloudi
Answers
Yes, use a container to store your object references. *-:)
If the number of objects is fixed, go w/ regular arrays:
https://Processing.org/reference/Array.html
Otherwise, an ArrayList is more appropriate as a dynamicly-sized container:
https://Processing.org/reference/ArrayList.html
http://www.JeffreyThompson.org/collision-detection/table_of_contents.php
@GoToLoop this is helpful however im having a hard time trying to put this example into my code. I made my asteroid and ship and its all based on x and y, meaning i have no variables that are for width and height. also i watched a video on collision (im sure you have seen it) and the lady uses
i thought that it would work but im not too sure since you mentioned this more complicated stuff. let me know your thoughts on this code and yours and if your code makes more sense too use can you explain why cause im kind of lost, thanks.
The same example I've posted above, but now w/ an extra display() method: O:-)
A slightly larger example, so you can have a better idea how to instantiate those classes: :-bd
@GoToLoop well im still not understanding too much, i think its because of my perameters.
extends
class Entity.Is this another question about your asteroids game? That's 7 now, how are we meant to know which one to answer? How are you meant to know which answer to believe?
This is why we prefer to keep the number of threads relating to a topic to a minimum.
i did some research about this and i found many people saying to use this http://www.jeffreythompson.org/collision-detection/object_oriented_collision.php i tried my hardest to use what this code is teaching and apply it to my code but i still doesn't work,i've also used other types of "easy" collision detection and it doesn't work out, i really just need to be able to use the variables that are inside my classes (x and y) outside. i know that i could just use a global variable but when i do that for my asteroid class it becomes very glitchy. there is a function called dist (im sure you guys have heard of it) and i want to do something like.
if (dist(sx,sy,ax,ay)) { // do something... }
maybe you guys can drill it into my head, heres all my code that relates with this, thanks.
I think you may need to read up about Inheritance.
You can access the x,y outside the class by saying
ship.x or
asteroids[2].x
etc.
....
@lord_of_the_galaxy thanks, i will read up on that later, @chrisir i could do that but then i would have to write manual code for 12 different asteroids, should i put in the asteroids for loop?
Yes. Do you mean ship versus asteroid?
That's easy
@chrisir yup i mean ship versus asteroid, i should use the dist function like i mentioned earlier right
Yes for-loop over asteroids and check with
dist
against ship@chrisir thanks, it looks like it should work. Unfortunately i cant try this until later, i have a big day of stuff to do. Ill let you know how it works and thanks again
Just PM me if you need something