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.
Using maths to solve programming problems.
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Party member
- Posts: 125
- Joined: Wed Nov 26, 2014 6:43 am
Re: Using maths to solve programming problems.
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:
The point of collision will be twice as far away from the player than the mouse position.
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
Re: Using maths to solve programming problems.
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.
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
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
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.
-
- Party member
- Posts: 125
- Joined: Wed Nov 26, 2014 6:43 am
Re: Using maths to solve programming problems.
Thatnk you guys for your replies!! I'll make good use of it.
Re: Using maths to solve programming problems.
Good example by QCode.
One thing I would add is a check that avoids a possible division by 0:
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
- radgeRayden
- Prole
- Posts: 29
- Joined: Sun Jul 27, 2014 6:49 pm
- Location: Brasil
- Contact:
Re: Using maths to solve programming problems.
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
https://gist.github.com/radgeRayden/2ae ... a4f3a9c76c
Who is online
Users browsing this forum: Ahrefs [Bot] and 6 guests