Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


stein102

Member Since 13 Aug 2010
Offline Last Active Jun 10 2015 05:26 PM

Topics I've Started

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;
    } 
}


Rendering sprite on isometric tilemap (LibGDX)

21 November 2014 - 04:46 PM

So I'm trying to create a isometric style action-RPG type game(similar to Diablo 1 and 2). I've got an isometric tiled map to render to the screen but the problem is getting a character to render on a specific square on the game map. I've tried reading a few articles and implementing their solutions but it doesn't seem to be working.  Below is my code(Keep in mind this is just a prototype).

public class MapManager {

	public static int TILE_WIDTH = 64;
	public static int TILE_HEIGHT = 32;
	
	private TiledMap map;
	private IsometricTiledMapRenderer renderer;

	public MapManager() {
		
		map = new TmxMapLoader().load("testMap.tmx");
		
		renderer = new IsometricTiledMapRenderer(map);
	}
	
	public void render(){
		renderer.render();
		renderer.setView(D2C.camera);
	}

}
public class Player {

	Vector2 pos;
	float velocity;
	Sprite sprite;
	
	
	public Player() {
		sprite = new Sprite(new Texture(Gdx.files.internal("player.png")));
		velocity = 5f;
		pos = new Vector2(0,38);
	}

}
public class D2C extends Game{
	public static OrthographicCamera camera;
	Player player;
	SpriteBatch batch;
	MapManager mapManager;

	
	@Override
	public void create () {
		batch = new SpriteBatch();
		
		player = new Player();
		
		camera = new OrthographicCamera();
		
		mapManager = new MapManager();
		
		Gdx.input.setInputProcessor(new InputHandler());
	}

	@Override
	public void render () {
		Gdx.gl.glClearColor(0, 0, 0, 0);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);		
		
		Vector2 pos = MapUtil.cartToIso(player.pos);

		mapManager.render();
		
		batch.begin();
			batch.draw(player.sprite,pos.x,pos.y);
		batch.end();
	}
	
	public void resize(int width, int height){
		camera.viewportHeight = height;
		camera.viewportWidth = width;
		camera.update();
	}
}
public class MapUtil {

	
	public static Vector2 cartToIso(Vector2 initialPosition){
		Vector2 iso = new Vector2();
		iso.x = (initialPosition.x - initialPosition.y)*MapManager.TILE_WIDTH;
		iso.y = (initialPosition.x + initialPosition.y)*MapManager.TILE_HEIGHT;
		return iso;
	}
}

Quest scripting for 2D RPG

19 June 2014 - 11:31 AM

Hey there, I've been working on a 2D RPG using Java and the LibGDX framework. I have a working quest system right now using XML for quest data but it seems to be fairly inflexible and I want to have more flexibility in my game. I've seen a lot about using scripting langauges such as lua or Python for quest scripting. So I have a few questions.

 

1) How should one scructure a script that describes a quest? For example say you need to collect 10 of some Item and return to a specific NPC to complete the quest. After completion you get some reward.

 

2) What are the pros/cons of using LuaJ/Jython? I'm not familiar with either language so I have no bias towards either of them.

 

3) How difficult would it be for a script to interact with a java object, call Java methods, etc?

 

Any suggestions or links to references would be greatly apprecated.

Thanks.


PARTNERS