C# XNA Ball Moving - c#

I have the ball in the very right bottom side. When I click somewhere, I want to be able to figure out the direction I clicked and once the user starts to drag, I will calculate the distance. Once the user lets go of the mouse, I want to give the ball some velocity and have it move towards the direction it was first clicked.
I don't know the formulas to compute these things. Any help with explanation is greatly appreciated.
Thanks.

You can check the first mouse down Mouse.GetState() (I think) and save it to a variable. Then check, if the mousedown-state is still given (do this in the Update() function), if not, this will be your destination point. Now you have a starting point and destination point, so you can move your ball in the update-method through the destination point. I hope, the explanation is clear :)

Related

How to get a specific point on a vector3 direction?

i could need some help with this problem:
I have a ball flying around in random directions. everytime he collides with something, he changes the direction.
i also have a player, for which i want to write a simple ai script.
the player should go to the point, where he could meet the ball.
i made a simple picture for this problem in paint so that it's better to understand:
What i have is the yellow arrow, thats the ball's direction that it's moving. what i need is the red arrow direction for the player, but i don't know how to do this?!
thanks for your help!!

Unity drag and throw a gameobject (ex: sprite) with mouse

I want to throw a gameObject (like a sprite) with my mouse.
I would like to handle to power if the mouvement is fast.
I've try to develop it myself but I haven't found yet.
In my mind I think I need to do this :
on mouse down : get the position
0.5secs after of on mouse up : get the new position
and calculate the distance between the 2 points.
Conclude a direction and a force.
What do you think about this ?
I created something fairly similar to this. You should do OnMouseDown() then Raycast on the position of the mouse click. Then your Raycast should grab the collider of the GameObject that was hit. Then you send a message, SendMessage, to that collider telling it what you want to do.
All the links I have given you and some trial an error will help you construct what you need, that is how I created my level editor which is in essence the same thing. Note that all links have code samples that teach you all you need to do. Just use your imagination and you can get it done!
Just start coding. If you get stuck then ask a more specific question and someone will help you. Good luck.

Sprite Targeting

So, I've been working on a game for awhile, and I've run into a snag. I have no idea how to go about this. Here's my problem:
I have ship, with forward-facing railguns and two missiles. The railguns can only fire in a straight line in front of the ship, but the missiles can fire if a ship is anywhere in a cone in front of the ship. The problem, is that the ship is rotating and moving about the screen. For the railguns, I need to check if there's a ship anywhere in front of the ship on the screen. For the missiles, I need to check if there's a ship within 250px in front of the ship, but no more than 45 degrees to either side.
Any help you guys could give would be appreciated. Thank you!
For the railguns you could do somthing like this:
public delegate void inRange();
public event inRange shipInRange;
public void checkRange() {
if(enemyShip.position.x < ship.position.x + 250 ||
enemyShip.position.x > ship.position.x + 250)
shipInRange();
}
Although I haven't tested it yet, you should check if enemyship is 250 units behind or ahead of ship. This assumes that you are only moving your boats on the x axis. Although it would be easy to add the y axis, I also recommend a check for which direction you are facing, then put a event for each direction and listen to the one you are facing. That would probably be the easiest way.
Then all you have to do is to listen for that event, and do what you need to do when it happens. Same thing for the missile, but check the angle as well.
Also if you had some example code, I might be able to provide you with a better example.

How to detect direction of HorizontalDrag in WP7 XNA?

Like mentioned what is the best way of detecting in which direction the user is dragging horizontally. I'm trying to create a camera class that responds to this gesture but am having problems determining which direction they are dragging. Any suggestions are appreciated.
Admittedly I haven't tested this, but the documentation suggests that you should check the value of GestureSample.Delta.X, which should be negative for a left movement, and positive for a rightwards one.
Because the delta is only for that particular gesture sample (not the overall gesture), you may need to accumulate it, and only trigger your drag action if the accumulated value is above some threshold (possibly upon receiving a DragComplete).

Collision detection 2D between rectangles

I am writting a collision detection engine for my game and I have some problems.
Indeed, since I have several fixed rectangle and one moving (the player), I need to know which side of the fixed one was collided at first by the player, to replace him correctly.
The fixed rectangle are NOT in a grid so they can be placed anywhere on the map and they can have diffents size. They are not rotated.
The player class stores it's direction vector.
Any idea?
KiTe
In a nutshell:
You'll compare the Y and X components of the bounding rectangles to eachother to check for a collision. If the top(Y) of the player is less than the bottom of an enemy then you don't need to check anymore because it's not possible that they're colliding. If the right(X) side of the player is less than the left side of the enemy then they can't be colliding. It would help to define top, right, bottom, left of each object you intend to check inside the class. This will allow you to know which side is hit also. This should be enough to get you thinking and experimenting.
Have fun!
The name is "Axis-Aligned Bounding Box" collision detection.
Now you know the name, you can Google for the rest.
thanks to both of you for your help.
I've heard about AABB, but at first sight it didnt seem to fit to my needs (since I didn't understand it well).
But after writing down everything on papper, the solution I found appeared to be exactly the same as AABB !

Categories