Moving object any direction on the screen.

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 332
  • Joined: 11-July 12

Moving object any direction on the screen.

Posted 07 October 2013 - 08:12 AM

I do not know where to put this topic, here or in game development, I hope you will not get angry. :)

So, I have a player, which is pretty much oval shape on the screen. Player can shoot a bullet. User uses mouse cursor to determine the direction where the bullet should go. It should be simple, if player would only be allowed to shot UP, DOWN, RIGHT, LEFT. But things gets complicated for me when I want to make allow shot any desired angle. For example 45,40,30 etc.

How can I achieve this? I put here a picture, which shows what I want to do.
Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Moving object any direction on the screen.

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 08:19 AM

How familiar are you with right triangle trig and vector math?

Also, I'm going to move this to Game Development for better visibility.
Was This Post Helpful? 0
  • +
  • -

#3 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 332
  • Joined: 11-July 12

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 08:21 AM

View Postmacosxnerd101, on 07 October 2013 - 08:19 AM, said:

How familiar are you with right triangle trig and vector math?

Also, I'm going to move this to Game Development for better visibility.

I never faced off with these in programming, but at school - yes.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 08:24 AM

So you should be able to apply them here. :)
Was This Post Helpful? 0
  • +
  • -

#5 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 332
  • Joined: 11-July 12

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 08:33 AM

View Postmacosxnerd101, on 07 October 2013 - 08:24 AM, said:

So you should be able to apply them here. :)/>

Hm, maybe you can give me a starting point? :D

Am I doing it well?
Attached Image
Was This Post Helpful? 0
  • +
  • -

#6 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:02 AM

Hey there, I use a method like this to get the mouse direction depending on some position. (in your case the player position)

        private static Vector2 GetMouseAimDirection(Vector2 position)
        {
            Vector2 direction = MousePosition - position;

            if (direction == Vector2.Zero)
                return Vector2.Zero;
            else
                return Vector2.Normalize(direction);
        }



This tutorial talks about what you're looking for.

http://gamedev.tutsp...basic-gameplay/

Hope this helps. :)
Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:14 AM

Quote

Am I doing it well?

Looks pretty good to me! Just remember that tangent is continuous on the open interval (-pi/2, pi/2). The Math.atan2() method may be of help here if you're sticking with Java.

See the documentation.
Was This Post Helpful? 1
  • +
  • -

#8 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 332
  • Joined: 11-July 12

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:21 AM

View Posttraxix, on 07 October 2013 - 09:02 AM, said:

Hey there, I use a method like this to get the mouse direction depending on some position. (in your case the player position)

        private static Vector2 GetMouseAimDirection(Vector2 position)
        {
            Vector2 direction = MousePosition - position;

            if (direction == Vector2.Zero)
                return Vector2.Zero;
            else
                return Vector2.Normalize(direction);
        }



This tutorial talks about what you're looking for.

http://gamedev.tutsp...basic-gameplay/

Hope this helps. :)/>

I have one problem, my eclipse doesn't know vector2, it knows only vector. Is this good? My game is 2D. Also, your tutorial is little bit advanced I think, cause I don't understand, for example the whole principle how this angle creation works. Also what is inside Vector. How you use them to solve this problem. :(
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:23 AM

I think traxix is suggesting an XNA approach. If you're not using XNA, then his code isn't particularly relevant.
Was This Post Helpful? 1
  • +
  • -

#10 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 332
  • Joined: 11-July 12

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:24 AM

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

Quote

Am I doing it well?

Looks pretty good to me! Just remember that tangent is continuous on the open interval (-pi/2, pi/2). The Math.atan2() method may be of help here if you're sticking with Java.

See the documentation.


Okey, so for example I have angle. What's know?

This post has been edited by novakasss: 07 October 2013 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:27 AM

Are you doing any thinking about this or are you just asking us to hold your hand? Seriously- make some real effort here.
Was This Post Helpful? 0
  • +
  • -

#12 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:28 AM

I hope I got this right, but this is the short version:
You will need to know the vector from the center of your player and to the mouse position
Normalize the vector and using the speed of the bullet to determine the dx and dy
Was This Post Helpful? 1
  • +
  • -

#13 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 332
  • Joined: 11-July 12

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:30 AM

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

Are you doing any thinking about this or are you just asking us to hold your hand? Seriously- make some real effort here.

Sorry, It is just a first time im am doing this and I have no idead what to do with that angle. Do I need to rotate graphics object with that angle?
Was This Post Helpful? 0
  • +
  • -

#14 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 79
  • View blog
  • Posts: 163
  • Joined: 27-May 13

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:30 AM

View Postnovakasss, on 07 October 2013 - 04:24 PM, said:

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

Quote

Am I doing it well?

Looks pretty good to me! Just remember that tangent is continuous on the open interval (-pi/2, pi/2). The Math.atan2() method may be of help here if you're sticking with Java.

See the documentation.


Okey, so for example I have angle. What's know?


The way I worked it out.
You have a "speed" variable. which tells you how fast a object can move per frame.
This is your vector.
You also know the angle.

From this info you can calculate the x and y speeds.

Basically, with the angle and a vector (x,y or total (not really sure how it's called in english)), you can calculate the other 2 vectors.

And then you can add those speeds to the charactes x and y values.
Was This Post Helpful? 1
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Moving object any direction on the screen.

Posted 07 October 2013 - 09:31 AM

You've had high school physics, trig or calculus right? Apply what you learned there to particle motion.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2