PixelGrabber

Hi!

Im currently working on a game (ever tried Zero Gravity on the Amiga 500, or Lunar Lander??). Well, anyway, its a 2D game where you navigate a Lander through treacherous terrain, and caves.

Im new to Java so Im learing as I go along, but there is one thing that I havent been able to figure out, and that is how to check for collisions with the background (the montains and rocks) while simultaneously rotating the lander.

The way Im checking for collision is by using PixelGrabber to store the pixel layout of the lander object into an array, and then, every frame, the background object checks the position of the lander, grabs the pixels of the background image corresponding to that position, and then compares the two.

I have made all the "empty" areas blue (255). As long at the background pixels that are overlapping the lander pixels are blue, nothing happens, but if the color of the pixels are anything but blue, and those overlapp a part of the lander pixels that are not blue, the ship blows up.

So the problem Im faceing at the moment is that I need to be able to grab pixels other than in an straight horizontal/vertical way, since the landers coordinates are not fixed in these directions.

I have a possible solutions to the problem which I havent tried yet, and that is to have an offscreen image of the background, which I rotate as the ship rotates. This offscreen image would not be used for painting though. But, the easiest way would be to alter the array in the lander object that contains the pixel layout. Or to be able to issue commands through PixelGrabber, to grab a "rotated, rectangle area".

Does anyone have a clue how I should procede to solve this problem?

Thanks!
// [Nano]

Comments

  • Hey Nano,
    Cool problem!
    My experience with PixelGrabber is that it's pretty slow, so I'm not sure that rotating your background and re-grabbing the pixels is a practical solution (neat idea, though).
    Is your ship a solid sprite or a wireframe shape (like the original Lunar Lander)? If the latter, I have an idea that *might* work.
    Assuming that the lander is a wireframe shape, you should have the coordinates for each vertex in its outline. You could rotate these to generate the coordinates for the rotated ship. Then, for your collision test, you could use Bresenham's "line-drawing" algorithm to move along each line segment in the lander's outline, pixel by pixel. At each point, you could check the pixel against the value in the background's PixelGrabber data and detonate the ship if you found any color other than blue.
    Incidently, is your terrain solid or wireframe? Again, if it's wireframe, you could forget the pixels altogether and just collide the ship's line segments with the terrain's. This would eliminate the need to do pixel-by-pixel checks--you could just check for line intersections (which could be made pretty fast, I think).
    I hope this makes sense. If not, post again and I'll try to be more clear.
    Good luck!

    Kreitler

    : Hi!
    :
    : Im currently working on a game (ever tried Zero Gravity on the Amiga 500, or Lunar Lander??). Well, anyway, its a 2D game where you navigate a Lander through treacherous terrain, and caves.
    :
    : Im new to Java so Im learing as I go along, but there is one thing that I havent been able to figure out, and that is how to check for collisions with the background (the montains and rocks) while simultaneously rotating the lander.
    :
    : The way Im checking for collision is by using PixelGrabber to store the pixel layout of the lander object into an array, and then, every frame, the background object checks the position of the lander, grabs the pixels of the background image corresponding to that position, and then compares the two.
    :
    : I have made all the "empty" areas blue (255). As long at the background pixels that are overlapping the lander pixels are blue, nothing happens, but if the color of the pixels are anything but blue, and those overlapp a part of the lander pixels that are not blue, the ship blows up.
    :
    : So the problem Im faceing at the moment is that I need to be able to grab pixels other than in an straight horizontal/vertical way, since the landers coordinates are not fixed in these directions.
    :
    : I have a possible solutions to the problem which I havent tried yet, and that is to have an offscreen image of the background, which I rotate as the ship rotates. This offscreen image would not be used for painting though. But, the easiest way would be to alter the array in the lander object that contains the pixel layout. Or to be able to issue commands through PixelGrabber, to grab a "rotated, rectangle area".
    :
    : Does anyone have a clue how I should procede to solve this problem?
    :
    : Thanks!
    : // [Nano]
    :

  • Hi again, Nano,
    I thought of a better algorithm for your pixel-perfect collisions. It's a lot like the previous one, but doesn't require you to implement Bresenham's algorithm. In fact, it's a lot like the one you're already using.
    I'll assume the following:
    You used a PixelGrabber to store the background terrain in a 2D array (call it worldPixels).
    You have a similar array that contains the pixels for the lander (call it shipPixels).
    The pseudocode for the collision routine looks like this:

    [code]
    for every column in shipPixels {
    for every row in shipPixels {
    get color from shipPixels[row][column]
    if color is not blue {
    transform the current pixel x into the terrain's coordinates.
    transform the current pixel y into the terrain's coordinates.
    look up the terrain pixel color in worldPixels.
    if terrain color is not blue {
    detonate ship
    exit all loops
    }
    }
    } next row
    } next column
    [/code]

    The only tricky bit is transforming the (x, y) values from the ship's coordinate system to that of the terrain. You're already doing this for translation--you just need to add in the rotation. It sounds like you know how to do that, but you haven't put it into the algorithm, yet.

    I hope this helps. If it doesn't make sense, post again and let me know.
    Cheers,

    Kreitler


    : Hi!
    :
    : Im currently working on a game (ever tried Zero Gravity on the Amiga 500, or Lunar Lander??). Well, anyway, its a 2D game where you navigate a Lander through treacherous terrain, and caves.
    :
    : Im new to Java so Im learing as I go along, but there is one thing that I havent been able to figure out, and that is how to check for collisions with the background (the montains and rocks) while simultaneously rotating the lander.
    :
    : The way Im checking for collision is by using PixelGrabber to store the pixel layout of the lander object into an array, and then, every frame, the background object checks the position of the lander, grabs the pixels of the background image corresponding to that position, and then compares the two.
    :
    : I have made all the "empty" areas blue (255). As long at the background pixels that are overlapping the lander pixels are blue, nothing happens, but if the color of the pixels are anything but blue, and those overlapp a part of the lander pixels that are not blue, the ship blows up.
    :
    : So the problem Im faceing at the moment is that I need to be able to grab pixels other than in an straight horizontal/vertical way, since the landers coordinates are not fixed in these directions.
    :
    : I have a possible solutions to the problem which I havent tried yet, and that is to have an offscreen image of the background, which I rotate as the ship rotates. This offscreen image would not be used for painting though. But, the easiest way would be to alter the array in the lander object that contains the pixel layout. Or to be able to issue commands through PixelGrabber, to grab a "rotated, rectangle area".
    :
    : Does anyone have a clue how I should procede to solve this problem?
    :
    : Thanks!
    : // [Nano]
    :

  • Hi Kreitler!

    Im using solid sprite objects for both the lander, and the background,
    and a one dimensional array for storing the pixel layout. Havent really thought about using a 2D array since the PixelGrabber only accepts a one dimensional array as a parameter, but using a 2D array is closer to reality and more logical. Thanks! =)

    The PixelGrabber object has three constructers, two of which take a one dimensional array as one of the parameters. So, in order to translate the one dimensional array that is processed by PixelGrabber and grabPixels(), into a 2D array, I have to create a new empty 2D array, and then initialize it with the values from the original one dimensional array, by using the known width and height of the image, right?

    The same goes for the background. Then I would have two 2D arrays to compare with eachother. But, as you write yourself, the tricky part is to rotate the 2D array, which I, for the moment have no clue how to do.
    I use a varible which I call "angle" in the Lander-object, which determines, as you might have guessed, the angle of the sprite that will be draw to the screen.

    I use the following code to manipulate the "angle" variable of the lander object (The code isent optimized and I intend to rewrite it as soon as this problem has been solved).

    // Updates the location of the lander
    public void updateLocation(){

    double dx, dy;
    dx = -Math.sin(angle);
    dy = Math.cos(angle);

    if(mainThrusterOn){
    deltaX -= dx;
    deltaY -= dy;
    }else{
    deltaY += gravity; // Gravity, if any, pulling the ship downwards
    }

    if(leftThrusterOn){
    angle -= Math.PI / 32;
    if(angle < 0)
    angle += 2 * Math.PI;
    }

    if(rightThrusterOn){
    angle += Math.PI / 32;
    if(angle > 2 * Math.PI)
    angle -= 2 * Math.PI;
    }

    currentLocationX = getLocation().x;
    currentLocationY = getLocation().y;

    currentLocationX += deltaX;
    currentLocationY += deltaY;

    setLocation(currentLocationX, currentLocationY);

    }

    Then, in the paint method, I have the following code.

    public void paint(Graphics2D g2D){

    AffineTransform at = new AffineTransform();
    at.rotate(angle, 15.0, 35.0); // Here is where I rotate the image.
    biop = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);

    g2D.drawImage(buffImages[currentFrame], biop,
    getSpaceOccupied().x, getSpaceOccupied().y);

    }

    If I were to use a bounding box defined by the Rectangle variable "spaceOccupied", and use that for collision detection, not only would that result in inaccurate detection but also, as is, im not rotating the Rectangle object, which means that it is positioned in a continuous upright position.

    I really need to be able to rotate that shipPixel array.

    Thanks for the pseudocode, appreciate it! <)
    // [Nano]



    : Hi again, Nano,
    : I thought of a better algorithm for your pixel-perfect collisions. It's a lot like the previous one, but doesn't require you to implement Bresenham's algorithm. In fact, it's a lot like the one you're already using.
    : I'll assume the following:
    : You used a PixelGrabber to store the background terrain in a 2D array (call it worldPixels).
    : You have a similar array that contains the pixels for the lander (call it shipPixels).
    : The pseudocode for the collision routine looks like this:
    :
    : [code]
    : for every column in shipPixels {
    : for every row in shipPixels {
    : get color from shipPixels[row][column]
    : if color is not blue {
    : transform the current pixel x into the terrain's coordinates.
    : transform the current pixel y into the terrain's coordinates.
    : look up the terrain pixel color in worldPixels.
    : if terrain color is not blue {
    : detonate ship
    : exit all loops
    : }
    : }
    : } next row
    : } next column
    : [/code]
    :
    : The only tricky bit is transforming the (x, y) values from the ship's coordinate system to that of the terrain. You're already doing this for translation--you just need to add in the rotation. It sounds like you know how to do that, but you haven't put it into the algorithm, yet.
    :
    : I hope this helps. If it doesn't make sense, post again and let me know.
    : Cheers,
    :
    : Kreitler
    :
    :
    : : Hi!
    : :
    : : Im currently working on a game (ever tried Zero Gravity on the Amiga 500, or Lunar Lander??). Well, anyway, its a 2D game where you navigate a Lander through treacherous terrain, and caves.
    : :
    : : Im new to Java so Im learing as I go along, but there is one thing that I havent been able to figure out, and that is how to check for collisions with the background (the montains and rocks) while simultaneously rotating the lander.
    : :
    : : The way Im checking for collision is by using PixelGrabber to store the pixel layout of the lander object into an array, and then, every frame, the background object checks the position of the lander, grabs the pixels of the background image corresponding to that position, and then compares the two.
    : :
    : : I have made all the "empty" areas blue (255). As long at the background pixels that are overlapping the lander pixels are blue, nothing happens, but if the color of the pixels are anything but blue, and those overlapp a part of the lander pixels that are not blue, the ship blows up.
    : :
    : : So the problem Im faceing at the moment is that I need to be able to grab pixels other than in an straight horizontal/vertical way, since the landers coordinates are not fixed in these directions.
    : :
    : : I have a possible solutions to the problem which I havent tried yet, and that is to have an offscreen image of the background, which I rotate as the ship rotates. This offscreen image would not be used for painting though. But, the easiest way would be to alter the array in the lander object that contains the pixel layout. Or to be able to issue commands through PixelGrabber, to grab a "rotated, rectangle area".
    : :
    : : Does anyone have a clue how I should procede to solve this problem?
    : :
    : : Thanks!
    : : // [Nano]
    : :
    :
    :

  • [b][red]This message was edited by kreitler at 2003-3-28 13:19:20[/red][/b][hr]
    Hi Nano,
    Ahh...the infamous AffineTransform. I forgot Java 2 had those. Unfortunately, they hide the math you'll need to rotate your pixel data for collision purposes. No problem, though--we'll just put it explicitly into the code!
    Here's a routine that should work to rotate your pixels. You might need to fiddle with it, depending on that axis from which you measure your angle of rotation, etc.

    [code]
    /** Returns 'true' if the lander collides with background pixels.
    * Assumes lander data is stored in an array 'landerPixels' (2D)
    * and terrain data is in 'terrainPixels' (2D). Further assumes that
    * pixel color data is compressed into a single 'int', which is '0'
    * for empty squares and non-zero for full squares.
    * @param theta -- angle at which the lander is rotated.
    * @return 'true' of any lander pixel overlaps and terrain pixel.
    */
    public boolean DidCollide(float theta) {
    boolean bHit = false;

    // Precalculate cos and sin (used to rotate pixel data).
    float cosTheta = (float)Math.cos(theta);
    float sinTheta = (float)Math.sin(theta);

    // Get position of lander's coordinate frame in the world.
    int landerWorldX = getLocation().x;
    int landerWorldY = getLocation().y;

    // Get offsets to the center of the lander.
    int landerMidX = landerPixels[0].length / 2;
    int landerMidY = landerPixels.length / 2;

    // Loop through lander rows and columns.
    for (int row=0; row 2 * Math.PI)
    : angle -= 2 * Math.PI;
    : }
    :
    : currentLocationX = getLocation().x;
    : currentLocationY = getLocation().y;
    :
    : currentLocationX += deltaX;
    : currentLocationY += deltaY;
    :
    : setLocation(currentLocationX, currentLocationY);
    :
    : }
    :
    : Then, in the paint method, I have the following code.
    :
    : public void paint(Graphics2D g2D){
    :
    : AffineTransform at = new AffineTransform();
    : at.rotate(angle, 15.0, 35.0); // Here is where I rotate the image.
    : biop = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
    :
    : g2D.drawImage(buffImages[currentFrame], biop,
    : getSpaceOccupied().x, getSpaceOccupied().y);
    :
    : }
    :
    : If I were to use a bounding box defined by the Rectangle variable "spaceOccupied", and use that for collision detection, not only would that result in inaccurate detection but also, as is, im not rotating the Rectangle object, which means that it is positioned in a continuous upright position.
    :
    : I really need to be able to rotate that shipPixel array.
    :
    : Thanks for the pseudocode, appreciate it! <)
    : // [Nano]
    :
    :
    :
    : : Hi again, Nano,
    : : I thought of a better algorithm for your pixel-perfect collisions. It's a lot like the previous one, but doesn't require you to implement Bresenham's algorithm. In fact, it's a lot like the one you're already using.
    : : I'll assume the following:
    : : You used a PixelGrabber to store the background terrain in a 2D array (call it worldPixels).
    : : You have a similar array that contains the pixels for the lander (call it shipPixels).
    : : The pseudocode for the collision routine looks like this:
    : :
    : : [code]
    : : for every column in shipPixels {
    : : for every row in shipPixels {
    : : get color from shipPixels[row][column]
    : : if color is not blue {
    : : transform the current pixel x into the terrain's coordinates.
    : : transform the current pixel y into the terrain's coordinates.
    : : look up the terrain pixel color in worldPixels.
    : : if terrain color is not blue {
    : : detonate ship
    : : exit all loops
    : : }
    : : }
    : : } next row
    : : } next column
    : : [/code]
    : :
    : : The only tricky bit is transforming the (x, y) values from the ship's coordinate system to that of the terrain. You're already doing this for translation--you just need to add in the rotation. It sounds like you know how to do that, but you haven't put it into the algorithm, yet.
    : :
    : : I hope this helps. If it doesn't make sense, post again and let me know.
    : : Cheers,
    : :
    : : Kreitler
    : :
    : :
    : : : Hi!
    : : :
    : : : Im currently working on a game (ever tried Zero Gravity on the Amiga 500, or Lunar Lander??). Well, anyway, its a 2D game where you navigate a Lander through treacherous terrain, and caves.
    : : :
    : : : Im new to Java so Im learing as I go along, but there is one thing that I havent been able to figure out, and that is how to check for collisions with the background (the montains and rocks) while simultaneously rotating the lander.
    : : :
    : : : The way Im checking for collision is by using PixelGrabber to store the pixel layout of the lander object into an array, and then, every frame, the background object checks the position of the lander, grabs the pixels of the background image corresponding to that position, and then compares the two.
    : : :
    : : : I have made all the "empty" areas blue (255). As long at the background pixels that are overlapping the lander pixels are blue, nothing happens, but if the color of the pixels are anything but blue, and those overlapp a part of the lander pixels that are not blue, the ship blows up.
    : : :
    : : : So the problem Im faceing at the moment is that I need to be able to grab pixels other than in an straight horizontal/vertical way, since the landers coordinates are not fixed in these directions.
    : : :
    : : : I have a possible solutions to the problem which I havent tried yet, and that is to have an offscreen image of the background, which I rotate as the ship rotates. This offscreen image would not be used for painting though. But, the easiest way would be to alter the array in the lander object that contains the pixel layout. Or to be able to issue commands through PixelGrabber, to grab a "rotated, rectangle area".
    : : :
    : : : Does anyone have a clue how I should procede to solve this problem?
    : : :
    : : : Thanks!
    : : : // [Nano]
    : : :
    : :
    : :
    :
    :



  • Thanks for your help Kreitler, that code really hit the spot! <)

    Kind regards
    [Nano]


    : [b][red]This message was edited by kreitler at 2003-3-28 13:19:20[/red][/b][hr]
    : Hi Nano,
    : Ahh...the infamous AffineTransform. I forgot Java 2 had those. Unfortunately, they hide the math you'll need to rotate your pixel data for collision purposes. No problem, though--we'll just put it explicitly into the code!
    : Here's a routine that should work to rotate your pixels. You might need to fiddle with it, depending on that axis from which you measure your angle of rotation, etc.
    :
    : [code]
    : /** Returns 'true' if the lander collides with background pixels.
    : * Assumes lander data is stored in an array 'landerPixels' (2D)
    : * and terrain data is in 'terrainPixels' (2D). Further assumes that
    : * pixel color data is compressed into a single 'int', which is '0'
    : * for empty squares and non-zero for full squares.
    : * @param theta -- angle at which the lander is rotated.
    : * @return 'true' of any lander pixel overlaps and terrain pixel.
    : */
    : public boolean DidCollide(float theta) {
    : boolean bHit = false;
    :
    : // Precalculate cos and sin (used to rotate pixel data).
    : float cosTheta = (float)Math.cos(theta);
    : float sinTheta = (float)Math.sin(theta);
    :
    : // Get position of lander's coordinate frame in the world.
    : int landerWorldX = getLocation().x;
    : int landerWorldY = getLocation().y;
    :
    : // Get offsets to the center of the lander.
    : int landerMidX = landerPixels[0].length / 2;
    : int landerMidY = landerPixels.length / 2;
    :
    : // Loop through lander rows and columns.
    : for (int row=0; row<landerPixels.length; row++) {
    : // Get y-pixel location in lander's coordinate frame.
    : float yLocal = (float)(row - landerMidY);
    :
    : for (int col=0; col<landerPixels[0].length; col++) {
    : // Get x-pixel location in lander's coordinate frame.
    : float xLocal = (float)(col - landerMidX);
    :
    : // Skip this check if the lander has no pixel data.
    : if (landerPixels[row][col] == 0) continue;
    :
    : // Rotate the location of the current pixel in the lander's frame.
    : float xRot = xLocal * cosTheta - yLocal * sinTheta;
    : float yRot = yLocal * cosTheta + xLocal * sinTheta;
    :
    : // Move the locally-rotated point to the correct World location.
    : int worldX = landerX + (int)(xRot + 0.5f);
    : int worldY = landerY + (int)(yRot + 0.5f);
    :
    : try {
    : int pixelVal = worldPixels[worldY][worldX];
    : if (pixelVal) {
    : return bHit = true;
    :
    : // Exit all loops.
    : row = landerPixels.length;
    : break;
    : }
    : }
    : catch (Exception e) { /* Lander pixel is out of world bounds. */
    : }
    : }
    :
    : return bHit;
    : }
    : [/code]
    :
    :
    : I made some assumptions to simplify the code. Most importantly, that the pixel arrays for both the lander and the terrain contain a single int for each pixel, and that the integer is '0' if the pixel is off (non-collidable) and non-zero otherwise. Also, I assumed that the arrays are 2D. You can easily modify the code to work with your 1D arrays and to handle the fact that each element in the array contains one component of the RGBA info.
    :
    : Also, I assumed that getLocation() returns the (x,y) coordinates of the center of the lander sprite. If it returns something else (like the upper left corner), you'll need to adjust the final worldX and worldY points accordingly (for instance, adding landerMidX and landerMidY in to worldX and worldY).
    :
    : I hope this more concrete example helps!
    :
    : Kreitler
    :
    :
    : : Hi Kreitler!
    : :
    : : Im using solid sprite objects for both the lander, and the background,
    : : and a one dimensional array for storing the pixel layout. Havent really thought about using a 2D array since the PixelGrabber only accepts a one dimensional array as a parameter, but using a 2D array is closer to reality and more logical. Thanks! =)
    : :
    : : The PixelGrabber object has three constructers, two of which take a one dimensional array as one of the parameters. So, in order to translate the one dimensional array that is processed by PixelGrabber and grabPixels(), into a 2D array, I have to create a new empty 2D array, and then initialize it with the values from the original one dimensional array, by using the known width and height of the image, right?
    : :
    : : The same goes for the background. Then I would have two 2D arrays to compare with eachother. But, as you write yourself, the tricky part is to rotate the 2D array, which I, for the moment have no clue how to do.
    : : I use a varible which I call "angle" in the Lander-object, which determines, as you might have guessed, the angle of the sprite that will be draw to the screen.
    : :
    : : I use the following code to manipulate the "angle" variable of the lander object (The code isent optimized and I intend to rewrite it as soon as this problem has been solved).
    : :
    : : // Updates the location of the lander
    : : public void updateLocation(){
    : :
    : : double dx, dy;
    : : dx = -Math.sin(angle);
    : : dy = Math.cos(angle);
    : :
    : : if(mainThrusterOn){
    : : deltaX -= dx;
    : : deltaY -= dy;
    : : }else{
    : : deltaY += gravity; // Gravity, if any, pulling the ship downwards
    : : }
    : :
    : : if(leftThrusterOn){
    : : angle -= Math.PI / 32;
    : : if(angle < 0)
    : : angle += 2 * Math.PI;
    : : }
    : :
    : : if(rightThrusterOn){
    : : angle += Math.PI / 32;
    : : if(angle > 2 * Math.PI)
    : : angle -= 2 * Math.PI;
    : : }
    : :
    : : currentLocationX = getLocation().x;
    : : currentLocationY = getLocation().y;
    : :
    : : currentLocationX += deltaX;
    : : currentLocationY += deltaY;
    : :
    : : setLocation(currentLocationX, currentLocationY);
    : :
    : : }
    : :
    : : Then, in the paint method, I have the following code.
    : :
    : : public void paint(Graphics2D g2D){
    : :
    : : AffineTransform at = new AffineTransform();
    : : at.rotate(angle, 15.0, 35.0); // Here is where I rotate the image.
    : : biop = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
    : :
    : : g2D.drawImage(buffImages[currentFrame], biop,
    : : getSpaceOccupied().x, getSpaceOccupied().y);
    : :
    : : }
    : :
    : : If I were to use a bounding box defined by the Rectangle variable "spaceOccupied", and use that for collision detection, not only would that result in inaccurate detection but also, as is, im not rotating the Rectangle object, which means that it is positioned in a continuous upright position.
    : :
    : : I really need to be able to rotate that shipPixel array.
    : :
    : : Thanks for the pseudocode, appreciate it! <)
    : : // [Nano]
    : :
    : :
    : :
    : : : Hi again, Nano,
    : : : I thought of a better algorithm for your pixel-perfect collisions. It's a lot like the previous one, but doesn't require you to implement Bresenham's algorithm. In fact, it's a lot like the one you're already using.
    : : : I'll assume the following:
    : : : You used a PixelGrabber to store the background terrain in a 2D array (call it worldPixels).
    : : : You have a similar array that contains the pixels for the lander (call it shipPixels).
    : : : The pseudocode for the collision routine looks like this:
    : : :
    : : : [code]
    : : : for every column in shipPixels {
    : : : for every row in shipPixels {
    : : : get color from shipPixels[row][column]
    : : : if color is not blue {
    : : : transform the current pixel x into the terrain's coordinates.
    : : : transform the current pixel y into the terrain's coordinates.
    : : : look up the terrain pixel color in worldPixels.
    : : : if terrain color is not blue {
    : : : detonate ship
    : : : exit all loops
    : : : }
    : : : }
    : : : } next row
    : : : } next column
    : : : [/code]
    : : :
    : : : The only tricky bit is transforming the (x, y) values from the ship's coordinate system to that of the terrain. You're already doing this for translation--you just need to add in the rotation. It sounds like you know how to do that, but you haven't put it into the algorithm, yet.
    : : :
    : : : I hope this helps. If it doesn't make sense, post again and let me know.
    : : : Cheers,
    : : :
    : : : Kreitler
    : : :
    : : :
    : : : : Hi!
    : : : :
    : : : : Im currently working on a game (ever tried Zero Gravity on the Amiga 500, or Lunar Lander??). Well, anyway, its a 2D game where you navigate a Lander through treacherous terrain, and caves.
    : : : :
    : : : : Im new to Java so Im learing as I go along, but there is one thing that I havent been able to figure out, and that is how to check for collisions with the background (the montains and rocks) while simultaneously rotating the lander.
    : : : :
    : : : : The way Im checking for collision is by using PixelGrabber to store the pixel layout of the lander object into an array, and then, every frame, the background object checks the position of the lander, grabs the pixels of the background image corresponding to that position, and then compares the two.
    : : : :
    : : : : I have made all the "empty" areas blue (255). As long at the background pixels that are overlapping the lander pixels are blue, nothing happens, but if the color of the pixels are anything but blue, and those overlapp a part of the lander pixels that are not blue, the ship blows up.
    : : : :
    : : : : So the problem Im faceing at the moment is that I need to be able to grab pixels other than in an straight horizontal/vertical way, since the landers coordinates are not fixed in these directions.
    : : : :
    : : : : I have a possible solutions to the problem which I havent tried yet, and that is to have an offscreen image of the background, which I rotate as the ship rotates. This offscreen image would not be used for painting though. But, the easiest way would be to alter the array in the lander object that contains the pixel layout. Or to be able to issue commands through PixelGrabber, to grab a "rotated, rectangle area".
    : : : :
    : : : : Does anyone have a clue how I should procede to solve this problem?
    : : : :
    : : : : Thanks!
    : : : : // [Nano]
    : : : :
    : : :
    : : :
    : :
    : :
    :
    :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories