Public Group

# Wrap-around world in OpenGL

This topic is 4960 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all ! I hope that you have already played to Asteroids ! Imagine that I want to make the same game in OpenGL: - asteroids are represented by spheres - the ship is represented by a box - the gameboard is represented by a big square Now, if an asteroid leave the square from the bottom, it should reappear from the top of the square, right ? Ok, but if one half of the sphere is at the bottom, the other half would be at the top of the square. How can I do that ? In 2D there is modulo, but in 3D I don't think the method is the same...

##### Share on other sites
I think what you want to do is define a 3D world (your game is 3D right?) of a certain size, let's say 100 units..

let x,y, and z all range from 0-100

psuedocode:

if x > 100
x = 0
if x < 0
x = 100

repeat for y and z.

##### Share on other sites
While a sphere is at the bottom (partially) spawn a new sphere ar the top, then when the bottom sphere is totally out of screen, remove it, and let only the top sphere.

##### Share on other sites
Of course you can use modulo in 3d.

I wrote an asteroids game in Java, with wrap-around. Here is the relevant part of the source:

package javagame;
import allegroj.*;

/**
* An abstract class which implements an object which wraps around the
* screen, i.e. goes off the top and comes back on the bottom.
*
* Additionally this implements entities with intertia.
*/

public abstract class WrapAroundEntity extends Entity implements Drawable
{
protected int dx,dy;
private int dxerror=0,dyerror=0; // error from last loop
public static final int SCALINGFACTOR=8;

public WrapAroundEntity(GameManager gm) {
super(gm);
}

/**
* This is the drawing method. If it is close to the edge of the screen,
* we have to call drawInstance() several times to make the object appear
* in different places.
*/

private void drawVert(Bitmap bm,int x, int y) {
drawInstance(bm,x,y);
drawInstance(bm,x,y-gameman.screenh);
}
drawInstance(bm,x,y+gameman.screenh);
}
}

public void draw(Bitmap bm) {
drawVert(bm,x,y);
drawVert(bm,x-gameman.screenw,y);
}
drawVert(bm,x+gameman.screenw,y);
}
}

void move(EntityManager entman) {
dxerror += dx;
dyerror += dy;
x+= (dxerror / SCALINGFACTOR);
y+= (dyerror / SCALINGFACTOR);
dxerror = dxerror %SCALINGFACTOR;
dyerror = dyerror %SCALINGFACTOR;
if (x<0) x+= gameman.screenw;
if (y<0) y+= gameman.screenh;
if (x>=gameman.screenw) x-= gameman.screenw;
if (y>=gameman.screenh) y-= gameman.screenh;
}

protected abstract void drawInstance(Bitmap bm,int x, int y);

}

In this case, I've subclassed the Abstract Entity class (and implemented Drawable). The subclasses of WrapAroundEntity implement drawInstance rather than draw, and it's called up to four times, depending on the location of the object.

gameman.screenw and screenh are the screen width and height respectively.

Mark

##### Share on other sites
Wait... is that java or C++? You did say Java right? I guess since it is(I think) I will ask if I should learn Java after C++. Or would it be a waste of my time. And no, I will not convert to Java now. I have almost gotten down OOP.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634060
• Total Posts
3015300
×