Magnetism & items

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Locked
User avatar
Accensus
Banned User
Posts: 2383
Joined: Thu Feb 11, 2016 9:59 am

Magnetism & items

Post by Accensus »

So I stumbled upon this issue where I have monsters drop coins but picking them up in open areas is a pain, so I am wondering - is there a way to make the player magnetic? As in they move towards the player when he is close enough. I was thinking of ThrustThing in the Spawn state of the coin itself, but I don't know how to determine the angle at which the player is located from the coin.

Long story short, I needed to make coins chase the player when he gets in specific range. After lots of head banging I realised that sometimes the answer is very, very simple. As promised, here's the code which makes coins follow you.

Code: Select all

Actor DRCoinPickup : CustomInventory
{
	Height 15
	Radius 20
	Speed 5 //Change that for faster coins.
	+INVENTORY.NOSCREENFLASH
	Inventory.PickupSound "Cash/CoinPickup"
States
	{
	Spawn:
		COIN A 0 A_CheckProximity("ISeeYou", "DoomPlayer", 256, 1, CPXF_ANCESTOR)
		COIN ABCDEF 2
		Loop
	ISeeYou:
		COIN ABCDEF 2 A_Chase
		Goto Spawn
	Pickup:
		TNT1 A 0 A_GiveInventory("DRCash")
		Stop
	}
}
User avatar
Accensus
Banned User
Posts: 2383
Joined: Thu Feb 11, 2016 9:59 am

Re: Magnetism & items

Post by Accensus »

I managed to solve it. Edited main post. Added source code.

EDIT: Lel, after testing it turns out the code Does work, but the coin movement is kinda hilarious. Guess I'll have to work on that, but I at least have a base now. :)

I suppose adding CHF_NORANDOMTURN might fix it. Eh, it doesn't. I'll come up with a fix...someday.
Last edited by Accensus on Fri May 06, 2016 11:50 am, edited 1 time in total.
User avatar
The Zombie Killer
Posts: 1528
Joined: Thu Jul 14, 2011 12:06 am
Location: Gold Coast, Queensland, Australia

Re: Magnetism & items

Post by The Zombie Killer »

Take a look at [wiki]A_RadiusThrust [/wiki]
Edit: on second thought this might not be helpful for what you want to do
User avatar
Accensus
Banned User
Posts: 2383
Joined: Thu Feb 11, 2016 9:59 am

Re: Magnetism & items

Post by Accensus »

Already tried it mate. Didn't work out as expected and I have to put it on the player. Not to mention it pulls everything. Downloaded a dev build to test the CHF_NORANDOMTURN flag and see if it'll work.
User avatar
The Zombie Killer
Posts: 1528
Joined: Thu Jul 14, 2011 12:06 am
Location: Gold Coast, Queensland, Australia

Re: Magnetism & items

Post by The Zombie Killer »

You could try using a combination of A_FaceTarget and A_ChangeVelocity, so that you don't need to abuse AI functions
The calls would probably look something like this:

Code: Select all

A_FaceTarget(0, 180)
A_ChangeVelocity(cos(angle) * cos(pitch) * speed, sin(angle) * cos(pitch) * speed, sin(pitch) * speed, CVF_REPLACE)
User avatar
Accensus
Banned User
Posts: 2383
Joined: Thu Feb 11, 2016 9:59 am

Re: Magnetism & items

Post by Accensus »

Much appreciated! I was looking for that formula. Will try it and post results.

EDIT. Yep, it works. Many thanks mate, you saved me hours of work. :)
User avatar
NeuralStunner
 
 
Posts: 12328
Joined: Tue Jul 21, 2009 12:04 pm
Preferred Pronouns: No Preference
Operating System Version (Optional): Windows 11
Graphics Processor: nVidia with Vulkan support
Location: capital N, capital S, no space
Contact:

Re: Magnetism & items

Post by NeuralStunner »

Let's simplify TZK's code a bit. Most of that cruft is unnecessary when you're already facing the target.

Code: Select all

A_FaceTarget(0, 180)
A_ChangeVelocity (Cos(Pitch) * Speed, 0, Sin(Pitch) * -Speed, CVF_Relative|CVF_Replace)
You might recognize this snippet. :P
Locked

Return to “Editing (Archive)”