Page 1 of 1

Using maths to solve programming problems.

Posted: Sat Sep 12, 2015 6:01 pm
by Sosolol261
Now.. I have a little problem here.
I have a player and that player is going to shoot a bullet.
The bullet is going to be aimed at (goalX, goalY) but I want it to go until (actualX, actualY). How can I achieve that?

I would prefer to use maths of some kind since maths is.. cool. yeah.

Image

Re: Using maths to solve programming problems.

Posted: Sat Sep 12, 2015 6:27 pm
by undef
You create a vector (GoalX - PlayerX, GoalY - PlayerY), then you add this Vector to your player coordinates with a coefficient greater than one.

Or in simple words:
You calculate the difference between the x and y coordinates of your player and the goal, then you add those values times something to the player coordinates.

If you do:

Code: Select all

ActualX = PlayerX + (GoalX - PlayerX)*2
ActualY = PlayerY + (GoalY - PlayerY)*2
The point of collision will be twice as far away from the player than the mouse position.

Re: Using maths to solve programming problems.

Posted: Sat Sep 12, 2015 7:15 pm
by Qcode
Creating a vector by subtracting is a good start, but you'll probably also want to divide by the total distance between the "Player Point" and the "Mouse Point". Otherwise, you'll have variable speed based on how far the mouse is away from the player. Theoretically you could design this as a gameplay mechanic but it would probably be more intuitive to hold down the mouse to charge and increase the bullet's speed.

Anyway, if you write some code like this.

Code: Select all

function getdistance(x1, y1, x2, y2)
	return math.sqrt((x2-x1)^2 + (y2-y1)^2)
end

--on mouse click
local overalldistance = getdistance(PlayerX, PlayerY, GoalX, Goaly)
bullet.xspeed = (GoalX-PlayerX)/overalldistance
bullet.yspeed = (GoalY-PlayerY)/overalldistance

--on update
bullet.x = bullet.x + bullet.xspeed * speedmodifier * dt
bullet.y = bullet.y + bullet.yspeed * speedmodifier * dt
Then you can adjust the speedmodifier variable to make the bullets however fast or slow you want, and the speed will remain consistent no matter the distance between the player and the mouse.

EDIT: Fixed a little thing where I had the Goal and Player subtraction round the wrong way

Re: Using maths to solve programming problems.

Posted: Sun Sep 13, 2015 2:26 am
by Sosolol261
Thatnk you guys for your replies!! I'll make good use of it.

Re: Using maths to solve programming problems.

Posted: Sun Sep 13, 2015 6:44 am
by ivan
Good example by QCode.
One thing I would add is a check that avoids a possible division by 0:

Code: Select all

--on mouse click
local overalldistance = getdistance(PlayerX, PlayerY, GoalX, Goaly)
if overalldistance > 0 then
  bullet.xspeed = (GoalX-PlayerY)/overalldistance
  bullet.yspeed = (GoalY-PlayerY)/overalldistance
end

Re: Using maths to solve programming problems.

Posted: Wed Sep 16, 2015 12:41 pm
by radgeRayden
A good idea is to treat positions effectively like vectors. I happen to have a module I made for myself that you can study and take ideas to make your own or simply use it if you like.
https://gist.github.com/radgeRayden/2ae ... a4f3a9c76c