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 » Sat Sep 12, 2015 6:01 pm

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 » Sat Sep 12, 2015 6:27 pm

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 » Sat Sep 12, 2015 7:15 pm

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 » Sun Sep 13, 2015 2:26 am

Thatnk you guys for your replies!! I'll make good use of it.

User avatar
ivan
Party member
Posts: 1450
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Using maths to solve programming problems.

Post by ivan » Sun Sep 13, 2015 6:44 am

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: 28
Joined: Sun Jul 27, 2014 6:49 pm
Location: Brasil
Contact:

Re: Using maths to solve programming problems.

Post by radgeRayden » Wed Sep 16, 2015 12:41 pm

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: Exabot [Bot], Google [Bot] and 9 guests