GPWiki.org
GPWiki.org
It is currently Thu May 23, 2013 11:59 pm

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Wed Oct 05, 2011 7:22 am 
Hi,
I was just wondering if anyone has any good idea how to solve this elegant?

I'm currently developing a 2D RPG engine and I'm trying to keep the rendering separate from the datamodel.
So I have like a Character class which has a position (map cordinates, 32px at the moment) and an id (and some other attributes that aren't needed for this question :))
int x, y;
string id;

The "world" is rendered "constantly" and the character is rendered by a CharacterRender on the current cordinate.
The problem is that as soon the user presses LEFT I want to move the character left one map square (x = x -1) and I want the CharacterRender to create a pixel smooth animation to move the char from the current coordinate to the new coordinate

How would you solve it? :D


Top
  
 
PostPosted: Wed Oct 05, 2011 8:33 am 
Bytewise
User avatar

Joined: Mon Feb 19, 2007 8:43 am
Posts: 274
You could have PreviousPosition and DesiredPosition members, then a CurrentPosition(dt) method to interpolate the actual position based on a delta time value.

Assuming you poll input every frame, you would need to check desiredPosition != currentPosition before updating to the new desiredPosition.


Top
 Profile  
 
PostPosted: Wed Oct 05, 2011 8:37 am 
Dexterous Droid
User avatar

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3735
Location: South Africa
The above is certainly one way of doing it. You could also do the following, which adds less variables to your model.

Store the pixel coordinates of the player as a float and increment them slowly in the game logic when the player pushes a key. Don't act on any new input until the float position has moved at least 32 pixels in the desired direction. Once it has completed the move, there might be some left over remainder on the pixel position, so just set the position to a multiple of 32 (p = floor(p / 32) * 32).

That's one way of doing it, there may be a better way though. The best way will be whatever suits what you've got already.

_________________
Whatever the mind can conceive and believe, it can achieve


Top
 Profile  
 
PostPosted: Wed Oct 05, 2011 10:49 am 
Rookie

Joined: Wed Oct 05, 2011 7:23 am
Posts: 2
Thanks for the rapid response guys :)

I've been thinking about it and I think I want to constraint the movement so it's only possible to stop directly on a map tile. It's just the transfer between the map tiles that I want pixel positioning on.
(Is the "stop only directly on a tile" weird?)

I think it's possible to use IGTHORNs idea with some modification. But I have still not gotten that "This is the perfect solution" feeling :D

This is kind of a new experience for me, I'm usually a web/desktop application developer :)

Does anyone have any examples on how they have solved it in similar cases?


Top
 Profile  
 
PostPosted: Wed Oct 05, 2011 10:55 am 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3809
Location: Ferriday, LA, US
Welcome a-"board", Megamannen! (Sorry, I'm one who simply cannot resist taking advantage of an opportunity to lay out a pun. :P)

I don't have any code in front of me right this minute, but look into the concept of "linear interpolation". It's moving a value from point A to point B based on time -- it sounds exactly like what you're asking for.

I will try and work out a quick example -- but in the meantime, look into linear interpolation, and see if you can get the basic idea.

EDIT -- Sorry Megamannen, I forgot that my understanding of math is on par with that of a particularly retarded mayonnaise container. I can't find any of my old code that solves this issue, my books explain mathematics in Klingon, and I just don't have the skill to work it out on my own. I'm positive, though, that someone will come along very soon and get you on the right track. My apologies!

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Last edited by Mugai on Wed Oct 05, 2011 11:27 am, edited 1 time in total.
Needed to point out that I'm stupid.


Top
 Profile  
 
PostPosted: Wed Oct 05, 2011 12:59 pm 
Dexterous Droid
User avatar

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3735
Location: South Africa
Megamannen wrote:
Thanks for the rapid response guys :)

I've been thinking about it and I think I want to constraint the movement so it's only possible to stop directly on a map tile. It's just the transfer between the map tiles that I want pixel positioning on.
(Is the "stop only directly on a tile" weird?)

I think it's possible to use IGTHORNs idea with some modification. But I have still not gotten that "This is the perfect solution" feeling :D

This is kind of a new experience for me, I'm usually a web/desktop application developer :)

Does anyone have any examples on how they have solved it in similar cases?

Keeping your character restricted to tiles may be good depending on how your game plays - if positioning is important for blocking off enemies from paths or something strategic like that.

What other information are you storing for the characters? If you're storing some kind of elapsedTime value for animation purposes, you could extend that to do what I suggested. Maybe tell us a little more about what's going on and a better solution will present itself :)

_________________
Whatever the mind can conceive and believe, it can achieve


Top
 Profile  
 
PostPosted: Wed Oct 05, 2011 1:19 pm 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2217
Location: England
Have three sets of coordinates:

For logic purposes:
(gridx, gridy) stores the integer grid location you are on.
(targetx, targety) stores the grid location you are moving to.

For graphics purposes
(drawx, drawy) stores the position of the sprite.

have an interpolator lambda that moves from one to the other.

drawx = gridx * (1 - lambda) + targetx * lambda
drawy = gridy * (1 - lambda) + targety * lambda

You can also use lambda to animate the walking motion, by selecting the appropriate frame to draw.
Once lambda is greater than or equal to 1, then the motion is completed, so let gridx = targetx, gridy=target y. and lambda=0

_________________
I ain't pushing no moon buttons.


Top
 Profile  
 
PostPosted: Wed Oct 05, 2011 4:03 pm 
Rookie

Joined: Wed Oct 05, 2011 7:23 am
Posts: 2
Restricted to tiles it is, you got me thinking about my battles and there the positioning is based on tiles. So why have it different outside of battles!? Thanks :)

I'll see if I can apply some of these ideas on my code, I'll be back ;)


Top
 Profile  
 
PostPosted: Wed Oct 05, 2011 4:29 pm 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3809
Location: Ferriday, LA, US
Megamannen wrote:
(...) you got me thinking about my battles and there the positioning is based on tiles. So why have it different outside of battles!? Thanks :)

A good example of practicing the principle of KISS. (Keep It Simple, Sweetie -- because "stupid" is not allowed on this forum!*)

Looking forward to what you come up with. Stop by if you have any problems, as well! :)

*Exceptions to this rule include: rotInMilc calling himself stupid; and users in general calling rotInMilc stupid. ;)

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group