Jump to content

  • Log In with Google      Sign In   
  • Create Account

stein102

Member Since 13 Aug 2010
Offline Last Active Jan 25 2016 04:42 PM

Topics I've Started

Debug my bubble sort algorithm?

22 December 2015 - 01:00 AM

I'm getting an error in my game where my foreground is not rendering on top of my background. My idea is that maybe I messed up my layer sorting method. Does anything look wrong with this?

    public static ArrayList<MapLayer> sortLayers(ArrayList<MapLayer> layers){
        ArrayList<MapLayer> sortedLayers;
        sortedLayers = layers;
        int n = sortedLayers.size();
        MapLayer temp = null;
        
        for(int i = 0; i < n; i++){
            for(int j = 1; j < (n-1); j++){
                if(sortedLayers.get(j-1).getDepth() > sortedLayers.get(j).getDepth()){
                    temp = sortedLayers.get(j-1);
                    sortedLayers.set(j-1,sortedLayers.get(j));
                    sortedLayers.set(j,temp);
                }
            }
        }
    }

Tile picking with moving camera

03 September 2015 - 06:38 PM

I've been working on a isometric tilemap engine. Everything has been working fine up until recently when I tried implementing a moving camera. The tile picking has been off if the camera is moved. I've tried offsetting by the camera's displacement and it's still not giving the desired results. If I only move the camera along the x-axis, the tile picking works as intended, if any y-axis movement is involved, it doesn't behave correctly. I'm not sure what's going on here. Any help would be appreciated.

 

This method is called when a place on the game world is clicked.

There is no correction for the camera in this method at the moment, I took it out due to it not working correctly. It's simply the base method that worked before the moving camera was implemented.

The first two lines of code give me the camera's displacement along a given axis. cam.viewportWidth/2 is the default camera x coordinate. This is also applied to the Y-Axis.

 

If you need any more information, let me know and I can supply it.

    public void getTileAt(OrthographicCamera cam, Vector2 mouse){
        camOffsetX = cam.viewportWidth/2-cam.position.x;
        camOffsetY = -(cam.viewportHeight/2-cam.position.y);
        
        System.out.println(camOffsetX + ":" + camOffsetY);
        
        mouse.x -= X_OFFSET;
        mouse.y = Math.abs(mouse.y - Gdx.graphics.getHeight());
        
        Vector2 mp = isoToCart(mouse);
        Vector2 coords = new Vector2((float)Math.floor(mp.x), (float)Math.floor(mp.y));

                
        try{
           Tile t = new Tile("cube","redcube.png");
           layers.get(0).setTileAt(t, (int)coords.x, (int)coords.y);
            
            System.out.println(coords);
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println("Coords out of bounds");
        }
    }

Platformer movement and collision

24 May 2015 - 10:27 PM

I'm working on getting a platformer prototype up and running in C# using monogame. The movement and collision "work" but are very awkward. For example, if you stand on the edge of a platform and jump straight up, you fall right through the platform. I'll post my code and hopefully someone sees something that doesn't look right. If any additional information is needed I can add more.

public Player(GraphicsDevice device)
        {
            x = 100;
            y = 100;
            width = 32;
            height = 64;
            maxVX = 10;
            gravity = -2;
            grounded = false;

            Rect = new Rectangle(x, y, width, height);

            WhiteRectangle = new Texture2D(device, 1, 1);
            WhiteRectangle.SetData(new[] { Color.White });
        }



        public void Update()
        {
            rect = new Rectangle(x, y, width, height);
            if (!grounded || standingOn==null)
            {
                aY = gravity;
            }else
            {
                aY = 0;
            }

                

            VY += AY;
            VX += AX;

            if (aX > 10) aX = 10;
            if (aX < -10) aX = -10;
            if (vX > maxVX) vX = maxVX;
            if (vX < -maxVX) vX = -maxVX;




            System.Console.WriteLine(vX +":" + aX);

            lastX = x;
            lastY = y;

            x += VX;
            y -= VY;

        }

        public void CollisionCheck(List<Platform> collideables)
        {
            foreach(var p in collideables)
            {
                if (rect.Intersects(p.Rect))
                {
                    Rectangle intersection = Rectangle.Intersect(rect, p.Rect);

                    if(intersection.Height < intersection.Width)
                    {
                        if (y < p.Y)
                        {
                            standingOn = p;
                            grounded = true;
                            y = p.Y - height;
                            vY = 0;
                            aY = 0;
                        }else
                        {
                            y = p.Y + p.Height + 1;
                            vY = 0;
                            
                        }



                    }else
                    {
                        if(x < p.X)
                        {
                            x = p.X - width - 1;
                        }else
                        {
                            x = p.X + p.Width + 1;
                        }
                    }
                
                    vX = 0;
                    aX = 0;
                }
            }

            //Check if player falls off platform
            if (standingOn != null)
            {
                if(x+width < standingOn.X || x > standingOn.X +standingOn.Width)
                {
                    grounded = false;
                    standingOn = null;
                }
            }
        }

        public void ApplyFriction()
        {
            aX = 0;
            if (vX > 0)
                aX -= 2;
            if (vX < 0)
                aX += 2;
        }

EDIT: Almost forgot to include the code that changes the acceleration

            player.AX += (int)(pad.ThumbSticks.Left.X * 10) / 5;

Is Xbox360 live arcade development still worth it?

23 May 2015 - 09:41 PM

So I recently started playing around with C# and Monogame. The question I have is pretty simple, would it be worth it to work on releasing a game for the xbox360 live arcade? Worth while as in, would there be a large enough demographic that still plays indie games on live arcade to release a game to that as opposed to another platform?

 

Thanks,

Stein102


Problem with 2D isometric tile picking

19 March 2015 - 05:24 AM

Right now I'm attempting to build an engine for rendering a tile based map with an isometric projection. Rendering the map seems to be working now but at the moment, I'm frustrated trying to figure out how to select a tile. For normal 2D this is simple, but I can't seem to figure out how this needs to be done. Since an isometric tile is a squished diamond shape, you need to find out if the click was actually on the tile, or one of the adjacent tiles. I've tried a bunch of methods such as checking if the click was in the polygon, then seeing which quadrant the click was in if the test failed. I've also tried checking if the click was in the polygon, then if the click was outside to check if the click was inside any of the eight adjacent tiles. I really don't know where to go with this so I'll post my code here.

 

Any suggestions would be appreciated.

Thanks.

package com.isoTest;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import java.awt.Point;
import java.awt.Polygon;

public class Map {

    
    private int[][] data;
    private Texture img, img2;
    private int TILE_HEIGHT = 32;
    private int TILE_WIDTH = 64;
    private int TILE_HEIGHT_HALF = TILE_HEIGHT/2;
    private int TILE_WIDTH_HALF = TILE_WIDTH/2;
    private int MAP_WIDTH;
    private int MAP_HEIGHT;
    private int X_OFFSET;
    
    public Map() {
        img = new Texture(Gdx.files.internal("isograss1.png"));
        img2 = new Texture(Gdx.files.internal("redcube.png"));
        data = new int[][]{{1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1},
                          {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                          {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                          {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                          {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                          {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},         
                          {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                          {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                          {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                          {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
        
 
        
       
            MAP_WIDTH = data[0].length*TILE_WIDTH;
            MAP_HEIGHT = (data[0].length*TILE_HEIGHT/2)+(16*data.length);
            X_OFFSET = ((data.length-1)*TILE_HEIGHT);
    }
    
    public Vector2 cartToIso(Vector2 init){
        Vector2 iso = new Vector2();
        iso.x = (((init.x - init.y) * TILE_WIDTH/2))+X_OFFSET;
        iso.y = MAP_HEIGHT-TILE_HEIGHT-((init.x + init.y) * TILE_HEIGHT/2);  
        return iso;  
    }
    
    public Vector2 cartToIso(int x, int y){
        Vector2 iso = new Vector2();
        iso.x = (((x - y) * TILE_WIDTH/2))+X_OFFSET;
        iso.y = MAP_HEIGHT-TILE_HEIGHT-((x + y) * TILE_HEIGHT/2); 
        return iso;  
    }
    
    public Vector2 isoToCart(Vector2 init){
        Vector2 cart = new Vector2();
        init.x = init.x-X_OFFSET;
        init.y = init.y-MAP_HEIGHT;
        cart.x = (init.x / (TILE_WIDTH/2) + init.y / (TILE_HEIGHT/2)) /2;
        cart.y = (init.y / (TILE_HEIGHT/2) -(init.x / (TILE_WIDTH/2))) /2;
        return cart;
    }
    
    public Vector2 isoToCart(int x, int y){
        Vector2 cart = new Vector2();
        x = x-X_OFFSET;
        y = y-MAP_HEIGHT;
        cart.x = (x / (TILE_WIDTH/2) + y / (TILE_HEIGHT/2)) /2;
        cart.y = (y / (TILE_HEIGHT/2) -(x / (TILE_WIDTH/2))) /2;
        return cart;
    }
    
    public void render(SpriteBatch batch){
        for(int i = 0; i < data.length; i++){
            for(int j = 0; j < data[i].length;j++){
                if(data[i][j] == 0){
                    Vector2 coords = cartToIso(j, i);
                    batch.draw(img,coords.x,coords.y);
                }else if(data[i][j] == 1){
                    Vector2 coords = cartToIso(j, i);
                    batch.draw(img2,coords.x,coords.y);
                }
            }
        }
        
    }
    /**
     * @param clickX mouse position x
     * @param clickY mouse position y
     */
    public void getTileAt(int clickX, int clickY){
        Vector2 coords = isoToCart(clickX,clickY);
        System.out.println("Isometric coordinates: "+coords.x+","+coords.y);
        
        //get actual tile for reference
        Vector2 tileCoords = cartToIso(coords);
        int centerX = (int)tileCoords.x + (TILE_WIDTH/2);
        int centerY = (int)tileCoords.y + (TILE_HEIGHT/2);
        
        
        Point p = new Point(clickX,Gdx.graphics.getHeight()-clickY);
        Point p1 = new Point(centerX,centerY+TILE_HEIGHT_HALF);
        Point p2 = new Point(centerX-TILE_WIDTH_HALF,centerY);
        Point p3 = new Point(centerX,centerY-TILE_HEIGHT_HALF);
        Point p4 = new Point(centerX+TILE_WIDTH_HALF,centerY);
        Polygon diamond = new Polygon();
        diamond.addPoint(p1.x,p1.y);
        diamond.addPoint(p2.x,p2.y);
        diamond.addPoint(p3.x,p3.y);
        diamond.addPoint(p4.x,p4.y);
        
        
       System.out.println("Point at: " + p1.x+","+p1.y);
       System.out.println("Point at: " + p2.x+","+p2.y);
       System.out.println("Point at: " + p3.x+","+p3.y);
       System.out.println("Point at: " + p4.x+","+p4.y);
       
       System.out.println(diamond.contains(p));
       if(!diamond.contains(p)){
            if(clickX>centerX){
                    if(clickY>centerY){
                        coords.y--;
                    }else if(clickY < centerY){
                        coords.x++;
                    }
                }else if(clickX < centerX){
                     if(clickY>centerY){
                        coords.x--;
                    }else if(clickY < centerY){
                        coords.y++;
                    }
                }
           

           System.out.println("Correction made!");
           System.out.println("Isometric coordinates: "+coords.x+","+coords.y);
       }
       
       System.out.println("Click: "+clickX+","+(Gdx.graphics.getHeight()-clickY));
 

        if(data[(int)coords.y][(int)coords.x] == 0){
            data[(int)coords.y][(int)coords.x] = 1;
        }else{
            data[(int)coords.y][(int)coords.x] = 0;
        }
    }

    
    

    public int getMAP_WIDTH() {
        return MAP_WIDTH;
    }

    public void setMAP_WIDTH(int MAP_WIDTH) {
        this.MAP_WIDTH = MAP_WIDTH;
    }

    public int getMAP_HEIGHT() {
        return MAP_HEIGHT;
    }

    public void setMAP_HEIGHT(int MAP_HEIGHT) {
        this.MAP_HEIGHT = MAP_HEIGHT;
    } 
}


PARTNERS