Click here to Skip to main content
15,914,780 members
Home / Discussions / C#
   

C#

 
QuestionIterator, foreach, IEnumerable IEnumerator for a beginner :s Pin
bon_chan24-May-10 7:47
bon_chan24-May-10 7:47 
AnswerRe: Iterator, foreach, IEnumerable IEnumerator for a beginner :s Pin
Luc Pattyn24-May-10 8:04
sitebuilderLuc Pattyn24-May-10 8:04 
AnswerRe: Iterator, foreach, IEnumerable IEnumerator for a beginner :s Pin
Abhinav S24-May-10 18:23
Abhinav S24-May-10 18:23 
GeneralRe: Iterator, foreach, IEnumerable IEnumerator for a beginner :s Pin
bon_chan25-May-10 4:48
bon_chan25-May-10 4:48 
Question2D Map collison help required Pin
venomation24-May-10 7:00
venomation24-May-10 7:00 
AnswerRe: 2D Map collison help required [modified] Pin
Alaric_24-May-10 8:55
professionalAlaric_24-May-10 8:55 
AnswerRe: 2D Map collison help required Pin
Alaric_24-May-10 9:07
professionalAlaric_24-May-10 9:07 
GeneralRe: 2D Map collison help required [modified] Pin
Alaric_25-May-10 4:48
professionalAlaric_25-May-10 4:48 
To follow up, the method that I posted earlier will work if your terrain is a constant slope, but you'll have to do something special for areas where the slope changes (i.e. the tile that transitions from 'flat' to 'hilly') That could be done by keeping a Stack of the 3 tiles that your character is interacting with.
Let's create a section of terrain, listing the slopes in left to right sequence
float[] terrain = new float[10];
terrain[0] = 0.0;
terrain[1] = 0.0;
terrain[2] = 0.0;
terrain[3] = 0.5;
terrain[4] = 0.5;
terrain[5] = 0.5;
terrain[6] = 0.0;
terrain[7] = -0.5;
terrain[8] = -0.5;
terrain[9] = 0.0;

So, if you can visualize this, it would be a section of flat ground, followed by a hill that has a slope of 1/2 that rises for 3 tiles, flattens off for 1 tile and then descends at 1/2 for 2 tiles.

You could want to maintain a "frame" of reference via a stack with the 3 tiles that are being interacted with. There is the tile that you are standing on, the tile behind you, and the tile in front of you. Pressing the -> arrow would move him forward. Pressing the <- arrow would obviously perform the reverse. Let's deal only with "forward" movement for this example.

Whenever your player tells Sonic to "move forward" the rotation that you apply to him will be contingent upon "Current Tile Slope" AND "Next Tile Slope"

...that way, if your player decides to stop him while one foot is still on the "Current Tile" but the lead foot is on "Next Tile" you will still maintain believable contact with the ground. Once Sonic's trailing foot is completely upon "Next Tile", your stack should shift to the next tile so that the "frame" refers to the next tuple of slopes.

So, to walk through this scenario (this is a barebones algorithm. There are more moving parts to it that are contingent upon your game specifics)
(Calling your player "Sonic" for brevity)

Note that I am saying "debounced" step to indicate that your player is not holding the forward movement button down. Your game loop needs to process ONE forward command...to explain the base case. "Holding down the button" will just loop the ONE forward command until the button is released, obviously.


setup)Let's say Sonic is standing on square 3. Your frame would contain {0, 0, 0.5}
KeyValuePair<int, float>[] frame = new KeyValuePair<int, float>[3];

frame[0] = new KeyValuePair<2, terrain[2]>();
frame[1] = new KeyValuePair<3, terrain[3]>();
frame[2] = new KeyValuePair<4, terrain[4]>();


1)Player issues a debounced step forward. (Issue one step forward) Sonic would advance one step in your walk cycle animation.
2)Check width of Sonic with width of tile, compare Sonic's trailing point + step offset to width of tile.
3)If ((Sonic's trailing foot position + Sonic's width) > tile width) his lead foot has moved to the next tile. You need to use "This Tile"s slope for trailing foot and "Next Tile"s slope for leading foot to determine the rotation that needs to be applied.
//since step forward, use "next" tile.
float rotateZ = CalculateRotation(frame[1], frame[2]); //I'll leave you to figure the math. It's pretty simple though.
Sonic.Rotate(rotateZ); 

4)If (Sonic's trailing foot position > (tile width + Sonic's width)) then the trailing foot has moved away from "This square" Cycle your frame to track a new square as "This Tile"
OffsetFrame(1);
Sonic.Rotate(CalculateRotation(frame[1], frame[2]));

public void OffsetFrame(int offset)
{
  frame[0] = new KeyValuePair<((int)frame[0].Key + 1), terrain[(int)frame[0].Key +1]>();
  frame[0] = new KeyValuePair<((int)frame[1].Key + 1), terrain[(int)frame[1].Key +1]>();
  frame[0] = new KeyValuePair<((int)frame[2].Key + 1), terrain[(int)frame[2].Key +1]>();

}

5)Loop 1-4


So, again for simplicity's sake, let's say that each debounced forward step moves your character from square to square "a la Frogger style". Your frame would look as such, starting with "This Tile" referencing square 3.

Frame: {0, 0, 0.5}
[Step forward]
Frame: {0, 0.5, 0.5}
[Step forward]
Frame: {0.5, 0.5, 0.5}
[Step forward]
Frame: {0.5, 0.5, 0}
[Step forward]
Frame: {0.5, 0, -0.5}

Get the idea? You're dealing with a sprite based walkcycle and multiple pitch terrain collision, so there are more details that are specific to your game but that should get you started in a direction that will at least get you moving. The "Frogger" example is to illustrate what I mean to do when Sonic's trailing foot moves onto a new square, but you won't be moving square by square. You'll be moving a partial square for each forward command to indicate a single step. Hope it's clear where the two examples above meet together to provide you with smooth, animated sprite movement.
"I need build Skynet. Plz send code"
modified on Wednesday, May 26, 2010 11:05 AM

AnswerRe: 2D Map collison help required Pin
venomation26-May-10 5:59
venomation26-May-10 5:59 
QuestionMessage Removed Pin
24-May-10 3:30
Xpnctoc24-May-10 3:30 
AnswerRe: Form controls data binding sequence Pin
Luc Pattyn24-May-10 5:21
sitebuilderLuc Pattyn24-May-10 5:21 
GeneralRe: Form controls data binding sequence Pin
Xpnctoc24-May-10 5:30
Xpnctoc24-May-10 5:30 
GeneralRe: Form controls data binding sequence Pin
Luc Pattyn24-May-10 7:50
sitebuilderLuc Pattyn24-May-10 7:50 
GeneralRe: Form controls data binding sequence Pin
Xpnctoc24-May-10 8:00
Xpnctoc24-May-10 8:00 
GeneralRe: Form controls data binding sequence Pin
Luc Pattyn24-May-10 8:09
sitebuilderLuc Pattyn24-May-10 8:09 
GeneralRe: Form controls data binding sequence Pin
Xpnctoc24-May-10 8:15
Xpnctoc24-May-10 8:15 
QuestionVisual Studions 2005 and crystal reports 11.5 R2 Pin
krown24-May-10 2:56
krown24-May-10 2:56 
QuestionWhy am I recieving corrupt images? Pin
TimSWatson24-May-10 2:37
TimSWatson24-May-10 2:37 
AnswerRe: Why am I recieving corrupt images? Pin
Luc Pattyn24-May-10 4:53
sitebuilderLuc Pattyn24-May-10 4:53 
JokeRe: Why am I recieving corrupt images? Pin
Wes Aday24-May-10 8:27
professionalWes Aday24-May-10 8:27 
JokeRe: Why am I recieving corrupt images? Pin
DaveyM6924-May-10 9:06
professionalDaveyM6924-May-10 9:06 
GeneralRe: Why am I recieving corrupt images? Pin
TimSWatson25-May-10 4:42
TimSWatson25-May-10 4:42 
Questiontext alignment problem on the image.... Pin
Nivas8224-May-10 1:40
Nivas8224-May-10 1:40 
AnswerRe: text alignment problem on the image.... Pin
Luc Pattyn24-May-10 4:43
sitebuilderLuc Pattyn24-May-10 4:43 
GeneralRe: text alignment problem on the image.... Pin
Nivas8226-May-10 22:04
Nivas8226-May-10 22:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.