Author Topic: Collision with Skybox  (Read 3035 times)

Offline AGP

  • quad
  • ******
  • Posts: 1515
    • View Profile
Collision with Skybox
« on: December 16, 2011, 08:57:14 pm »
Egon, I have a game that uses a sky sphere object that I wrote years ago. Now I want to convert it to the util.Skybox, because it's so much easier to deal the textures (not to mention easier to find stuff for). Problem is that this game requires me to collide with the sky. I could keep the sphere just for the collision bit, but I'd much rather be able to collide straight with the skybox (for one thing, it would save a good deal of polygons and for another it would be better for future games!). The best way to do this, I think, would be to get a method like Skybox.getObject3D(). Would you be willing to do that?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: Collision with Skybox
« Reply #1 on: December 16, 2011, 09:01:39 pm »
The SkyBox-class has a getWorld()-method. You can access the skyboy object from this world.

Offline AGP

  • quad
  • ******
  • Posts: 1515
    • View Profile
Re: Collision with Skybox
« Reply #2 on: December 16, 2011, 09:02:32 pm »
How?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: Collision with Skybox
« Reply #3 on: December 16, 2011, 09:14:13 pm »
Just use the Enumeration of Object3Ds. The first (and only) one is the skybox.

Offline AGP

  • quad
  • ******
  • Posts: 1515
    • View Profile
Re: Collision with Skybox
« Reply #4 on: December 16, 2011, 09:24:28 pm »
To be perfectly honest, I never used it. Could you just point me in the right direction?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: Collision with Skybox
« Reply #5 on: December 16, 2011, 09:36:25 pm »
Something like:

Code: [Select]
Object3D boxObj=(Object3D) skybox.getWorld().getObjects().nextElement();

Offline AGP

  • quad
  • ******
  • Posts: 1515
    • View Profile
Re: Collision with Skybox
« Reply #6 on: December 16, 2011, 09:38:28 pm »
Thanks a lot. But the question that follows is: isn't this a little clumsy? After all, I have no way of testing for whether I got the right object.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: Collision with Skybox
« Reply #7 on: December 16, 2011, 10:35:12 pm »
Yes, it is. But you can simply extend SkyBox and add a getObject3D()-method that does this internally, if you want to hide it from the rest of the application. But "what if" this changes?...well, it won't. You can rely on the fact that the first object returned will be the sky box.

Offline AGP

  • quad
  • ******
  • Posts: 1515
    • View Profile
Re: Collision with Skybox
« Reply #8 on: December 16, 2011, 10:38:20 pm »
OK, thanks. Would you add that to the docs for future reference?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: Collision with Skybox
« Reply #9 on: December 16, 2011, 10:40:11 pm »
Yes, i already did that.

Offline AGP

  • quad
  • ******
  • Posts: 1515
    • View Profile
Re: Collision with Skybox
« Reply #10 on: December 16, 2011, 10:48:40 pm »
Thanks.

Scratch what I just wrote afterwards, I just got it. :- )
« Last Edit: December 16, 2011, 10:50:16 pm by AGP »

Offline AGP

  • quad
  • ******
  • Posts: 1515
    • View Profile
Re: Collision with Skybox
« Reply #11 on: December 16, 2011, 11:11:28 pm »
Now I'm really going to sound like an idiot (it's a Java 5 quirk that I've never used too much):
Code: [Select]
theWorld.getObjects().nextElement().addCollisionListener(this);

doesn't work. I don't know whether I should (and where as I've tried on both ends of theWorld.getObjects().nextElement() put <Object3D>. And it can't find addCollisionListener without it.

Offline AGP

  • quad
  • ******
  • Posts: 1515
    • View Profile
Re: Collision with Skybox
« Reply #12 on: December 18, 2011, 08:14:50 pm »
I typecast it hope it works. Now here's another question: what happens when I replace the skybox (to change between levels)? Do I just keep calling nextElement()?

Again, I'm going to emphasize how clumsy this is.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: Collision with Skybox
« Reply #13 on: December 18, 2011, 09:06:58 pm »
If you replace the instance, the world inside will be replaced too.  If you can't get used to use the Enumeration (it's exists since Java 1.1...i'm not sure what you mean with Java 5 quirk...), just extend the SkyBox class, implement it once and forget about it.

Offline AGP

  • quad
  • ******
  • Posts: 1515
    • View Profile
Re: Collision with Skybox
« Reply #14 on: December 18, 2011, 09:22:12 pm »
What do you mean? When I change levels, I call World.removeAll(). I don't reinitialize the world.

The <...> stuff (instead of good old typecasting) is a Java 5+ thing.

By the way, I'm not having any success in rotating the Skybox to match the game's orientation (the X/Y plane instead of the X/Z one).
« Last Edit: December 18, 2011, 09:24:25 pm by AGP »