on 05-05-2012 03:10 PM
I've been looking at some XNA examples to workout how I can integrate collisions into my PSS game. I've found the following code to transform a texture into a 2D array. However I've not yet been successful in recreating this in the PSS.
private Color[,] TextureTo2DArray(Texture2D texture)
{
Color[] colors1D = new Color[texture.Width * texture.Height];
texture.GetData(colors1D);
Color[,] colors2D = new Color[texture.Width, texture.Height];
for (int x = 0; x < texture.Width; x++)
for (int y = 0; y < texture.Height; y++)
colors2D[x, y] = colors1D[x + y * texture.Width];
return colors2D;
}
Does anyone have any example code that can do pixel based collisions within the GameEngine2D?
on 06-05-2012 12:59 AM
It feels like it's going around the houses a little - but I can't see any way of grabbing texture data from a Texture2D or PixelBuffer object directly, so you might want to also load up the texture using the 'Image' object, which will give you access to the pixel data before loading up the Texture object. This is untested, but off the top of my head - something like the following might be an option;
// Note. I'm making an assumption that all images you read in are 'ImageMode.RGBA' as I believe Image objects are either RGBA or just A. This is completely untested, and it just a potential idea for a solution.
---------
String filePath = "/Application/assets/image.png";
Image image = new Image(filePath);
Byte[] byteData = image.ToBuffer();
image.Dispose();
Texture2D texture = new Texture(byteData, false, PixelFormat.Rgba);
if ( byteData.Length > 0 ) {
Byte[] alphaChannelData = new Byte[byteData.Length / 4];
for ( int i = 0; i < alphaChannelData.Length; i++ )
{
alphaChannelData[i] = byteData[i*4];
}
}
--------
This *should* pull out the alpha channel when you load a sprite and you could store and use that alphaChannelData (1 byte per pixel) in your per-pixel collision detection routine later on. There are possibly (probably) better alternatives available, but this is what comes to mind after the limited time I've been playing with the SDK.
With this in mind however, I don't know how much much juice the PS Vita / PSS devices are realistically going to have for per-pixel collision detection. I coded up a PP Collision Detection routine using XNA and even using a system to pull out the intersecting rectangle first, the operation was rather expensive in terms of CPU power. You might want to consider a shape system and/or using the Physics2D library - even if you only use it with 'triggers' (collision detection).
on 06-05-2012 09:51 AM
on 06-05-2012 10:29 AM
on 06-05-2012 11:57 PM
Hmm, this looks more complicated than I thought it would be (but possibly only because things are in Beta stage and not 100% yet).
* ActionGameDemo *
I've only had a very brief look at the ActionGameDemo, but it looks like every frame each entity adds itself to the 'collider'. Then, the EntityCollilder simply iterates over each of the pairs of entities and does a 'distance squared' check against the radius of each object. Hence, it's modelling everything as a circle and brute force checking everything to see if it collides. If you don't have too many objects in the scene, and most of your entities are easily modelled by one or more circles, this could work ok.
* Physics2D Library *
I'm looking at the Physics2D library at the moment, as I'm trying to integrate it into my game. It looks to be quite 'heavy weight' but I'm assuming it's highly optimised. If you are creating a scene which contains 30 fixed objects and they move around - then it doesn't look too hard to get the hang of. If you are creating a model in which objects are rapidly created and destroyed (I'm trying to add bullets to my space shooter) then I've not quite figured that out yet. Removing entities from the scene looks to be a bit of a pain, as it works with a fixed length array not a list / vector. I guess I'm saying - this will probably do the trick very well, but it could be a complicated solution.
* Getting Pixels from an Image *
My first impression is that it could be that this method might not be properly supported yet in the SDK. However, this might sound obvious, but have you also checked the 'Size' property to ensure that the image is infact loading properly? I'm not sure if you might also need to call the method 'Decode()' on the object after constructing it but before using it? I'm guessing that GetPixelData is a private method on the Image object.
If this doesn't work, I'm a bit stuffed on what to suggest I'm afraid. It seems like the GetPixel type methods have been deliberately hidden away (unless I've missed something obvious). The only other ways I can think of achieving what you are after are...
1. Drawing your graphic to the Frame Buffer and using GraphicsContext.ReadPixels() to read back the pixel data from the frame buffer. You would obviously have to do all this before starting your game level as you're essentially drawing it to the screen just to get access to the pixel data.
2. Try to find some PNG loading library and use that to load the pixel data in.
3. Store shape data in text files along with the image files you make for your app / game. Then load these shapes in and write a shape based collision detection system.
4. Use something like the Farseer Physics Engine (http://farseerphysics.codeplex.com/), which I am thinking of attempting to make use of in our project. Farseer is based on Box2D, so there is a lot of information about how to use it (even just for collision detection).
on 07-05-2012 04:10 PM
Thanks for all your help, the Decode() method is required in order to use the ToBuffer() method.
I've now managed to extract the alpha data into an array:
Image image = new Image(filePath);
image.Decode();
Byte[] byteData = image.ToBuffer();
Byte[] alphaChannelData = new Byte[byteData.Length / 4];
if ( byteData.Length > 0 )
{
for ( int i = 0; i < alphaChannelData.Length; i++ )
{
alphaChannelData[i] = byteData[(i*4)+3];
}
}
on 09-05-2012 09:24 AM
In regards to creating and destroying bullets often why don't you have a bullets pool and reuse bullets
on 23-06-2012 09:14 PM
What means this code?
if ( byteData.Length > 0 )
{
for ( int i = 0; i < alphaChannelData.Length; i++ )
{
alphaChannelData[i] = byteData[(i*4)+3];
}
}
Good Work
PSM Developer Registration (for free) on PSM DevPortal is required to post on the forum.
Please sign out then sign in again to the forum and PSM DevPortal after you have completed the registration.
フォーラムへ投稿をするにはPSM DevPortalへの登録(無料)が必要です。
登録後はフォーラムと
PSM DevPortalを一度ログアウトし、再度ログインしてください。


Website ©2013 Sony Computer Entertainment Europe
All content, game titles, trade names and/or trade dress, trademarks, artwork and associated imagery are trademarks and/or copyright material of their respective owners. All rights reserved. [more info]
%%http://community.eu.playstation.com/t5/Announcements/Beta-Trial-Information/td-p/11386362
best_shooter.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_driver.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_performer.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_footballer.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_fighter.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_creator.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_action_player.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
dev2.png%%http://community.eu.playstation.com/t5/Website-and-Forum-Help-Feedback/Producer-and-Developer-Ranks/td-p/18407352
trophy.gif%%http://community.eu.playstation.com/t5/Website-and-Forum-Help-Feedback/The-Community-Awards-FAQ/td-p/18407096
PSlogoSM.png%%http://community.eu.playstation.com/t5/Website-and-Forum-Help-Feedback/Online-Support-Coordinator-rank/td-p/18414870