It’s 1:32 in the morning and I’m not amazing at programming. Does anyone have any idea what I should do to make
if (Mouse.isButtonDown(0)) {
int mx = Mouse.getX();
int my = Display.getHeight() - Mouse.getY();
if (Math.sqrt(Math.pow((mx - cx), 2) + Math.pow((my - cy), 2)) <= 150){
GL11.glColor3f(0.0f, 1.0f, 0.0f);
GL11.glLineWidth(2.5f);
GL11.glBegin(GL11.GL_LINE_STRIP);
GL11.glVertex2f(mx, my);
GL11.glVertex2f(cx + 8, cy + 8);
GL11.glEnd();
}
work so that if you leave the 150 distance boundary you’d still draw the line, in that direction, but stopping after that distance?
I thought about using slope but slope doesn’t really work in a non 0,0 centered plane. Basically I’m a tired newb, and I’d appreciate some help on how I need to set this up.
Oh, and most of this code is from guides, yes I understand it, I am trying to learn as well, so it might not be optimal, and I didn’t write all of it, and I don’t understand all aspects of LWJGL, that is of course why I’m asking for help here. Or maybe that’s just my lack of algebra skills.
Extra info: The area I have rendered is 512 x 512.
The rest of the code:
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
public class Tile {
static int cx = 248;
static int cy = 248;
static int mDelta = 1;
/**
* Start the example
*/
public void start() {
initGL(512,512);
while (true) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
render();
pollInput();
update();
Display.update();
Display.sync(60);
if (Display.isCloseRequested()) {
Display.destroy();
System.exit(0);
}
}
}
/**
* Initialize the GL display
*
* @param width The width of the display
* @param height The height of the display
*/
private void initGL(int width, int height) {
try {
Display.setDisplayMode(new DisplayMode(width,height));
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// enable alpha blending
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0,width,height);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
/**
* draw a room with openings
*/
public void render() {
for(int y = 0; y < 32; y += 1){
for(int x = 0; x < 32; x += 1) {
if(((x >= 14 && x <= 17) && (y == 0 || y == 31))||((x == 0 || x == 31) && (y >= 14 && y <= 17))){
Color.black.bind();
}else if(((x < 14 || x > 17) && (y == 0 || y == 31))||((x == 0 || x == 31) && (y < 14 || y > 17))){
Color.darkGray.bind();
}else{
Color.white.bind();
}
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f((x * 16), (y * 16));
GL11.glVertex2f((x * 16), 16 + (y * 16));
GL11.glVertex2f(16 + (x * 16), 16 + (y * 16));
GL11.glVertex2f(16 + (x * 16), (y * 16));
GL11.glEnd();
}
}
}
public static void pollInput(){
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
if (cy <= 20 && cx >= 216 && cx <= 280){
cy = 248;
cx = 248;
}else if(cy >= 20){
cy -= 4 * mDelta;
}else{
cy += 0;
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
if (cx <= 20 && cy >= 216 && cy <= 280){
cy = 248;
cx = 248;
}else if (cx >= 20){
cx -= 4 * mDelta;
}else{
cx += 0;
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
if (cy >= 476 && cx >= 216 && cx <= 280){
cy = 248;
cx = 248;
}else if (cy <= 476){
cy += 4 * mDelta;
}else{
cy += 0;
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
if (cx >= 476 && cy >= 216 && cy <= 280){
cy = 248;
cx = 248;
}else if (cx <= 476){
cx += 4 * mDelta;
}else{
cx += 0;
}
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
mDelta = 2;
}else if (!Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
mDelta = 1;
}
if (Mouse.isButtonDown(0)) {
int mx = Mouse.getX();
int my = Display.getHeight() - Mouse.getY();
if (Math.sqrt(Math.pow((mx - cx), 2) + Math.pow((my - cy), 2)) <= 150){
GL11.glColor3f(0.0f, 1.0f, 0.0f);
GL11.glLineWidth(2.5f);
GL11.glBegin(GL11.GL_LINE_STRIP);
GL11.glVertex2f(mx, my);
GL11.glVertex2f(cx + 8, cy + 8);
GL11.glEnd();
}
}
}
public static void update(){
Color.magenta.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(cx,cy);
GL11.glVertex2f(cx,cy + 16);
GL11.glVertex2f(cx + 16,cy + 16);
GL11.glVertex2f(cx + 16,cy);
GL11.glEnd();
}
/**
* Main Class
*/
public static void main(String[] argv) {
Tile tile = new Tile();
tile.start();
}
}