forums

TNT Basic Forums > Programming Q&A
Sprite Collision
< Last Thread     Next Thread >
Author
Thread        Post A Reply

09-23-2005 01:20

Posted by:
Ian

Location:
canada

Find more posts by Ian

Im looking for a clean efficient way to make a sprite (or tile) impassable by another sprite. ive come up with several solutions, but each has some situation where the sprite passes through, gets perminently stuck, or faces some other unwanted glitch. if anyone knows of a good way, id appreciate your input.

thanks,
Ian

09-23-2005 07:38

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Is this a continuation of the 'Moving Sprites Via Keyboard' thread? If so, mail me the source, with a description of what you are trying to achieve, and I'll put something together.
Generally, it's not that hard a thing to do. Tiles are easier to deal with in this situation if you are talking about terrain obstacles. If you are placing sprites for the obstacles (and using a map to do so) you would use the same technique, it's only when you are trying to avoid moving sprites that things tend to get 'stuck'. You just need to poll for input using the code snippets provided by MapMaker, but instead of checking for them all at once, check for the horizontal movement (or vertical) first and get the coordinates that the sprite would end up in if it moved. Then divide the x, y's by tile width and height respectively, use ints not floats for these. Find the map tile at these coordinates and see if it is an obstruction, it's a lot easier here if you group all obstructions in a fixed range, for example the obstructions are tile numbers 1 through 10, so if the reference is >10 it's clear. If the reference is clear, move the sprite in this axis, if not then don't. Then do the same for the other axis. If both axes are clear it will move diagonally, if requested, but if one is blocked it should 'slide' round the obstruction until it clears it, then carry on without getting stuck.
Hope this helps, but if you want further help just mail the source and I'll see what I can do. Good luck...

Danny (nods)

09-24-2005 23:59

Posted by:
Ian

Location:
canada

Find more posts by Ian

no, this isnt a continuation, seeing as i didnt post that thread. i know perfectly well how to move sprites with the keyboard.

as for your suggestion, i figured out all that myself. i get that. its specifically how to make a sprite not go through that i cant get. right now my system of movement is basically "if right then charX=charX+charXspeed" and so on for all directions. when it is touching an object, do i say charspeed=0? or do i make the object push back with the oposite of what the character is pushing? ive tried all sorts of things, including those. but if you say, (loosely) "if touching a certain tile then charXspeed=0" and the same for the y speed, sure, it will prevent you from going left through the object, but it also prevents you from going up down left or right away from it. im jsut looking for a somewhat specific code for how some one has made an object where a sprite cant go through it, from any side, and not get stuck or go through on some miniscule condition. i need a way to make an object that when you go up towards it, it stops you, and when you go down towards it it stops you, and left, and right... i wont believe that no one has made a wall in their game, or a rock, or something. hopefully im making sense.

thanks
Ian

09-26-2005 00:16

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Oops...

Sorry mate, was looking at the thread below.

OK, are you setting up a movement 'string' when you press a key (for example 'if right then start moving right'), or are you just saying 'if right then go right by one step'?

If it's the first case then when the character is blocked, just set a flag that tells you it is. Then when you go to move the sprite, only do it if the flag is clear, you haven't actually changed the speed or angle of movement of the sprite, just told the loop that it's not going anywhere in that direction for a bit. If it can move in one axis then do it, if can't then don't. Check collisions every loop and set the flags (for X and Y) and move as appropriate. If both are set (ie you are backed into a corner) you will need to handle this situation according to your game needs, if you give me a bit more info I can try and help further.
You can also use this method if you are doing step by step, but it's really not worth the bother. Ignore the variables for x and y displacemet and move the sprite within the collision check (or not as the case may be...). If the sprite still gets stuck, which it shouldn't, the problem is with your detection strategy. Try increasing the range (making it more pre-emptive) or just revise it completely. Make sure you are checking the character's final intended destination, taking both displacements into consideration, and check for rounding errors under division. If the destination is blocked, but the sprite still ends up in it anyway, there has to be a problem with your game's definition of where the sprite is heading.

I know now that you didn't post the thread I mentioned (for which I'm sorry, it must have been a long day!) but take a look at this anyway, I put a link to a very good tutorial on how to do advanced path finding at the bottom, and this may be a good place to look if you want to get really clever. Otherwise, give these ideas a go and get back to me if you're still stuck (so to speak...)

Danny (nods)

10-04-2005 00:37

Posted by:
Ian

Location:
canada

Find more posts by Ian

No worries. you needn't apologize, youve been more than helpful. ill definately look into that pathfinder thing.

thanks again

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.