I have created a XNA game background as a model, but it contains more models inside. For example: "ground contains a ball". Now, what I want to do is go inside the Ground and retrieve the ball and resize it.
Is there a possibility of doing this or do I need to import ground and ball separately and then resize it (I would prefer this to be the last option)?
I think what you mean is that you want to scale a specific ModelMesh inside a model. This can be done using a Matrix array, which will contain transforms specific to individual meshes. Something like this:
//In the class for the background, or the game class if there isn't one
Matrix[] specificTransforms;
//Initialize the array however you want, and assign the specific matrix for the ball to its corresponding index
//Loop through the meshes like usual but add this below your code that multiplies the transform matrices:
if (specificTransforms[(put your iterator variable here)] != null)
{
(put your BasicEffect here).World *= specificTransforms[put your iterator variable here];
}
This will combine the world matrix for the mesh with the specific transformation that you want to apply to the ball.
This is not the best way of doing it, and it would be easier to give you an example if you post the code that you're using. Unless you do that, there is no guarantee that this will work.
Related
With UIDynamics it is simple to have the physics objects, called dynamic items, control custom views (or indeed anything you want) using the protocol UIDynamitItem with the three properties center, transform and bounds. I would like to do something similar with SpriteKit, but according to the manual:
Unlike views, you cannot create SKNode subclasses that perform custom drawing
Specifically, I would like to have the physics bodies control some vector graphics I currently have in a drawrect. There are two things I am after here:
The first is to let the vector graphics move around like any other node.
The second is to let position, angle and other properties change the exact position of some of the control points.
I am fairly certain that I could achieve this with UIDynamics and dynamic items, but then I wouldn't be able to use actions and other nice sprite kit features.
It would also seem that 1 could be handled by converting the paths to cgpaths and using shape nodes, but that would be limiting, and not cover 2.
Any suggestions?
I'm currently trying to write a little game engine in C# using SlimDX just for fun.
I want my world to be destructable, so I have to be able to modify my map. My map is currently vector based, represented by an ID2D1PathGeometry (PathGeometry in SlimDX) object. This object is modified, using the CombineWithGeometry method of ID2D1Geometry (Geometry in SlimDX).
For reasonable collision detection, I need knowledge about the exact shape of my ID2D1PathGeometry object, for instance for calculating angles of balls bouncing of the walls.
So, is it possible to access all or specific (per location) segements/lines/points of my ID2D1PathGeometry object? Or are there other, better ways to accomplish my goals, e.g. storing all lines and shapes additionaly in another data structure?
Please note that bitmap based maps are not the way to go here, since I don't want to have memory as a constraint on the map size.
with best regards, Emi
There is no way to retrieve just the geometry segments that are close to a certain point, but there is a way to retrieve all geometry segments.
Implement a class that inherits from ID2D1SimplifiedGeometrySink.
Create an instance of that class and pass it to ID2D1Geometry::Simplify.
More info and example are here How to Retrieve Geometry Data by Extending ID2D1SimplifiedGeometrySink
If you are interested in retrieving only portion close to a certain point, perhaps you'd like to:
Create rectangular geometry surrounding the point of interest.
Intersect it with your geometry via ID2D1Geometry::CombineWithGeometry using D2D1_COMBINE_MODE==D2D1_COMBINE_MODE_INTERSECT.
Get sink of the intersected geometry using the method above.
More info: ID2D1Geometry::CombineWithGeometry method
I'm trying to go for a 2D game like Legend of Mana and Star Ocean Second Story rather than tile based 2D games.
OVERVIEW
Currently, the way I'm going about building my game is like so:
I have geometry files and texture files. Geometry files store width, height, XY position, Texture ID number to use and texture name to use.
The way I'm trying to do is:
I will have a scene manager object that loads "scene" objects. Each scene object stores a list of all geometry objects in that scene as well as texture and sound objects (to be implemented).
Each scene is rendered using vertex array whereby the scene manager object would call a method like get scene vertices by scene name which returns a pointer to an array of GLFloats (GLfloat *) and this pointer to GLfloat gets used in OpenGL's glVertexPointer() function.
When I want to update each character's position (like the hero for example), my aim is to use the "hero" game objects in the current scene and call a function like:
Hero.Move(newXPosition, newYPosition);
which will actually alter the hero geometry object's vertex data. So it will do something like:
for(int i = 0; i < 8; i++)
{
vertexList[i] = vertexList[i] + newXPosition;
vertexList[i+1] = vertexList[i+1] + newYPosition;
...
}
This way, when I go to render the scene in the render frame, it will render the entire scene again with the updated vertex coordinates.
Each object in the game will just be a quadrilateral with a texture mapped to it.
THE PROBLEM
I'm using Objective C for my programming and OpenGL. This is for iOS platform.
I have been successful thus far using this method to render 1 object.
The problem I have is I'm using a NSMutableDictionary which is a data structure that uses key-value pair to store geometry instance objects in the scene object class. Dictionaries in Objective C doesn't retrieve data in the same order every time the code is run. It retrieves then in random order.
Becausing of this, I am having trouble combining all the vertex array data from each geometry object in the scene object and passing out 1 single vertex pointer to GLfloats.
Each geometry object stores it's own array of 8 vertex values (4 pairs of X,Y coordinate value). I would like each geometry object to manage it's own vertices (so I can use Move, Rotate like mentioned earlier) and at the same time, I would like my scene object to be able to output a single pointer reference to all vertices data of all geometry objects in the current scene for using in OpenGL's glVertexArray() function.
I am trying to avoid calling OpenGL's glDrawArrays(GL_TRIANGLE_STRIP, 0, 4) multiple times. Like draw hero, draw map, draw AI agents, draw BG objects. That would not be very efficient. Minimizing the amount of GL draw calls as much as possible (to 1 draw call preferably), especially on limited hardware like the iPhone is what was suggested when I was reading about OpenGL game development.
SUGGESTIONS?
What is the best practice way of going about doing what I'm trying to do?
Should I use a SQL database to store my game data like geometry vertices data and load JUST 1 scene into iPhone memory from the sql database file on iPhone's disk?
You can use a a list of lists to keep track of draw layer and order, and use the dictionary solely for fast lookup.
What I don't understand is why you don't use Cocos2D, that happens to be built on the scene manager paradigm. Think of all the development time you will save...
I have worked in a company that did wonderful games. It eventually died because they kept putting out buggy games, due to the lack of development time for debugging. They did however find time to create a graphics rendering engine.
I thought, and still think, they had wrong priorities. It seems to me you are doing the same mistake: are you trying to make an iOS game, or are you trying to learn how to do a 2D gaming engine for iOS?
Note: Cocos2D is Open Source, you can therefore read it, now that you have thought about the process to create such an engine.
I am using XNA for a 2D project. I have a problem and I don't know which way to solve it. I have a texture (an image) that is drawn to the screen for example:
|+++|+++|
|---|---|
|+++|+++|
Now I want to be able to destroy part of that structure/image so that it looks like:
|+++|
|---|---|
|+++|+++|
so that collision now will work as well for the new image.
Which way would be better to solve this problem:
Swap the whole texture with another texture, that is transparent in the places where it is destroyed.
Use some trickery with spriteBatch.Draw(sourceRectangle, destinationRectangle) to get the desired rectangles drawn, and also do collision checking with this somehow.
Split the texture into 4 smaller textures each of which will be responsible for it's own drawing/collision detection.
Use some other smart-ass way I don't know about.
Any help would be appreciated. Let me know if you need more clarification/examples.
EDIT: To clarify I'll provide an example of usage for this.
Imagine a 4x4 piece of wall that when shot at, a little 1x1 part of it is destroyed.
I'll take the third option:
3 - Split the texture into 4 smaller
textures each of which will be
responsible for it's own
drawing/collision detection.
It's not hard do to. Basically it's just the same of TileSet struct. However, you'll need to change your code to fit this approach.
Read a little about Tiles on: http://www-cs-students.stanford.edu/~amitp/gameprog.html#tiles
Many sites and book said about Tiles and how to use it to build game worlds. But you can use this logic to everything which the whole is compost from little parts.
Let me quick note the other options:
1 - Swap the whole texture with
another texture, that is transparent
in the places where it is destroyed.
No.. have a different image to every different position is bad. If you need to change de texture? Will you remake every image again?
2- Use some trickery with
spriteBatch.Draw(sourceRectangle,
destinationRectangle) to get the
desired rectangles drawn, and also do
collision checking with this somehow.
Unfortunately it's don't work because spriteBatch.Draw only works with Rectangles :(
4 Use some other smart-ass way I don't
know about.
I can't imagine any magic to this. Maybe, you can use another image to make masks. But it's extremely processing-expensive.
Check out this article at Ziggyware. It is about Deformable Terrain, and might be what you are looking for. Essentially, the technique involves settings the pixels you want to hide to transparent.
Option #3 will work.
A more robust system (if you don't want to be limited to boxes) would use per-pixel collision detection. The process basically works as follows:
Calculate a bounding box (or circle) for each object
Check to see if two objects overlap
For each overlap, blit the sprites onto a hidden surface, comparing pixel values as you go. If a pixel is already set when you try to draw the pixel from the second sprite, you have a collision.
Here's a good XNA example (another Ziggyware article, actually): 2D Per Pixel Collision Detection
Some more links:
Can someone explain per-pixel collision detection
XNA 2-d per-pixel collision
I ended up choosing option 3.
Basically I have a Tile class that contains a texture and dimention. Dimention n means that there are n*n subtiles within that tile. I also have an array that keeps track of which tiles are destroyed or not. My class looks like this in pseudo code:
class Tile
texture
dimention
int [,] subtiles; //0 or 1 for each subtile
public Tile() // constructor
subtiles = new int[dimention, dimention];
intialize_subtiles_to(1);
public Draw() // this is how we know which one to draw
//iterate over subtiles
for(int i..
for(int j ...)
if(subtiles[i,j] == 1)
Vector2 draw_pos = Vector2(i*tilewidth,
j*tileheight)
spritebatch.Draw(texture, draw_pos)
In a similar fashion I have a collision method that will check for collision:
public bool collides(Rectangle rect)
//iterate over subtiles
for i...
for j..
if(subtiles[i,j]==0) continue;
subtile_rect = //figure out the rect for this subtile
if(subtile_rect.intersects(rect))
return true;
return false;
And so on. You can imagine how to "destroy" certain subtiles by setting their respective value to 0, and how to check if the whole tile is destroyed.
Granted with this technique, the subtiles will all have the same texture. So far I can't think of a simpler solution.
I am just playing around with XNA, and I have several different models I need to draw in each frame.
at the moment, the Game object holds references to all my models, and draws them one after the other. Each with his own different way of drawing - one has two separate textures, the other might be mirrored to the other side, etc.
I was wondering if it is acceptable to just add a
public void Draw(SpriteBatch spriteBatch)
method to all my models (from the BaseModel, of course), and have each class be in charge of drawing itself, or maybe I should stick to letting the classes set their data according to events (KeyboardState) on the Update method, and keep all graphic logic in the Game class.
Is there a preferred way to do this?
Generally, I have a base class that contains a BaseModel, texture data, rotation and scale data, etc. For each type of actor in the game, I create a derived class. The base class provides a Draw method that, by default, draws the model with the texture, rotation, and scale data given in the class. Derived classes can override it to draw the actor however they like.
Then, I have a DrawableGameComponent that acts as my scene graph. It contains a list of all active actor objects. In the component's Draw and Update methods, I iterate through the list of actors and call their Draw and Update methods.
That's one way of approaching it ... for the sake of completeness in this post, I'll highlight the other approach. Basically, the opposing view states that no one entity should need (or have) custom knowledge of how to render itself. An entity is merely a collection of state ... and the renderer can simply look at that state, and draw it in the correct way.
An example ... say you have a number of ships. Some go fast, some shoot rockets, some have a sattelite orbiting around it that also shoots. Your "Entity" class can have the following properties
Model VisualRepresentation
Matrix Position
Entity[] AttachedEntities
Your renderer can then iterate over your generic "List<Entity>", and
Draw the visual representation (ie. Model) of the entity using the position
Loop over the AttachedEntities and draw them (recursively).
It's obviously a simplified example ... but this way the drawing logic is completely contained in the rendering code, and only needs to concern itself with as little amount of information as possible. While the ship class can focus on the game logic itself (ie. how fast do I fly, what weapon am I using, how much energy do I have in my shields, etc.).
As far as which one is preferred, really the answer lies within your project's requirements, and what you feel comfortable with. Don't try to make a game engine before making a game ... just do whatever it takes to make your game, and then maybe you can extract the components that worked after you ship the game :-P