Jump to content
  • Advertisement
Sign in to follow this  
ChristianFrantz

Simple for loop issue

This topic is 2240 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have three for loops that are used to loop through an array of cubes and set the World matrix to a position. The problem is that all the cubes are being drawn on top of each other. Am i missing something? I was able to do this before somehow. Would the order of the loops matter? My goal here is to create a 10x10 grid of cubes.

http://pastebin.com/LcvSCLV8

Im on my phone so i cant post any code so sorry for the inconvenience :S

Share this post


Link to post
Share on other sites
Advertisement

The particular problem you're having has nothing to do with the for loops.

 

You're trying to use the world matrix as a vector... or something.  It's hard to describe.

 

Your WVP matrix is the multiplication of "World", "View", and "Projection"; currently you're just multiplying VP together.

Likewise you probably shouldn't be setting your vertex declaration to your world matrix.  I think this is XNA, and if it is, that doesn't seem right.

 

EDIT:  Does this help?

http://blogs.msdn.com/b/shawnhar/archive/2010/06/17/drawinstancedprimitives-in-xna-game-studio-4-0.aspx

Edited by SeraphLance

Share this post


Link to post
Share on other sites

Looking at the code there, these cubes are 2x2x2, so you want to space by that much.  However, seeing as how you're spacing them on 1x1 intervals, they shouldn't be completely on top of each other.

Share this post


Link to post
Share on other sites

Oh i got it. Removed the first loop and used x * z for the index

 
This is wrong. If you want a linear index for a "2D array", it's x * stride + z (or z*stride + x). Your stride is probably count here, but note that you also allocate too small an array. It should be rather Cube[] cubes = new Cube[count*count].

Your original problem was that you set count*count cubes to the same index i. I advise to not use an array to collect cubes, but List<Cube> and Add them as you generate them. Then finally use ToArray(). No hassling with manual indexing needed.

@SeraphLance: The code is a bit confusing (View*Projection used for the WVP uniform) but I think it's ok: You use View*Projection since the world transformation comes from the instances.

As an aside: If you're not going to rotate and scale your cubes, a simple offset can be used instead of a full world transformation. Much less memory footprint wink.png Edited by unbird

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!