Moving object any direction on the screen.

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 687 Views - Last Post: 07 October 2013 - 01:52 PM Rate Topic: -----

#16 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 167
  • Joined: 27-May 13

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:33 AM

View Postmacosxnerd101, on 07 October 2013 - 04:31 PM, said:

You've had high school physics, trig or calculus right? Apply what you learned there to particle motion.


being able to think about problems mathematically is indeed important.
Was This Post Helpful? 0
  • +
  • -

#17 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 347
  • Joined: 11-July 12

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:35 AM

View Postmacosxnerd101, on 07 October 2013 - 09:31 AM, said:

You've had high school physics, trig or calculus right? Apply what you learned there to particle motion.

I'm studying not in the high school, I'm at the 11th grade. And I know only how to add vectors. Pretty much that's it.
Was This Post Helpful? 0
  • +
  • -

#18 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,469
  • Joined: 27-December 08

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:45 AM

A good introduction to vectors. You need to do some reading rather than expecting us to hold your hand through vector math.
Was This Post Helpful? 1
  • +
  • -

#19 traxix  Icon User is offline

  • D.I.C Head

Reputation: 85
  • View blog
  • Posts: 116
  • Joined: 10-August 13

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:48 AM

I was offering a more conceptual method, with an XNA example. So you get the idea of how it's done and then you can implement it in your game. But of course that supposed you knew how Vectors work and their properties and stuff. So your best bet would be to study up on vectors. Everything in games is done with vectors. (pretty much :P) :)
Was This Post Helpful? 0
  • +
  • -

#20 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 347
  • Joined: 11-July 12

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 10:21 AM

Can you take a look?
	private void setVectors(){
		//First vector
		double vectorX1 = player.getX()+10;
		double vectorY1 = player.getY()+10;
		
		//Second vector
		double vectorX2 = mouseX;
		double vectorY2 = mouseY;
		
		//Vector from player to mouse
		double deltaX = vectorX2 - vectorX1;
		double deltaY = vectorY2 - vectorY1;
		
		//Normalization ???
		double unit1 = Math.sqrt(Math.pow(deltaX,2)+Math.pow(deltaY,2));
		deltaX = deltaX / unit1;
		deltaY = deltaY / unit1;
		
		double radian = Math.atan2(deltaY, deltaX);
		
		speedX = speed*(float)Math.cos(radian);
		speedY = speed*(float)Math.sin(radian);
	}


Is this the way it should work?

This post has been edited by novakasss: 07 October 2013 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

#21 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 560
  • View blog
  • Posts: 1,263
  • Joined: 24-April 12

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 01:52 PM

I might suggest you check out my article on vectors.

But here's the way I'm envisioning it.

I'm thinking you've got your player's object that fires bullets. It has it's position kept by a vector that's not "really" a vector, but actually just the position where the object is at on the screen. Likewise, I'm thinking the point your mouse is pointing to is again a position stored in a 2D vector (vectors can be 3D also) and not an actual vector.

What I'm imagining you are wanting is a bullet to launch from the player object in the direction of the mouse and then travel forever in that direction (being deleted after a certain distance or time).

So, you can use vector math subtraction to produce an actual vector that "points" from the player object to the mouse cursor. I believe that would be BulletVector = MouseCursorPosition - PlayerObjectPosition where all 3 are stored in Vector objects.

So, at this point you have a "BulletVector" that points in the direction from the Player to the Cursor. If you add the "BulletVector" to the PlayerPosition vector the result will be the exact spot where the cursor is at, although we probably don't want to do this.

Thinking of it as an actual vector, it points in exactly the right direction, but it's length/amount is basically meaningless at this point. The direction is perfect, but the length/amount needs to represent something more "useful" such as velocity/movement per game frame (generally 60 frames per second depending on what environment you are in and this can be extremely important, so find out).

So, what we want to do is change the length of the vector to our bullet velocity/speed/movement per frame. To change a vector's length/amount, you normalize it. There is "probably" a method in your environment to just "do" that for you. But you could also do it yourself mathematically. It's been awhile since I've actually had to do it, but I think you simply divide the vector by it's magnitude/length/amount. It's a vector divided by a scalar (scalars are normal non-vector numbers). When you're done, the vector should point in the exact same direction but have its length set to exactly one. A vector with a length of one is a normalized vector, or unit vector.

Now to set the length of a vector, you normalize it and then multiply the normalized vector times the length that you want it to be. (That's why we normalize it first.) So, if you want to set the velocity to 0.03 units per frame (at 60 fps that's 1.8 units per second) you just multiply the vector times 0.03 and it's length will become 0.03.

Now you've got a vector that points in the direction from the Player to the Cursor and has a length of 0.03. Create a bullet object and assign a vector to hold it's position. If you add the 0.03 vector that we just created to it's position vector every frame, it will move in that direction by that amount every frame. Maybe delete the bullet after a certain number of frames (or it will eventually crash your game).

Does that help?
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2