Path: chuka.playstation.co.uk!news From: Phil Gooch Newsgroups: scee.yaroze.programming.2d_graphics Subject: Re: 2 Questions - Same Game Date: Tue, 20 Oct 1998 16:41:41 +0100 Organization: PlayStation Net Yaroze (SCEE) Lines: 88 Message-ID: <362CAF35.50EF7FDA@easynet.co.uk> References: <70f9cd$s3k10@chuka.playstation.co.uk> NNTP-Posting-Host: 193.131.140.246 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.05 [en] (Win95; I) John Whitmore wrote: > Hi, > I was wondering if anyone out there could help me? I am producing a 2D, > tile based game for my Final year University Project. This is my first > question: > > When using GsBG (GsCELL,GsMAP) what is the most efficient way of > checking the value of the cell the PC character is 'standing' on? > I.E. how can I tell if a cell is walkable/non-walkable for collision > detection. I also wish to use this technique for the collection of bonuses > within the level etc. > > Secondly a maths question, when moving my character around I wish to > store a vector (x,y) of the direction it is facing so a weapon can be fired > using this same vector. how is best to calculate this vector so it can be > used in this situation? > I have tried using the rotational value of the sprite, as this is how I am > changing the visual appearance of the sprite on screen, but with no success. > Thanks in advance, > > Hi John I don't use the GsBG functions as they're quite limited. Also, its easy to write your own routine. Set up a TIM file containing all your tiles (256*256 gives you 64 tiles of size 32*32 pixels) The approach I use is to set up an array of numbers to represent my 'world', where the first three bits of the number represents the 'x' offset of the tile in my TIM, the next three bits represents the 'y' offset of the tile in the TIM, and the other two bits can be used as flags for collision detection. This allows me to use bit-shifting and masking (which is supposed to be very fast) a) to load in the correct tile on screen when I loop through the array to draw the tiles on screen and b) to check collisions To see what tile your character is on, if your tile size is 32*32, set the sprite's mx and my values to be the center of the sprite and divide your character's 'x' and 'y' sprite co-ordinates by 32. This will give you the the world array co-ordinates of the centre of the sprite. (object.mapX = object.sprite.x/32 and object.mapY = object.sprite.y/32 - see below) Then, if you have used bit 6 as your collision flag (#define COLLISION_FLAG 1<<6), you just do if ( worldData[object.mapY][object.mapX] & COLLISION_FLAG ) to test for a collision. You'll need to do a bit more work to adjust object.mapX and object.mapY for scrolling worlds, but you get the idea. For storing a rotation vector, the best thing to do is when you are processing the keypad, rather than immediately rotating the sprite, store the rotation angle in a structure field, e.g. if (pad & PADleft) object.rotAngle--; if (pad & PADright) object.rotAngle++; Then modularise the angle to 360 and make positive: object.rotAngle = (360 + (object.rotAngle % 360)) Use this value to rotate your sprite: object.sprite.rotate = object.rotAngle*ONE and to generate your vector: object.directionX = COS[object.rotAngle] object.directionY = SIN[object.rotAngle] where COS[] and SIN[] are predefined arrays containing cos and sine values scaled up by 4096 (ONE) (James Shaughnessy has kindly provided these - you can get these from his Web site) Hope this helps Phil (one day I'll finish my game...)