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.

2D All the way! / moving sprites to their destination smoothly

Author
Message
Malboro Jones
14
Years of Service
User Offline
Joined: 10th Dec 2009
Location: Wales - UK
Posted: 12th Jan 2010 18:45
Hellow! I'm making an rpg type game and I'm having a problem "throwing bombs" I get the player to click the where they want the bombs to go and then the sprite moves from the player to the coordinates and explodes but I want to make it look a bit better.

I'll try to explain in enough detail:

At the moment I have two functions that return 1, 0 or -1 to tell the sprite which way to move, because of this it will move in both directions at the same time, if the x position is reached it will stop and carry on along the y axis

e.g. xpos = 100, ypos = 100, xdest = 150, ydest = 200
in this case the functions will both return +1 and the sprite will move perfectly diagonal until x = 150, then it would continue along the y axis. This is OK but I'd like to improve the "throwing" effect.

I'd like the sprite to move smoothly to its destination - as in it moves directly towards it if this makes sense.

Can you provide me with an example on how to do this please? I'm guessing theres a bit of maths to do =]

Thanks Malb
jfroco
14
Years of Service
User Offline
Joined: 1st Dec 2009
Location: Chile
Posted: 13th Jan 2010 00:57
Hi Malboro Jones,

See the attached code.




Actually it was very fun to code it
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 13th Jan 2010 01:29
You can use floats # for smooth movement even though the floats are mostly ignored until they equal 1. Floats add a time delay to the sprite being moved, and the time delay works like an optical illusion that the sprites are moving half pixels and less.

Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 13th Jan 2010 10:19
Agreed, in this case floats are vital. You do not want to use integers when moving at non-integer values, if your not moving in solid integer numbers then you need floats to actually remember the position, instead of rounding off the position each time. If you use floats, then a bullet will fly straight to the destination and hit it perfectly, like in jfroco's example - without floats the bullet would probably just be restricted to 8 directional movement, diagonal or straight - no inbetween.

One thing though, with bullet movement, you have to consider collision first and foremost. If you are using pixel based collision for example, you might only want each bullet to move 1 pixel at a time (as a float though), so the movement would be a standard -1.0 to 1.0 vector, and if the speed is 5, then that means moving 5 times instead of moving in a chunk of 5. This would prevent any collision pitfalls, like if the bullet passes right through thin obstacles.
In 3D, the movement needn't be a vector so much, it could move a bullet 50 units, but with line intersect collision that's fine, because the whole path of the bullet would be checked.


Health, Ammo, and bacon and eggs!
Malboro Jones
14
Years of Service
User Offline
Joined: 10th Dec 2009
Location: Wales - UK
Posted: 14th Jan 2010 23:06
Ah that bit of code was brilliant! I'm not using Pro btw, had to tinker with it a bit but got it to work perfectly! Thanks! I'll probably be using it a lot for other weapons and AI throwing stuff back. Thanks again!!

Don't make war, make tea.
jfroco
14
Years of Service
User Offline
Joined: 1st Dec 2009
Location: Chile
Posted: 15th Jan 2010 02:53
Excellent, Malboro.

Good luck with your game!!!

Login to post a reply

Server time is: 2024-03-29 11:37:27
Your offset time is: 2024-03-29 11:37:27