Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  What you think of my game until now? How can i make the archer move properly?  (Read 586 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-09-07 17:53:02 »

Heya guys!!!
Well, i have a little gameplay video to show, and hm, i was wondering, how can i make my archer and animals move properly in the slopes??
Heres the movement code i have, its from dermetfan but i changed so much due to my own needs hehe Tongue

So anyway, i need help, at least with theory so i can start drawning ideas and logics in my book Tongue

So heres the video :

http://www.youtube.com/watch?v=_SDAY_vuqeo

And heres the movement code of the archer :


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
 public void update(float delta) {

        if (data.getCurrent_Health() <= 0) {
            alive = false;
        }

        if (alive) {
            // apply gravity
           velocity.y -= gravity * delta;

            // clamp velocity
           if (velocity.y > speed) {
                velocity.y = speed;
            } else if (velocity.y < -speed) {
                velocity.y = -speed;
            }

            // save old position
           float oldX = getX(), oldY = getY(), tileWidth = collisionLayer.getTileWidth(), tileHeight = collisionLayer.getTileHeight();
            boolean collisionX = false, collisionY = false;

            // move on x
           setX(getX() + velocity.x * delta);

            if (velocity.x < 0) { // going left
               // top left
               TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (getX() / tileWidth), (int) ((getY() + getHeight()) / tileHeight));
                if (cell != null) {
                    collisionX = cell.getTile().getProperties().containsKey("blocked");
                }

                // middle left
               if (!collisionX) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) (getX() / tileWidth), (int) ((getY() + getHeight() / 2) / tileHeight));
                    if (cell1 != null) {
                        collisionX = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }

                // bottom left
               if (!collisionX) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) (getX() / tileWidth), (int) (getY() / tileHeight));
                    if (cell1 != null) {
                        collisionX = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }
            } else if (velocity.x > 0) { // going right
               // top right
               TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) ((getX() + getWidth()) / tileWidth), (int) ((getY() + getHeight()) / tileHeight));
                if (cell != null) {
                    collisionX = cell.getTile().getProperties().containsKey("blocked");
                }
                // middle right
               if (!collisionX) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((getX() + getWidth()) / tileWidth), (int) ((getY() + getHeight() / 2) / tileHeight));
                    if (cell1 != null) {
                        collisionX = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }

                // bottom right
               if (!collisionX) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((getX() + getWidth()) / tileWidth), (int) (getY() / tileHeight));
                    if (cell1 != null) {
                        collisionX = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }
            }

            // react to x collision
           if (collisionX) {
                setX(oldX);
                velocity.x = 0;
            }

            // move on y
           setY(getY() + velocity.y * delta);

            if (velocity.y < 0) { // going down
               // bottom left
               TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (getX() / tileWidth), (int) (getY() / tileHeight));
                if (cell != null) {
                    collisionY = cell.getTile().getProperties().containsKey("blocked");
                }
                // bottom middle
               if (!collisionY) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((getX() + getWidth() / 2) / tileWidth), (int) (getY() / tileHeight));
                    if (cell1 != null) {
                        collisionY = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }

                // bottom right
               if (!collisionY) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((getX() + getWidth()) / tileWidth), (int) (getY() / tileHeight));
                    if (cell1 != null) {
                        collisionY = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }

                canJump = collisionY;
            } else if (velocity.y > 0) { // going up
               // top left
               TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (getX() / tileWidth), (int) ((getY() + getHeight()) / tileHeight));
                if (cell != null) {
                    collisionY = cell.getTile().getProperties().containsKey("blocked");
                }

                // top middle
               if (!collisionY) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((getX() + getWidth() / 2) / tileWidth), (int) ((getY() + getHeight()) / tileHeight));
                    if (cell1 != null) {
                        collisionY = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }

                // top right
               if (!collisionY) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((getX() + getWidth()) / tileWidth), (int) ((getY() + getHeight()) / tileHeight));
                    if (cell1 != null) {
                        collisionY = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }
            }

            // react to y collision
           if (collisionY) {
                setY(oldY);
                velocity.y = 0;
            }
        }
       
.....
           
        }
       
    }
Offline Andre Lopes
« Reply #1 - Posted 2013-09-08 00:08:01 »

How can i fix this?
http://img545.imageshack.us/img545/2733/9nmg.jpg

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
private int default_x_width = 38; //40
   private int default_y_height = 40; //44
   

    public void updateMovement(TiledMapTileLayer collisionLayer, Vector2 velocity, float gravity, float delta, float speed) {
        //player.setOrigin(player.getOriginX() + player.getOriginX() / 3, player.getOriginY());
       // apply gravity
       velocity.y -= gravity * delta;

        // clamp velocity
       if (velocity.y > speed) {
            velocity.y = speed;
        } else if (velocity.y < -speed) {
            velocity.y = -speed;
        }

        // save old position
       float oldX = player.getX(), oldY = player.getY(), tileWidth = collisionLayer.getTileWidth(), tileHeight = collisionLayer.getTileHeight();
        boolean collisionX = false, collisionY = false;

        // move on x
       player.setX(player.getX() + velocity.x * delta);

        if (velocity.x < 0) { // going left
           // top left
           TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
            if (cell != null) {
                collisionX = cell.getTile().getProperties().containsKey("blocked");
            }

            // middle left
           if (!collisionX) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) ((player.getY() + default_y_height / 2) / tileHeight));
                if (cell1 != null) {
                    collisionX = cell1.getTile().getProperties().containsKey("blocked");
                }
            }

            // bottom left
           if (!collisionX) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) (player.getY() / tileHeight));
                if (cell1 != null) {
                    collisionX = cell1.getTile().getProperties().containsKey("blocked");
                }
            }
        } else if (velocity.x > 0) { // going right
           // top right
           TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
            if (cell != null) {
                collisionX = cell.getTile().getProperties().containsKey("blocked");
            }
            // middle right
           if (!collisionX) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) ((player.getY() + default_y_height / 2) / tileHeight));
                if (cell1 != null) {
                    collisionX = cell1.getTile().getProperties().containsKey("blocked");
                }
            }

            // bottom right
           if (!collisionX) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) (player.getY() / tileHeight));
                if (cell1 != null) {
                    collisionX = cell1.getTile().getProperties().containsKey("blocked");
                }
            }
        }

        // react to x collision
       if (collisionX) {
            player.setX(oldX);
            velocity.x = 0;
        }

        // move on y
       player.setY(player.getY() + velocity.y * delta);

        if (velocity.y < 0) { // going down
           // bottom left
           TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) (player.getY() / tileHeight));
            if (cell != null) {
                collisionY = cell.getTile().getProperties().containsKey("blocked");
            }
            // bottom middle
           if (!collisionY) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width / 2) / tileWidth), (int) (player.getY() / tileHeight));
                if (cell1 != null) {
                    collisionY = cell1.getTile().getProperties().containsKey("blocked");
                }
            }

            // bottom right
           if (!collisionY) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) (player.getY() / tileHeight));
                if (cell1 != null) {
                    collisionY = cell1.getTile().getProperties().containsKey("blocked");
                }
            }

            player.canJump = collisionY;
        } else if (velocity.y > 0) { // going up
           // top left
           TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
            if (cell != null) {
                collisionY = cell.getTile().getProperties().containsKey("blocked");
            }

            // top middle
           if (!collisionY) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width / 2) / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
                if (cell1 != null) {
                    collisionY = cell1.getTile().getProperties().containsKey("blocked");
                }
            }

            // top right
           if (!collisionY) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
                if (cell1 != null) {
                    collisionY = cell1.getTile().getProperties().containsKey("blocked");
                }
            }
        }

        // react to y collision
       if (collisionY) {
            player.setY(oldY);
            velocity.y = 0;
        }

    }
Offline Jeremy
« Reply #2 - Posted 2013-09-08 01:20:28 »

You need to use an appropriately sized capsule for your physics collision model.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #3 - Posted 2013-09-08 02:52:12 »

You need to use an appropriately sized capsule for your physics collision model.

What you mean?
Am i not doing that now already? with default size width?
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #4 - Posted 2013-09-08 03:05:51 »

Right now, the default width is being based upon the image size, which by the looks of it, is probably 36wide, by 48 pixels wide

for example, I made an outline of whats happening


You can see the bounding box and why he is doing that.

What you need to do, is set probably defaultWidth/2  and then offset it a bit to be centered

So it looks and behaves more like




However,
many games do support a 'stand on ledge'  by 1 pixel type approach
megaman for example



As you can see here, the bounding box is slightly smaller than the character,  and his feet overlap the ground tile



However somegames actually use a combination box AND a 'circle'  for the character.  (box for left/right and top collsion) but then a circle for feet, so they can stand really close to the edge.  But because of the 'roundness' of the circle.  They could potentially slide off the edge.

However there are many approaches.

I would suggest looking at this site, it has some good information
http://www.emanueleferonato.com/2012/05/24/the-guide-to-implementing-2d-platformers/


 Wink Cool Tongue Emo Pointing Roll Eyes Pointing Undecided Clueless persecutioncomplex Clueless

"Experience is what you get when you did not get what you wanted"
Offline Andre Lopes
« Reply #5 - Posted 2013-09-08 03:38:03 »

"offset it a bit to be centered"

Does that mean to change the origin?
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #6 - Posted 2013-09-08 06:06:07 »

Depends on your logic and rendering code

But you need to cut your bounding box to about 2/3 of its current size (play around with it a bit)

Also offset your rendering sprite a bit too( try tweaking some settings, maybe the offset will do it)  there are several ways to go about fixing this.

But now that you have a slightly better idea of whats going on, you can try and fix it.


Also, your collision code that you posted before could use some work.  There are several ways, of simplifying it without having ot have so many nested tiledmap calls that you are currently doing.  Such as building a simple integer or boolean[][] 2d array of 'blocked' and open,  that you simply compile/construct when the map first loads.  May make it easier than what you have right now.

"Experience is what you get when you did not get what you wanted"
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (53 views)
2014-04-15 18:08:23

BurntPizza (51 views)
2014-04-15 03:46:01

UprightPath (65 views)
2014-04-14 17:39:50

UprightPath (48 views)
2014-04-14 17:35:47

Porlus (65 views)
2014-04-14 15:48:38

tom_mai78101 (89 views)
2014-04-10 04:04:31

BurntPizza (149 views)
2014-04-08 23:06:04

tom_mai78101 (245 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!