This is my first post here so please be gentle. This is my first time making a game in C# XNA (I know it's not supported anymore). I am attempting to spawn rocks on the ground the the player collides with. I have made collision with many other objects in the game, but for some reason it's not working on these rocks.
Whenever I run the code I get a Null reference exception on line 146 of my room.cs (see first link below)
I have only tried adding collision to the first rock in the first case of the switch statement in my room.cs, so ignore the the other statements in the switch beyond line 146, they are there just to draw the textures in the correct place.
If there is anything more I need to add please say so. Like I said this is my first time posting here so I may not be very good at this.
Room.cs
http://pastebin.com/WeYSpBZa (line 146 is where the error occurs on runtime)
Rock.cs (didn't know if you needed this to help figure it out, so I added it anyway)
http://pastebin.com/6NYZrFir
Your rocklist is null. Instantiate this field in the constructor : rockList = new List<Rock>();
Related
I'm coding a 2d tile-based, turn-based game in Unity, and have been struggling to solve a problem for the past two days. At first, I managed to get tile to tile pathfinding working using the Dijkstra algorithm. Next, I tried modifying the code to instead efficiently pathfind to every tile within a character’s movement range
To that end, I’m trying to make a method that returns a dictionary. The keys are all the reachable WorldTile tiles within the character’s mobility range (WorldTile is a class representing each tile in the game) and the value for each key is a PathAndCost pathAndCost (PathAndCost is a class that stores 2 properties: Path, of type Queue and Cost, of type Double).
This way, I can store a list of all the tiles the character can reach while simultaneously also storing the path to each tile (represented by a Queue of tiles) and the cost to reach each tile (represented by a Double).
However, in practice, there is something wrong with the method and I can’t figure out what exactly is wrong. Through testing, I’ve confirmed that the dictionary that the method returns DOES correctly contain all the reachable tiles as its keys. Meanwhile, the pathAndCost values returned are only half correct. Strangely, each pathAndCost.Cost is correct. However, pathAndCost.Path is always wrong. When I check the queue count of the paths being stored, it’s always 0, meaning no tiles are stored in the paths!
github link to my script
(apologies if formatting of my post or code is messy as I am new to both stackoverflow and programming)
If you look at the two Debugging blocks I wrote in my script, INSIDE the StorePathAndCost loop, the paths are indeed being made correctly as queue counts there are not always 0. Somehow, something goes wrong afterwards that sets the paths back to being empty before the method returns the dictionary.
Since the tile keys and cost values aren’t being affected, I highly suspect that there is a logical error in the way I’m making and storing the paths within the StorePathAndCost loop. I’ve stared at my code for so long and can’t figure out what it is though.
EDIT: Solved! Realised that the problem lies in the Dequeue() method affecting my path values even after they were stored in the dictionary.
Not going to wade through your whole program code, but I will offer help in the form a nice video tutorial showing a successful way of implementing pathfinding for ya.
Tarodevs Pathfinding
I'm stuck on collision detection:
I've read about different ways of dealing with collisions, and choose to implement it with AABB. Problem appears right in the beginning: I have my test object - wood tower, and I'd like to enclose it with AABB object, I'm iterating through vertices to find min and max values for each axis, which works ok, but takes a lot time, even for single one object (It's quite big object I think ~8000 vertices).
What is proper way of doing it? Should data like this be in object files or I'm just doing it wrong way? Object may be big (I don't know if it is), but it's just one object, I'm afraid about what will happen when I set more of them
I'm writing with c# and OpenTK
Edit:
I know I should use the simplified collision model, I'm trying to find the boundaries over which I will build the AABB box (I had the screen hooked up, but it looks like mods didn't like it). Iterating over each vector seems somehow wrong (although it works, I have a nice box, fitted to the size of the model, but not in the effective processing time)
Yes, you should do that when creating the model resource file. I think your calculation method is fine as is.
And then if this model rotates or scales in your game, make a new AABB from the OBB(which was the AABB loaded from the file before transformation).
First off, I'd like to note that I've seen the other topic trying to explain this.
I've read over the topic and tried to implement the math, but it doesn't seem to work. I'm probably doing something wrong.
For reference, I'm using C# in the Unity engine. I'm working on a game simmilar to Miegakure, in the fact that you "turn" to face the fourth dimension, seeing it in slices. This means that projecting 4D objects to 3D doesn't work. I need to get 3D Cross-sections.
I already have this working for my actual terrain itself, since it's voxels which makes it easy to change how the mesh generator iterates through the 4D world.
My current problem is non-voxel meshes. I want to have them in my world, for things like the player, and dynamic blocks that move from dimension to dimension (Therefore giving the effect of hypercubes)
In that post, they use some complicated vector math, which I don't think I understand.
So long story short, I just need a slightly better explanation. Does anyone have a link I can steal of an example from 3D>2D slicing, so I can try and extend it to 4D>3D?
Literally any help, even just a re-explanation of that previous post, or someone translating it to actual script, would be nice.
Thanks in advance!
I'm trying to make a truncated icosahedron, though with more subdivision (so more hexagons)
In the game I use it, eacht pentagon and hexagon is a separate object. So after generating the icosahedron, I just use the generated points to place either a pentagon or a hexagon on it (instead of doing the find-middles-of-each-triangle-thing, I do this since I need them to be separate object anyway.) I have some questions about it though, and google doesn't really help, so I'm hoping there are some smart smath-knowing people here :D
Here we go:
Am I assured that the length of each sides is equal?
Since each hexa/petagon is a separate object, I need to rotate them to get them positioned properly, any help with this?
Assuming I have hexa/petagons with a radius of 1 (one), how far for the middle do I have to position them? (Basecly, whats the relationship between the radius of my hexa/pentagons and the radius of my truncated icosahedron.)
Here's my first test, I generated a icosahedron and then on each point put a pentagon model, which I rotate so it's pointing away from the middle. As you can see they still need to be rotated to fit together (question 2) and their distance to the middle has to the tweaked aswell (question 3).
I'll continue on working on this too, though all help will be appreciated! (I'm making this in Unity, using c#, so if you give sample code, it would be really really awesome if you use that.)
Thanks a lot!
Well, not the answer to your questions but maybe worth thinking about:
Wouldn't it be easier to start with a ready made Blender, Maya, ... model of a soccer ball like for example this one on Blend Swap, change it to fit your needs. Or do it on your own as there are a couple of YouTube tutorials. Then you will have far more options like LOD, materials. You can design it in Blender with each pentagon/hexagon as single object and so it will be imported in Unity.
I'm coding a server for a multi-player RPG, and I'm currently struggling with implementing a sight range. Since some maps are rather large, I have to limit what the client sees. My approach:
If I get new coordinates from the client, I save them as the destination, together with a move start time. Once every x ms I go through all creatures in the world, and update their current position, after saving the position they were at the last time I've updated them. Basically I calculate the new position, based on move start time and speed, and write those in the current position variables, while saving the new start time. Once this update is done, I'm going through all creatures which moved, aka those who have a different position than at the last update. In a sub-loop I go through all creatures/clients again, to check if I have to notify them about a (dis)appearing creature. At the moment I'm running this update every 100ms.
This approach is working, but I have a feeling it's not the best way to do this. And I'm not sure what will happen once I have a few thousand creatures (players, monster, etc) in the world, which have to be updated and checked.
Since I weren't able to find resources about this particular problem, I'm asking here.
Is this approach okay? Will I run into problems soon? What's the standard to do this? What's the best way?
Eric Lippert had a really good series of posts on shadowcasting that might be helpful in approaching/solving this.
You may want to consider using quadtrees to split the game world into sections based on the areas that player characters can see. Then you don't need to loop over every creature in the game all the time; you only need to loop over the ones within the section that the player character in question is located in, and any adjacent ones in case something crossed the boundary.
I haven't done this sort of coding personally myself, but I did work with someone who did this in a space combat game for which I was developing a GUI!