Using maths to solve programming problems.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Sosolol261
Party member
Posts: 125
Joined: Wed Nov 26, 2014 6:43 am

Using maths to solve programming problems.

Post 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
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Using maths to solve programming problems.

Post 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.
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Using maths to solve programming problems.

Post 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
Last edited by Qcode on Sun Sep 13, 2015 4:06 pm, edited 1 time in total.
Sosolol261
Party member
Posts: 125
Joined: Wed Nov 26, 2014 6:43 am

Re: Using maths to solve programming problems.

Post by Sosolol261 »

Thatnk you guys for your replies!! I'll make good use of it.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Using maths to solve programming problems.

Post 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
User avatar
radgeRayden
Prole
Posts: 29
Joined: Sun Jul 27, 2014 6:49 pm
Location: Brasil
Contact:

Re: Using maths to solve programming problems.

Post 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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 6 guests