Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / 2D Sprite Collision and positioning

Author
Message
Ramon156
13
Years of Service
User Offline
Joined: 13th Jul 2011
Location: Netherlands
Posted: 6th Aug 2011 22:29
I've got a problem with sprite collision. When my player is positioned under a sprite and jumps, the player positions on top of the sprite and basically goes through it. How can I make it so the player can't fall through sprites when standing on it, but also can't jump through when under it?

Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 7th Aug 2011 19:46
I have not tested all your code, but at a quick glance I noticed this:

IF SPRITE collision (2,0)

That line will only tell you what sprite is colliding with sprite 2. The line will produce a number equal to the sprite colliding with sprite 2.

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Quisco DaLuse
17
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: USA
Posted: 8th Aug 2011 03:58
Save the sprite's x and y positions before performing the collision check. If there is a collision, put the sprite back to the saved coordinates.
Ramon156
13
Years of Service
User Offline
Joined: 13th Jul 2011
Location: Netherlands
Posted: 10th Aug 2011 21:27
Hello Quisco DaLuse, could you help me with some example code? I've tried saving the "old" X and Y coordinates and position the sprite at those coordinates when it collides, but it doesn't seem to have any effect. The sprite slides through everything or it just does nothing at all. Even at some places in the code the screen just goes black.

Sprite collision (2,0) = Sprite collision (player,all the floors and walls that are in the game at this point)
Quisco DaLuse
17
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: USA
Posted: 11th Aug 2011 18:56
Okay, I made up something based off of your code that works.



I made some images in the code and created sprites with them. You can move the red box around and use the spacebar to jump. When it collides with the overhead sprite, it puts the player back where they were. The return key exits. I also added manual SYNCing, as the player flew off the screen immediately.

Your gravity and YSpeed variables were floats, which is used for 3D positioning, not 2D sprites. Anyway, I changed them.

You can look at the code to see how it works. I hope this is helpful.
Ramon156
13
Years of Service
User Offline
Joined: 13th Jul 2011
Location: Netherlands
Posted: 13th Aug 2011 19:29
Hello again. I've been trying allot but can't figure out what I'm doing wrong. I've uploaded the whole *updated* package with new gravity code and other stuff I'm working on, tile-scrolling enemy positioning etc. You can see that when I "run" into the side of the box I get positioned on top of it wich I don't want it to, I want it to stand to the side of the box. When ýou're on top of the box and jump the player sprite goes a bit nuts + you can't jump the same height up as you can from the ground.
Any ideas?

Thank you for helping '


p.s. I'd appreciate it also if someone could take a quick peek at positioning clouds at random coordinates

Attachments

Login to view attachments
Quisco DaLuse
17
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: USA
Posted: 14th Aug 2011 23:22 Edited at: 15th Aug 2011 00:05
You are moving the map instead of the player, yet you move the player on the Y axis. You should decide one way or the other; either you move the map around the player or move the player around the map.

Also, you should assign all of the tiles to the map as a whole initially (writing a map editor or finding one on this forum would be a good idea). Then you simply display a portion of the map based upon world x and world y coordinates. You could then use the map array to check whether or not the player is colliding with something.

You can move the clouds at a random speed and move them additionally based upon the distance the player moves. Once they get past the edge of the screen, you randomly place them at a new x / y coordinate prior to the screen. You could pick a new moving speed at that point. Actually, you could also stretch them randomly to make them appear a little different too.

EDIT: I would suggest using tiles that are smaller than 64 X 64, maybe use 32 X 32 instead.

Login to post a reply

Server time is: 2024-11-24 02:25:02
Your offset time is: 2024-11-24 02:25:02