Hi guys i was experementing with tiled maps and wondered how to make the user stop when they hit a wall. also how to make directional bullet firing controlled by mouse. im a new programmer and need a little bit of a kickstart thanks for the help!
3 replies to this topic
Sponsor:
#2 Members - Reputation: 134
Posted 14 February 2012 - 07:57 PM
Lets break down what you're asking for.
When you first create games, your player will simply have an X position and a Y position. In a tile-based game, you can use this to detect the player's position in relation to the tiles and the grid on which they lie. Imagine that your player lies on a chess board, if the squares on the board are 64 pixels wide and long, then if your player was at (32,32) then he'd be considered to be in the top-left square. If he was at (500,500), he'd be in the bottom-right square.
Now, each tile in your game must be considered passable or not-passable. Grass would usually be passable, but Walls would not. This allows you to define which tiles players CAN and CANNOT walk on.
With this information together, you can work out which grid-space the player is attempting to enter and check if it is passable or not. If it is not passable, prevent the player from entering that space, but position them so they are immediately outside it.
That is the theory. Hopefully someone can provide you with useful articles to help you implement it yourself.
Secondly:
These concepts are explained quite simply here:
http://www.rodedev.c...ls/gamephysics/
Thirdly:
p.s. Hopefully my explanations were easy to follow. Giving advice to others is quite a new thing for me.
- User must stop when they hit a wall
When you first create games, your player will simply have an X position and a Y position. In a tile-based game, you can use this to detect the player's position in relation to the tiles and the grid on which they lie. Imagine that your player lies on a chess board, if the squares on the board are 64 pixels wide and long, then if your player was at (32,32) then he'd be considered to be in the top-left square. If he was at (500,500), he'd be in the bottom-right square.
Now, each tile in your game must be considered passable or not-passable. Grass would usually be passable, but Walls would not. This allows you to define which tiles players CAN and CANNOT walk on.
With this information together, you can work out which grid-space the player is attempting to enter and check if it is passable or not. If it is not passable, prevent the player from entering that space, but position them so they are immediately outside it.
That is the theory. Hopefully someone can provide you with useful articles to help you implement it yourself.
Secondly:
- Directional bullet firing
These concepts are explained quite simply here:
http://www.rodedev.c...ls/gamephysics/
Thirdly:
- Mouse input
p.s. Hopefully my explanations were easy to follow. Giving advice to others is quite a new thing for me.
#3 Members - Reputation: 198
Posted 15 February 2012 - 06:16 AM
I'm not sure what your language, OS, or scripting interface is; so I'm going to answer as if you're in Windows C++.
It's been several years since I've made a tile-based game, so bear with me. These are the highlights that I remember.
Tile maps:
The key thing to realize is that tile maps can have multiple "layers".
Primary layer: where your character runs around.
Foreground layers: objects 'in front' of your character, usually scrolls faster than primary layer
Background layers: objects 'behind' your character (IE scenery), usually scrolls slower than primary layer
Collision layer: non-visible areas where the character will 'collide' with the world. May define several types of collision, such as slopes or different terrain (ice)
Trigger layer: Areas that cause an event when something enters them (player, enemy, etc)
Refer to TehOwn's post for more information about tile collision.
Mouse Input & bullet firing:
You really want your input to be hardware accelerated, but to get things going quickly you can use the windows function GetCursorPos(LPPOINT).
This returns a point in screen space, you need to convert it to client space.
This is completely untested code, but that's the general idea.
It's been several years since I've made a tile-based game, so bear with me. These are the highlights that I remember.
Tile maps:
The key thing to realize is that tile maps can have multiple "layers".
Primary layer: where your character runs around.
Foreground layers: objects 'in front' of your character, usually scrolls faster than primary layer
Background layers: objects 'behind' your character (IE scenery), usually scrolls slower than primary layer
Collision layer: non-visible areas where the character will 'collide' with the world. May define several types of collision, such as slopes or different terrain (ice)
Trigger layer: Areas that cause an event when something enters them (player, enemy, etc)
Refer to TehOwn's post for more information about tile collision.
Mouse Input & bullet firing:
You really want your input to be hardware accelerated, but to get things going quickly you can use the windows function GetCursorPos(LPPOINT).
This returns a point in screen space, you need to convert it to client space.
PSEUDOCODE
void Player::UpdateGun()
{
// This buffers the input, so we aren't shooting a bullet
// every frame the left mouse button is held
static bool bMouseWasClicked = false;
// Check if the left mouse button is pressed
if( GetAsyncKeyState( VK_LBUTTON ) && !bMouseWasClicked )
{
POINT mousePos;
float bulletDirX, bulletDirY;
bMouseWasClicked = true;
// Get current position of mouse
GetCursorPos( &mousePos );
ScreenToClient( hWindow, &mousePos );
// Get a vector from the gun to the mouse
bulletDir.x = mousePos.x - m_Gun.x;
bulletDir.y = mousePos.y - m_Gun.y;
// Normalize the direction vector
float mag= sqrt( bulletDirX * bulletDirX + bulletDirY * bulletDirY );
if( mag == 0.0f ) mag = 1.0f;
bulletDirX /= mag;
bulletDirY /= mag;
// Tell the gun which way to fire
m_Gun.Fire( bulletDirX, bulletDirY );
}
else
{
bMouseWasClicked = false;
}
}
This is completely untested code, but that's the general idea.
#4 Members - Reputation: 100
Posted 16 February 2012 - 10:09 AM
Thank you guys so much for the help you really explained it well, i do know about collision detection and was just wondering how to implement it into tiled maps so that i could have walls and such and the code for the mouse directional shooting is very helpful thankyou!






